ifc_template_catalog/compliance/
apply.rs1use crate::definition::{
2 PropertyKind, PropertyTemplate, QuantityTemplate, SetTemplate, SetTemplateKind,
3};
4
5pub trait TemplateSink {
10 type Error;
12 fn begin(&mut self, template: &SetTemplate) -> Result<(), Self::Error>;
15 fn property(&mut self, path: &str, property: &PropertyTemplate) -> Result<(), Self::Error>;
18 fn quantity(&mut self, _quantity: &QuantityTemplate) -> Result<(), Self::Error> {
21 Ok(())
22 }
23 fn finish(&mut self, template: &SetTemplate) -> Result<(), Self::Error>;
25}
26
27pub fn apply_template<S: TemplateSink>(
31 template: &SetTemplate,
32 sink: &mut S,
33) -> Result<(), S::Error> {
34 sink.begin(template)?;
35 match &template.kind {
36 SetTemplateKind::Property { properties, .. } => {
37 walk_properties("", properties, sink)?;
38 }
39 SetTemplateKind::Quantity { quantities, .. } => {
40 for quantity in quantities {
41 sink.quantity(quantity)?;
42 }
43 }
44 }
45 sink.finish(template)
46}
47
48fn walk_properties<S: TemplateSink>(
49 parent: &str,
50 properties: &[PropertyTemplate],
51 sink: &mut S,
52) -> Result<(), S::Error> {
53 for property in properties {
54 let path = if parent.is_empty() {
55 property.name.clone()
56 } else {
57 format!("{parent}.{}", property.name)
58 };
59 sink.property(&path, property)?;
60 if let PropertyKind::Complex { properties, .. } = &property.kind {
61 walk_properties(&path, properties, sink)?;
62 }
63 }
64 Ok(())
65}