rust_texas/component/
envs.rs

1use std::fmt::Display;
2
3use crate::prelude::*;
4
5/// OG environment, with \begin{} ... \end{}. For lists, please use `List`.
6/// Halfway through implementing arguments, stay tuned.
7#[derive(Debug, Clone)]
8pub struct Environment {
9    name: String,
10    components: Vec<Component>,
11    opt: Vec<String>,
12}
13impl AsLatex for Environment {
14    fn to_string(&self) -> String {
15        let comps = self
16            .components
17            .iter()
18            .map(|s| s.to_string())
19            .collect::<String>();
20        let opts = self.opt.join(", ");
21        format!(
22            "\\begin{{{}}}[{}] \n {} \n \\end{{{}}} \n ",
23            self.name, opts, comps, self.name
24        )
25    }
26}
27impl Populate for Environment {
28    fn attach(&mut self, other: Component) -> TexResult<&mut Self> {
29        self.components.push(other);
30        Ok(self)
31    }
32    fn attach_vec(&mut self, other: Vec<Component>) -> TexResult<&mut Self> {
33        self.attach_iter(other.into_iter())
34    }
35
36    fn attach_iter<I: Iterator<Item = Component>>(&mut self, other: I) -> TexResult<&mut Self> {
37        self.components.extend(other);
38        Ok(self)
39    }
40}
41impl Opt for Environment {
42    fn add_option(&mut self, opt: &str) {
43        self.opt.push(opt.to_string());
44    }
45}
46impl Environment {
47    pub fn new(name: &str) -> Self {
48        Self {
49            name: name.to_string(),
50            components: vec![],
51            opt: vec![],
52        }
53    }
54}
55
56/// OG List, itemize or enumerate. If y'all want description, please put up an issue.
57#[derive(Debug, Clone)]
58pub struct List {
59    items: Vec<Component>,
60    typ: ListType,
61    opt: Vec<String>,
62}
63impl AsLatex for List {
64    fn to_string(&self) -> String {
65        let comps = self
66            .items
67            .iter()
68            .map(|s| format!("\t\\item {}\n", s.to_string()))
69            .collect::<String>();
70        let opts = self.opt.join(", ");
71        format!(
72            "\\begin{{{}}}[{}] \n {} \n \\end{{{}}} \n ",
73            self.typ.to_string(),
74            opts,
75            comps,
76            self.typ.to_string()
77        )
78    }
79}
80impl Populate for List {
81    fn attach(&mut self, other: Component) -> TexResult<&mut Self> {
82        self.items.push(other);
83        Ok(self)
84    }
85    fn attach_vec(&mut self, other: Vec<Component>) -> TexResult<&mut Self> {
86        self.attach_iter(other.into_iter())
87    }
88
89    fn attach_iter<I: Iterator<Item = Component>>(&mut self, other: I) -> TexResult<&mut Self> {
90        self.items.extend(other);
91        Ok(self)
92    }
93}
94impl Opt for List {
95    fn add_option(&mut self, opt: &str) {
96        self.opt.push(opt.to_string());
97    }
98}
99impl List {
100    pub fn new(typ: ListType) -> Self {
101        Self {
102            items: vec![],
103            typ,
104            opt: vec![],
105        }
106    }
107
108    pub fn with_items(typ: ListType, items: Vec<Component>) -> Self {
109        Self {
110            items,
111            typ,
112            opt: vec![],
113        }
114    }
115}
116
117/// Variants for itemize and enumerate.
118#[derive(Debug, Clone)]
119pub enum ListType {
120    Itemize,
121    Enumerate,
122}
123impl Display for ListType {
124    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125        write!(
126            f,
127            "{}",
128            match &self {
129                Self::Itemize => "itemize",
130                Self::Enumerate => "enumerate",
131            }
132        )?;
133
134        Ok(())
135    }
136}
137
138#[derive(Debug, Clone)]
139pub struct Figure {
140    img: Image,
141    caption: String,
142    opt: Vec<String>,
143}
144
145impl Figure {
146    pub fn new(img: &str, caption: String) -> Self {
147        Self {
148            img: Image::new(img),
149            caption,
150            opt: vec![],
151        }
152    }
153    pub fn from_img(img: Image, caption: String) -> Self {
154        Self {
155            img,
156            caption,
157            opt: vec![],
158        }
159    }
160}
161
162impl AsLatex for Figure {
163    fn to_string(&self) -> String {
164        format!(
165            "\\begin{{figure}}[{}] \n \\centering \n {} \n \\caption{{{}}} \n \\end{{figure}} ",
166            self.opt.join(", "),
167            self.img.to_string(),
168            self.caption
169        )
170    }
171}
172
173impl Opt for Figure {
174    fn add_option(&mut self, opt: &str) {
175        self.opt.push(opt.to_string());
176    }
177}