solidrs/
modify.rs

1use crate::{
2    element::InnerElement,
3    var::{Arg, Val},
4    Element,
5};
6
7impl Element {
8    #[must_use]
9    /// # Panics
10    //  panics for types that don't allow centering
11    pub fn center(&self) -> Self {
12        let inner = match &self.0 {
13            InnerElement::Cube { x, y, z, .. } => InnerElement::Cube {
14                x: x.clone(),
15                y: y.clone(),
16                z: z.clone(),
17                centered: true,
18            },
19            InnerElement::Cylinder { h, r, .. } => InnerElement::Cylinder {
20                h: h.clone(),
21                r: r.clone(),
22                centered: true,
23            },
24            // ToDo fix this with types to not allow invalid calls
25            other => panic!("center() is not allowed for type {other}"),
26        };
27        Element(inner)
28    }
29
30    #[must_use]
31    pub fn margin(&self, margin: impl Arg) -> Self {
32        Element(self.0.clone().margin(margin.val()))
33    }
34}
35
36impl InnerElement {
37    #[must_use]
38    fn margin(self, margin: Val) -> Self {
39        match self {
40            InnerElement::Cube { x, y, z, centered } => margin_cube(x, y, z, centered, margin),
41            Self::Translate { x, y, z, child } => Self::Translate {
42                x,
43                y,
44                z,
45                child: Box::new(child.margin(margin)),
46            },
47
48            // ToDo fix this with types to not allow invalid calls
49            other => panic!("margin() is not allowed for type {other}"),
50        }
51    }
52}
53
54fn margin_cube(x: Val, y: Val, z: Val, centered: bool, margin: Val) -> InnerElement {
55    let cube = InnerElement::Cube {
56        x: Val::Calc(x + margin.clone() * 2),
57        y: Val::Calc(y + margin.clone() * 2),
58        z: Val::Calc(z + margin.clone() * 2),
59        centered,
60    };
61    if centered {
62        cube
63    } else {
64        InnerElement::Translate {
65            x: Val::Calc(-margin.clone()),
66            y: Val::Calc(-margin.clone()),
67            z: Val::Calc(-margin),
68            child: Box::new(cube),
69        }
70    }
71}