codeviz_rust/
struct_spec.rs

1use super::*;
2
3#[derive(Debug, Clone)]
4pub struct StructSpec {
5    pub name: String,
6    pub attributes: Elements,
7    pub elements: Elements,
8    pub public: bool,
9}
10
11impl StructSpec {
12    pub fn new(name: &str) -> StructSpec {
13        StructSpec {
14            name: name.to_owned(),
15            attributes: Elements::new(),
16            elements: Elements::new(),
17            public: false,
18        }
19    }
20
21    pub fn public(&mut self) {
22        self.public = true;
23    }
24
25    pub fn push_attribute<D>(&mut self, attribute: D)
26    where
27        D: Into<Element>,
28    {
29        self.attributes.push(attribute.into());
30    }
31
32    pub fn push<E>(&mut self, element: E)
33    where
34        E: Into<Element>,
35    {
36        self.elements.push(element);
37    }
38}
39
40impl From<StructSpec> for Element {
41    fn from(value: StructSpec) -> Element {
42        let mut out = Elements::new();
43
44        out.push(value.attributes);
45
46        let mut decl = Statement::new();
47
48        if value.public {
49            decl.push("pub ");
50        }
51
52        decl.push("struct ");
53        decl.push(value.name);
54        decl.push(" {");
55
56        out.push(decl);
57
58        if !value.elements.is_empty() {
59            out.push_nested(value.elements.join(Spacing));
60        }
61
62        out.push("}");
63
64        out.into()
65    }
66}