microcad_lang/model/
models.rs1use crate::model::*;
7use derive_more::{Deref, DerefMut};
8use microcad_core::BooleanOp;
9
10#[derive(Debug, Default, Clone, PartialEq, Deref, DerefMut)]
12pub struct Models(Vec<Model>);
13
14impl Models {
15 pub fn single_model(&self) -> Option<Model> {
17 match self.0.len() {
18 1 => self.0.first().cloned(),
19 _ => None,
20 }
21 }
22
23 pub fn to_multiplicity(&self, src_ref: SrcRef) -> Model {
25 match self.single_model() {
26 Some(model) => model,
27 None => ModelBuilder::new(Element::Multiplicity, src_ref)
28 .add_children(self.clone())
29 .build(),
30 }
31 }
32
33 pub fn union(&self) -> Model {
35 self.boolean_op(microcad_core::BooleanOp::Union)
36 }
37
38 pub fn boolean_op(&self, op: BooleanOp) -> Model {
40 match self.single_model() {
41 Some(model) => model,
42 None => ModelBuilder::new(Element::BuiltinWorkpiece(op.into()), SrcRef::none())
43 .add_children(
44 [ModelBuilder::new(Element::Group, SrcRef::none())
45 .add_children(self.clone())
46 .build()]
47 .into_iter()
48 .collect(),
49 )
50 .build(),
51 }
52 }
53
54 pub fn filter_by_source_hash(&self, source_hash: u64) -> Models {
56 self.iter()
57 .filter(|model| source_hash == model.source_hash())
58 .cloned()
59 .collect()
60 }
61
62 pub fn deduce_output_type(&self) -> OutputType {
64 self.iter().map(|model| model.deduce_output_type()).fold(
65 OutputType::NotDetermined,
66 |result_output_type, model_output_type| result_output_type.merge(&model_output_type),
67 )
68 }
69}
70
71impl From<Vec<Model>> for Models {
72 fn from(value: Vec<Model>) -> Self {
73 Self(value)
74 }
75}
76
77impl From<Option<Model>> for Models {
78 fn from(value: Option<Model>) -> Self {
79 Self(value.into_iter().collect())
80 }
81}
82
83impl std::fmt::Display for Models {
84 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85 self.iter().try_for_each(|model| model.fmt(f))
86 }
87}
88
89impl FromIterator<Model> for Models {
90 fn from_iter<T: IntoIterator<Item = Model>>(iter: T) -> Self {
91 Self(iter.into_iter().collect())
92 }
93}
94
95impl TreeDisplay for Models {
96 fn tree_print(&self, f: &mut std::fmt::Formatter, depth: TreeState) -> std::fmt::Result {
97 self.iter().try_for_each(|child| child.tree_print(f, depth))
98 }
99}