1mod schema;
4
5pub use schema::CatalogSchema;
6
7use std::collections::BTreeSet;
8
9use crate::catalog::Catalog;
10use crate::definition::{PropertyKind, PropertyTemplate, SetTemplateKind};
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14#[non_exhaustive]
15pub enum DiagnosticCode {
16 MissingPropertyDataType,
18 EmptyEnumeration,
20 EmptyApplicability,
22 DuplicateMember,
24 UnknownApplicableEntity,
26 UnknownPropertyDataType,
28 UnknownReferenceEntity,
30}
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
34#[non_exhaustive]
35pub enum DiagnosticSeverity {
36 Warning,
38 Error,
40}
41
42#[derive(Debug, Clone, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct CatalogDiagnostic {
46 pub code: DiagnosticCode,
48 pub severity: DiagnosticSeverity,
50 pub template: String,
52 pub member: Option<String>,
54 pub message: String,
56}
57
58impl Catalog {
59 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}