Skip to main content

ifc_validate/
options.rs

1//! [`ValidateOptions`]: what a validation run checks beyond the bundled
2//! tables, and how much it may report.
3
4use 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/// Everything a validation run may be given besides the model and its
16/// tables.
17///
18/// ```no_run
19/// # fn demo(model: &ifc_model::Model) {
20/// use ifc_validate::{validate_with_options, Budget, ValidateOptions};
21/// let options = ValidateOptions::new().budget(Budget { max_findings: 100 });
22/// let report = validate_with_options(model, ifc_schema::ifc4(), &options);
23/// # }
24/// ```
25#[derive(Clone, Copy, Default)]
26#[non_exhaustive]
27pub struct ValidateOptions<'a> {
28    /// How many findings to record.
29    pub budget: Budget,
30    /// The release's EXPRESS rules, read from its schema text. With them,
31    /// every WHERE rule, type rule and global rule is evaluated, and the
32    /// hand-written rules they cover are not run.
33    #[cfg(feature = "express")]
34    pub express: Option<&'a ExpressRules>,
35    /// The PSD/QTO catalog that `Pset_` and `Qto_` set names are checked
36    /// against.
37    pub catalog: Option<&'a dyn PropertySetCatalog>,
38    /// Check every INVERSE clause's cardinality
39    /// (`structure.inverse.cardinality`). Off unless asked for, or unless
40    /// EXPRESS rules are supplied: it is part of full EXPRESS conformance,
41    /// and IFC2X3's `PlacesObject : SET [1:1]` alone turns many files that
42    /// share a placement between products non-conformant.
43    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    /// The default budget, no EXPRESS rules, no catalog: what
58    /// [`validate`](crate::validate) runs.
59    #[must_use]
60    pub fn new() -> Self {
61        Self::default()
62    }
63
64    /// These options with another finding budget.
65    #[must_use]
66    pub fn budget(mut self, budget: Budget) -> Self {
67        self.budget = budget;
68        self
69    }
70
71    /// These options evaluating `rules`, and checking INVERSE cardinality
72    /// with them.
73    #[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    /// These options with INVERSE cardinality checked, or not.
82    #[must_use]
83    pub fn inverse_cardinality(mut self, check: bool) -> Self {
84        self.inverse_cardinality = check;
85        self
86    }
87
88    /// These options checking set names against `catalog`.
89    #[must_use]
90    pub fn catalog(mut self, catalog: &'a dyn PropertySetCatalog) -> Self {
91        self.catalog = Some(catalog);
92        self
93    }
94}
95
96/// Validate a model with everything `options` supplies.
97///
98/// Runs what [`validate_with`](crate::validate_with) runs, then what the
99/// options ask for: INVERSE cardinality, every EXPRESS rule of the release
100/// (replacing the hand-written rules with the same id), and the reserved
101/// set-name check against a catalog.
102#[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}