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