1use ifc_model::Model;
18use ifc_schema::{for_version, Schema, SchemaVersion};
19
20use crate::error::SchemaResolutionError;
21
22#[derive(Clone, Copy)]
28pub(crate) struct Release {
29 pub(crate) version: SchemaVersion,
30 pub(crate) schema: &'static Schema,
31}
32
33impl Release {
34 pub(crate) fn is_a(self, candidate: &str, ancestor: &str) -> bool {
37 self.schema.is_a(candidate, ancestor)
38 }
39
40 pub(crate) fn slot(self, entity: &str, attribute: &str) -> Option<usize> {
47 self.schema
48 .attribute_names(entity)
49 .iter()
50 .position(|name| name.eq_ignore_ascii_case(attribute))
51 }
52
53 fn bind(version: SchemaVersion) -> Result<Self, SchemaResolutionError> {
54 let schema =
55 for_version(version).map_err(|_| SchemaResolutionError::UnsupportedSchema {
56 schema: version.release_id().to_owned(),
57 })?;
58 Ok(Self { version, schema })
59 }
60}
61
62const VERIFIED: &[SchemaVersion] = &[
70 SchemaVersion::Ifc2x3,
71 SchemaVersion::Ifc4,
72 SchemaVersion::Ifc4x3,
73];
74
75pub fn schema_of(model: &Model) -> Result<SchemaVersion, SchemaResolutionError> {
90 resolve(model).map(|release| release.version)
91}
92
93pub(crate) fn resolve(model: &Model) -> Result<Release, SchemaResolutionError> {
95 match model.header().schema.as_slice() {
96 [] => Err(SchemaResolutionError::MissingSchema),
97 [token] => match SchemaVersion::from_header_token(token) {
98 Some(version) if VERIFIED.contains(&version) => Release::bind(version),
99 _ => Err(SchemaResolutionError::UnsupportedSchema {
100 schema: token.clone(),
101 }),
102 },
103 schemas => Err(SchemaResolutionError::MultipleSchemas {
104 schemas: schemas.len(),
105 }),
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
117 fn slots_hold_in_every_verified_release() {
118 use crate::connectivity::relation::slot as connects;
119 use crate::port::definition::slot as port;
120 use crate::system::group::slot as group;
121 use crate::system::services::slot as services;
122 use crate::zone::spatial_group::slot as spatial;
123 let pinned = [
124 (
125 "IFCRELASSIGNSTOGROUP",
126 "RelatedObjects",
127 group::ASSIGNS_RELATED,
128 ),
129 (
130 "IFCRELASSIGNSTOGROUP",
131 "RelatingGroup",
132 group::ASSIGNS_GROUP,
133 ),
134 ("IFCRELCONNECTSPORTS", "RelatingPort", connects::RELATING),
135 ("IFCRELCONNECTSPORTS", "RelatedPort", connects::RELATED),
136 (
137 "IFCRELCONNECTSPORTS",
138 "RealizingElement",
139 connects::REALIZING,
140 ),
141 ("IFCRELNESTS", "RelatingObject", port::NESTS_PARENT),
142 ("IFCRELNESTS", "RelatedObjects", port::NESTS_CHILDREN),
143 (
144 "IFCRELCONNECTSPORTTOELEMENT",
145 "RelatingPort",
146 port::PORT_TO_ELEMENT_PORT,
147 ),
148 (
149 "IFCRELCONNECTSPORTTOELEMENT",
150 "RelatedElement",
151 port::PORT_TO_ELEMENT_ELEMENT,
152 ),
153 ("IFCDISTRIBUTIONPORT", "FlowDirection", port::FLOW_DIRECTION),
154 (
155 "IFCRELCONTAINEDINSPATIALSTRUCTURE",
156 "RelatedElements",
157 spatial::RELATED_ELEMENTS,
158 ),
159 (
160 "IFCRELCONTAINEDINSPATIALSTRUCTURE",
161 "RelatingStructure",
162 spatial::RELATING_STRUCTURE,
163 ),
164 (
165 "IFCRELREFERENCEDINSPATIALSTRUCTURE",
166 "RelatedElements",
167 spatial::RELATED_ELEMENTS,
168 ),
169 (
170 "IFCRELREFERENCEDINSPATIALSTRUCTURE",
171 "RelatingStructure",
172 spatial::RELATING_STRUCTURE,
173 ),
174 (
175 "IFCRELSERVICESBUILDINGS",
176 "RelatingSystem",
177 services::RELATING_SYSTEM,
178 ),
179 (
180 "IFCRELSERVICESBUILDINGS",
181 "RelatedBuildings",
182 services::RELATED_BUILDINGS,
183 ),
184 ("IFCSYSTEM", "Name", group::NAME),
185 ("IFCDISTRIBUTIONPORT", "Name", port::NAME),
186 ];
187 let mut checked = 0;
188 for &version in VERIFIED {
189 let release = Release::bind(version).expect("verified releases are bundled");
190 for (entity, attribute, slot) in pinned {
191 assert_eq!(
192 release.slot(entity, attribute),
193 Some(slot),
194 "{version:?} {entity}.{attribute}"
195 );
196 checked += 1;
197 }
198 }
199 assert_eq!(checked, 3 * pinned.len());
200 }
201
202 #[test]
203 fn unbound_and_unverified_headers_are_refused() {
204 let mut model = Model::new();
205 assert!(matches!(
206 resolve(&model),
207 Err(SchemaResolutionError::MissingSchema)
208 ));
209 for token in ["IFC4X1", "IFC4X2", "IFC9"] {
210 model.header_mut().schema = vec![token.to_owned()];
211 assert!(
212 matches!(resolve(&model), Err(SchemaResolutionError::UnsupportedSchema { ref schema }) if schema == token),
213 "{token}"
214 );
215 }
216 model.header_mut().schema = vec!["IFC4".into(), "IFC2X3".into()];
217 assert!(matches!(
218 resolve(&model),
219 Err(SchemaResolutionError::MultipleSchemas { schemas: 2 })
220 ));
221 model.header_mut().schema = vec!["IFC4X3_ADD2".into()];
222 assert_eq!(schema_of(&model), Ok(SchemaVersion::Ifc4x3));
223 }
224}