1use ifc_model::Model;
5use ifc_schema::Schema;
6
7#[cfg(feature = "express")]
8use crate::express::ExpressRules;
9use crate::property_set::{self, PropertySetCatalog};
10use crate::report::Report;
11#[cfg(feature = "express")]
12use crate::report::{Finding, Path};
13use crate::where_rule::Budget;
14
15#[derive(Clone, Copy, Default)]
26#[non_exhaustive]
27pub struct ValidateOptions<'a> {
28 pub budget: Budget,
30 #[cfg(feature = "express")]
34 pub express: Option<&'a ExpressRules>,
35 pub catalog: Option<&'a dyn PropertySetCatalog>,
38 pub inverse_cardinality: bool,
44}
45
46impl std::fmt::Debug for ValidateOptions<'_> {
47 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48 let mut debug = f.debug_struct("ValidateOptions");
49 debug.field("budget", &self.budget);
50 #[cfg(feature = "express")]
51 debug.field("express", &self.express.map(ExpressRules::schema_name));
52 debug.field("catalog", &self.catalog.is_some()).finish()
53 }
54}
55
56impl<'a> ValidateOptions<'a> {
57 #[must_use]
60 pub fn new() -> Self {
61 Self::default()
62 }
63
64 #[must_use]
66 pub fn budget(mut self, budget: Budget) -> Self {
67 self.budget = budget;
68 self
69 }
70
71 #[cfg(feature = "express")]
74 #[must_use]
75 pub fn express(mut self, rules: &'a ExpressRules) -> Self {
76 self.express = Some(rules);
77 self.inverse_cardinality = true;
78 self
79 }
80
81 #[must_use]
83 pub fn inverse_cardinality(mut self, check: bool) -> Self {
84 self.inverse_cardinality = check;
85 self
86 }
87
88 #[must_use]
90 pub fn catalog(mut self, catalog: &'a dyn PropertySetCatalog) -> Self {
91 self.catalog = Some(catalog);
92 self
93 }
94}
95
96#[must_use]
103pub fn validate_with_options(
104 model: &Model,
105 schema: &Schema,
106 options: &ValidateOptions<'_>,
107) -> Report {
108 let budget = options.budget;
109 let mut report = Report::with_max_findings(budget.max_findings);
110 crate::header::check(model, &mut report);
111 crate::structure::check(model, schema, budget, &mut report);
112 crate::type_check::check(model, schema, budget, &mut report);
113 if options.inverse_cardinality {
114 crate::structure::inverse_cardinality(model, schema, &mut report);
115 }
116 #[cfg(feature = "express")]
117 let express = options.express.filter(|rules| {
118 let paired = rules.schema_name().eq_ignore_ascii_case(schema.name());
119 if !paired {
120 report.push(Finding::unsupported(
121 "where.express.release",
122 Path::File,
123 format!(
124 "the EXPRESS rules are {}'s, the tables {}'s; the rules were not evaluated",
125 rules.schema_name(),
126 schema.name()
127 ),
128 ));
129 }
130 paired
131 });
132 #[cfg(not(feature = "express"))]
133 let express: Option<&()> = None;
134 #[cfg(feature = "express")]
135 let covers = |id: &str| express.is_some_and(|rules| rules.covers(id));
136 #[cfg(not(feature = "express"))]
137 let covers = |_: &str| false;
138 crate::where_rule::evaluate_except(
139 model,
140 schema,
141 budget,
142 &mut report,
143 &covers,
144 express.is_some(),
145 );
146 #[cfg(feature = "express")]
147 if let Some(rules) = express {
148 crate::express::evaluate(model, rules, budget, &mut report);
149 }
150 property_set::check(model, schema, options.catalog, &mut report);
151 report
152}