rust_texas/component/
hierarchy.rs

1use crate::prelude::*;
2
3/// \part{}: Only available for \documentclass{book}
4#[derive(Debug, Clone)]
5pub struct Part {
6    name: String,
7    components: Vec<Component>,
8}
9impl AsLatex for Part {
10    fn to_string(&self) -> String {
11        let comps = self
12            .components
13            .iter()
14            .map(|s| s.to_string())
15            .collect::<String>();
16        format!("\\part{{{}}} \n {} \n ", self.name, comps)
17    }
18}
19impl Populate for Part {
20    fn attach(&mut self, other: Component) -> TexResult<&mut Self> {
21        self.components.push(other);
22        Ok(self)
23    }
24    fn attach_vec(&mut self, other: Vec<Component>) -> TexResult<&mut Self> {
25        self.attach_iter(other.into_iter())
26    }
27
28    fn attach_iter<I: Iterator<Item = Component>>(&mut self, other: I) -> TexResult<&mut Self> {
29        self.components.extend(other);
30        Ok(self)
31    }
32}
33impl Part {
34    pub fn new(name: &str) -> Self {
35        Self {
36            name: escape(name, None),
37            components: vec![],
38        }
39    }
40
41    pub fn with_components(name: &str, components: Vec<Component>) -> Self {
42        Self {
43            name: escape(name, None),
44            components,
45        }
46    }
47}
48
49/// \chapter{}: Only available for \documentclass{book}
50#[derive(Debug, Clone)]
51pub struct Chapter {
52    name: String,
53    components: Vec<Component>,
54}
55impl AsLatex for Chapter {
56    fn to_string(&self) -> String {
57        let comps = self
58            .components
59            .iter()
60            .map(|s| s.to_string())
61            .collect::<String>();
62        format!("\\chapter{{{}}} \n {} \n ", self.name, comps)
63    }
64}
65impl Populate for Chapter {
66    fn attach(&mut self, other: Component) -> TexResult<&mut Self> {
67        self.components.push(other);
68        Ok(self)
69    }
70    fn attach_vec(&mut self, other: Vec<Component>) -> TexResult<&mut Self> {
71        self.attach_iter(other.into_iter())
72    }
73
74    fn attach_iter<I: Iterator<Item = Component>>(&mut self, other: I) -> TexResult<&mut Self> {
75        self.components.extend(other);
76        Ok(self)
77    }
78}
79impl Chapter {
80    pub fn new(name: &str) -> Self {
81        Self {
82            name: escape(name, None),
83            components: vec![],
84        }
85    }
86
87    pub fn with_components(name: &str, components: Vec<Component>) -> Self {
88        Self {
89            name: escape(name, None),
90            components,
91        }
92    }
93}
94
95/// \section{}: Major partitioning device within a document
96#[derive(Debug, Clone)]
97pub struct Section {
98    name: String,
99    components: Vec<Component>,
100}
101impl AsLatex for Section {
102    fn to_string(&self) -> String {
103        let comps = self
104            .components
105            .iter()
106            .map(|s| s.to_string())
107            .collect::<String>();
108        format!("\\section{{{}}} \n {} \n ", self.name, comps)
109    }
110}
111impl Populate for Section {
112    fn attach(&mut self, other: Component) -> TexResult<&mut Self> {
113        self.components.push(other);
114        Ok(self)
115    }
116    fn attach_vec(&mut self, other: Vec<Component>) -> TexResult<&mut Self> {
117        self.attach_iter(other.into_iter())
118    }
119
120    fn attach_iter<I: Iterator<Item = Component>>(&mut self, other: I) -> TexResult<&mut Self> {
121        self.components.extend(other);
122        Ok(self)
123    }
124}
125impl Section {
126    pub fn new(name: &str) -> Self {
127        // let name = name.to_string().replace("_", "\\_")
128
129        Self {
130            name: escape(name, None),
131            components: vec![],
132        }
133    }
134
135    pub fn with_components(name: &str, components: Vec<Component>) -> Self {
136        Self {
137            name: escape(name, None),
138            components,
139        }
140    }
141}
142
143/// \subsection{}
144#[derive(Debug, Clone)]
145pub struct Subsection {
146    name: String,
147    components: Vec<Component>,
148}
149impl AsLatex for Subsection {
150    fn to_string(&self) -> String {
151        let comps = self
152            .components
153            .iter()
154            .map(|s| s.to_string())
155            .collect::<String>();
156        format!("\\subsection{{{}}} \n {} \n ", self.name, comps)
157    }
158}
159impl Populate for Subsection {
160    fn attach(&mut self, other: Component) -> TexResult<&mut Self> {
161        self.components.push(other);
162        Ok(self)
163    }
164    fn attach_vec(&mut self, other: Vec<Component>) -> TexResult<&mut Self> {
165        self.attach_iter(other.into_iter())
166    }
167
168    fn attach_iter<I: Iterator<Item = Component>>(&mut self, other: I) -> TexResult<&mut Self> {
169        self.components.extend(other);
170        Ok(self)
171    }
172}
173impl Subsection {
174    pub fn new(name: &str) -> Self {
175        Self {
176            name: escape(name, None),
177            components: vec![],
178        }
179    }
180
181    pub fn with_components(name: &str, components: Vec<Component>) -> Self {
182        Self {
183            name: escape(name, None),
184            components,
185        }
186    }
187}
188
189/// Block of text bracketed by "\n\n". Generates a latex paragraph.
190#[derive(Debug, Clone)]
191pub struct Paragraph {
192    components: Vec<Component>,
193}
194impl AsLatex for Paragraph {
195    fn to_string(&self) -> String {
196        let comps = self
197            .components
198            .iter()
199            .map(|s| s.to_string())
200            .collect::<String>();
201        format!("\n\n {} \n\n ", comps)
202    }
203}
204impl Populate for Paragraph {
205    fn attach(&mut self, other: Component) -> TexResult<&mut Self> {
206        self.components.push(other);
207        Ok(self)
208    }
209    fn attach_vec(&mut self, other: Vec<Component>) -> TexResult<&mut Self> {
210        self.attach_iter(other.into_iter())
211    }
212
213    fn attach_iter<I: Iterator<Item = Component>>(&mut self, other: I) -> TexResult<&mut Self> {
214        self.components.extend(other);
215        Ok(self)
216    }
217}
218impl Paragraph {
219    pub fn new() -> Self {
220        Self { components: vec![] }
221    }
222
223    pub fn with_components(components: Vec<Component>) -> Self {
224        Self { components }
225    }
226}
227
228/// Terminated by "\\ \n", causes linebreaks within the document.
229#[derive(Debug, Clone)]
230pub struct Line {
231    components: Vec<Component>,
232}
233impl AsLatex for Line {
234    fn to_string(&self) -> String {
235        let comps = self
236            .components
237            .iter()
238            .map(|s| s.to_string())
239            .collect::<String>();
240        if comps.trim().is_empty() {
241            format!("\n")
242        } else {
243            format!("{} \\\\\n", comps)
244        }
245    }
246}
247impl Populate for Line {
248    fn attach(&mut self, other: Component) -> TexResult<&mut Self> {
249        self.components.push(other);
250        Ok(self)
251    }
252    fn attach_vec(&mut self, other: Vec<Component>) -> TexResult<&mut Self> {
253        self.attach_iter(other.into_iter())
254    }
255
256    fn attach_iter<I: Iterator<Item = Component>>(&mut self, other: I) -> TexResult<&mut Self> {
257        self.components.extend(other);
258        Ok(self)
259    }
260}
261impl Line {
262    pub fn new() -> Self {
263        Self { components: vec![] }
264    }
265
266    pub fn with_components(components: Vec<Component>) -> Self {
267        Self { components }
268    }
269}