Skip to main content

ifc_template_catalog/
diagnostic.rs

1//! Structural and schema-aware catalog diagnostics.
2
3mod schema;
4
5pub use schema::CatalogSchema;
6
7use std::collections::BTreeSet;
8
9use crate::catalog::Catalog;
10use crate::definition::{PropertyKind, PropertyTemplate, SetTemplateKind};
11
12/// Category of a structural or schema-aware catalog defect.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14#[non_exhaustive]
15pub enum DiagnosticCode {
16    /// A property's official `DataType` element carried no `type` attribute.
17    MissingPropertyDataType,
18    /// An `EnumeratedValue` property has neither lexical values nor documented constants.
19    EmptyEnumeration,
20    /// A set template declares no applicable IFC entity.
21    EmptyApplicability,
22    /// Two members of the same set template (or nested `Complex` scope) share a name.
23    DuplicateMember,
24    /// [`Catalog::schema_diagnostics`] found an applicability entity the schema does not declare.
25    UnknownApplicableEntity,
26    /// [`Catalog::schema_diagnostics`] found a property value type the schema does not declare.
27    UnknownPropertyDataType,
28    /// [`Catalog::schema_diagnostics`] found a `ReferenceValue` entity the schema does not declare.
29    UnknownReferenceEntity,
30}
31
32/// How serious a [`CatalogDiagnostic`] is.
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
34#[non_exhaustive]
35pub enum DiagnosticSeverity {
36    /// Worth flagging but does not indicate malformed catalog data.
37    Warning,
38    /// Indicates malformed or self-inconsistent catalog data.
39    Error,
40}
41
42/// One structural or schema-aware defect found in a catalog snapshot.
43#[derive(Debug, Clone, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct CatalogDiagnostic {
46    /// Category of the defect.
47    pub code: DiagnosticCode,
48    /// How serious the defect is.
49    pub severity: DiagnosticSeverity,
50    /// `Name` of the set template the defect was found in.
51    pub template: String,
52    /// Dotted member path within the template, absent for template-level defects.
53    pub member: Option<String>,
54    /// Human-readable description of the defect.
55    pub message: String,
56}
57
58impl Catalog {
59    /// Report source-shape defects without changing official definitions.
60    pub fn diagnostics(&self) -> Vec<CatalogDiagnostic> {
61        let mut output = Vec::new();
62        for template in self.iter() {
63            if template.applicability.is_empty() {
64                output.push(issue(
65                    DiagnosticCode::EmptyApplicability,
66                    DiagnosticSeverity::Warning,
67                    &template.name,
68                    None,
69                    "template declares no applicable IFC entity",
70                ));
71            }
72            match &template.kind {
73                SetTemplateKind::Property { properties, .. } => {
74                    duplicate_members(&template.name, properties, &mut output);
75                    inspect_properties(&template.name, "", properties, &mut output);
76                }
77                SetTemplateKind::Quantity { quantities, .. } => {
78                    let mut names = BTreeSet::new();
79                    for quantity in quantities {
80                        if !names.insert(quantity.name.as_str()) {
81                            output.push(issue(
82                                DiagnosticCode::DuplicateMember,
83                                DiagnosticSeverity::Error,
84                                &template.name,
85                                Some(&quantity.name),
86                                "duplicate quantity template name",
87                            ));
88                        }
89                    }
90                }
91            }
92        }
93        output
94    }
95}
96
97fn inspect_properties(
98    template: &str,
99    parent: &str,
100    properties: &[PropertyTemplate],
101    output: &mut Vec<CatalogDiagnostic>,
102) {
103    for property in properties {
104        let path = if parent.is_empty() {
105            property.name.clone()
106        } else {
107            format!("{parent}.{}", property.name)
108        };
109        visit_data_types(&property.kind, &mut |type_name| {
110            if type_name.is_none() {
111                output.push(issue(
112                    DiagnosticCode::MissingPropertyDataType,
113                    DiagnosticSeverity::Error,
114                    template,
115                    Some(&path),
116                    "official DataType element has no type attribute",
117                ));
118            }
119        });
120        match &property.kind {
121            PropertyKind::EnumeratedValue {
122                values, constants, ..
123            } if values.is_empty() && constants.is_empty() => output.push(issue(
124                DiagnosticCode::EmptyEnumeration,
125                DiagnosticSeverity::Error,
126                template,
127                Some(&path),
128                "enumerated property has no values",
129            )),
130            PropertyKind::Complex { properties, .. } => {
131                duplicate_members(template, properties, output);
132                inspect_properties(template, &path, properties, output);
133            }
134            _ => {}
135        }
136    }
137}
138
139pub(crate) fn visit_data_types(kind: &PropertyKind, visitor: &mut impl FnMut(&Option<String>)) {
140    match kind {
141        PropertyKind::SingleValue { data_type }
142        | PropertyKind::BoundedValue { data_type }
143        | PropertyKind::ListValue { data_type } => visitor(&data_type.type_name),
144        PropertyKind::EnumeratedValue { data_type, .. } => {
145            if let Some(data_type) = data_type {
146                visitor(&data_type.type_name);
147            }
148        }
149        PropertyKind::TableValue {
150            defining_type,
151            defined_type,
152            ..
153        } => {
154            visitor(&defining_type.type_name);
155            visitor(&defined_type.type_name);
156        }
157        PropertyKind::ReferenceValue { .. } | PropertyKind::Complex { .. } => {}
158    }
159}
160
161fn duplicate_members(
162    template: &str,
163    properties: &[PropertyTemplate],
164    output: &mut Vec<CatalogDiagnostic>,
165) {
166    let mut names = BTreeSet::new();
167    for property in properties {
168        if !names.insert(property.name.as_str()) {
169            output.push(issue(
170                DiagnosticCode::DuplicateMember,
171                DiagnosticSeverity::Error,
172                template,
173                Some(&property.name),
174                "duplicate property template name",
175            ));
176        }
177    }
178}
179
180pub(crate) fn issue(
181    code: DiagnosticCode,
182    severity: DiagnosticSeverity,
183    template: &str,
184    member: Option<&str>,
185    message: &str,
186) -> CatalogDiagnostic {
187    CatalogDiagnostic {
188        code,
189        severity,
190        template: template.to_owned(),
191        member: member.map(str::to_owned),
192        message: message.to_owned(),
193    }
194}