Skip to main content

ifc_template_catalog/diagnostic/
schema.rs

1use crate::catalog::Catalog;
2use crate::definition::{PropertyKind, PropertyTemplate, SetTemplateKind};
3
4use super::{issue, visit_data_types, CatalogDiagnostic, DiagnosticCode, DiagnosticSeverity};
5
6/// Minimum schema capability needed to validate external catalog names.
7pub trait CatalogSchema {
8    /// True when the schema declares an entity by this exact name.
9    fn has_entity(&self, name: &str) -> bool;
10    /// True when the schema declares a defined type by this exact name.
11    fn has_type(&self, name: &str) -> bool;
12}
13
14impl Catalog {
15    /// Validate entity and value-type references against the selected IFC schema.
16    pub fn schema_diagnostics(&self, schema: &impl CatalogSchema) -> Vec<CatalogDiagnostic> {
17        let mut output = Vec::new();
18        for template in self.iter() {
19            for selector in &template.applicability {
20                if !schema.has_entity(&selector.entity) {
21                    output.push(issue(
22                        DiagnosticCode::UnknownApplicableEntity,
23                        DiagnosticSeverity::Error,
24                        &template.name,
25                        None,
26                        &format!("unknown applicable entity `{}`", selector.entity),
27                    ));
28                }
29            }
30            if let SetTemplateKind::Property { properties, .. } = &template.kind {
31                inspect_properties(&template.name, properties, schema, &mut output);
32            }
33        }
34        output
35    }
36}
37
38fn inspect_properties(
39    template: &str,
40    properties: &[PropertyTemplate],
41    schema: &impl CatalogSchema,
42    output: &mut Vec<CatalogDiagnostic>,
43) {
44    for property in properties {
45        visit_data_types(&property.kind, &mut |type_name| {
46            if let Some(type_name) = type_name {
47                if !schema.has_type(type_name) {
48                    output.push(issue(
49                        DiagnosticCode::UnknownPropertyDataType,
50                        DiagnosticSeverity::Error,
51                        template,
52                        Some(&property.name),
53                        &format!("unknown property data type `{type_name}`"),
54                    ));
55                }
56            }
57        });
58        match &property.kind {
59            PropertyKind::ReferenceValue { reference_type }
60                if !schema.has_entity(reference_type) =>
61            {
62                output.push(issue(
63                    DiagnosticCode::UnknownReferenceEntity,
64                    DiagnosticSeverity::Error,
65                    template,
66                    Some(&property.name),
67                    &format!("unknown reference entity `{reference_type}`"),
68                ));
69            }
70            PropertyKind::Complex { properties, .. } => {
71                inspect_properties(template, properties, schema, output);
72            }
73            _ => {}
74        }
75    }
76}
77
78#[cfg(feature = "schema")]
79impl CatalogSchema for ifc_schema::Schema {
80    fn has_entity(&self, name: &str) -> bool {
81        self.entity(name).is_some()
82    }
83
84    fn has_type(&self, name: &str) -> bool {
85        self.type_def(name).is_some()
86    }
87}