microcad_lang/model/
builder.rs1use crate::model::*;
7
8use microcad_lang_base::SrcRef;
9
10#[derive(Default)]
12pub struct ModelBuilder {
13 root: ModelInner,
14 pub properties: Properties,
16 pub children: Models,
18}
19
20impl ModelBuilder {
24 pub fn new(element: Element, src_ref: SrcRef) -> Self {
26 Self {
27 root: ModelInner::new(element, src_ref),
28 ..Default::default()
29 }
30 }
31
32 pub fn add_children(mut self, mut children: Models) -> Self {
34 self.children.append(&mut children);
35 self
36 }
37
38 pub fn attributes(mut self, attributes: Attributes) -> Self {
40 self.root.attributes = attributes;
41 self
42 }
43
44 pub fn properties(mut self, properties: Properties) -> Self {
46 self.properties = properties;
47 self
48 }
49
50 pub fn build(mut self) -> Model {
52 if let Element::Workpiece(workpiece) = &mut self.root.element.value {
53 workpiece.add_properties(self.properties);
54 }
55
56 let model = Model::new(self.root.into());
57 model.append_children(self.children);
58 model.deduce_output_type();
59 model
60 }
61}
62
63impl std::fmt::Display for ModelBuilder {
64 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65 write!(f, "{}", self.properties)
66 }
67}