tex-rs 0.2.8

Library to create latex documents in Rust
Documentation
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use crate::Convert;
// Type aliases //
type Body = String;
type Meta = String;
type Pkg = String;
/// User-Defined Tuple for Body, Meta and Pkg levels
pub type UDTuple = (Body, Meta, Pkg);
/// Item for Environment and List
pub type Item = String;
// Structs //
/// Part struct, contains text and a vec for elements
#[derive(Debug, Clone)]
pub struct Part(pub String, pub Vec<Element>);
/// Chapter struct, contains text and a vec for elements
#[derive(Debug, Clone)]
pub struct Chapter(pub String, pub Vec<Element>);
/// Section struct, contains text and a vec for elements
#[derive(Debug, Clone)]
pub struct Section(pub String, pub Vec<Element>);
/// Paragraph struct, contains text and a vec for elements
#[derive(Debug, Clone)]
pub struct Paragraph(pub String, pub Option<Vec<Element>>);
/// Text struct with text and specifid text type
#[derive(Debug, Clone)]
pub struct Text(pub String, pub TextType);
/// Input struct with text for filename
#[derive(Debug, Clone)]
pub struct Input(pub String);
/// Environment struct with text for name, and vec for items (String)
#[derive(Debug, Clone)]
pub struct Environment(pub String, pub Vec<Item>);
/// User-defined command with string and level for it to be in
/// - Meta: In the metadata level, just before packages
/// - Package: In the level where packages are
/// - Body: Inside the document environment
#[derive(Debug, Clone)]
pub struct UserDefined(pub String, pub Level);
/// List struct with ListMode (enumerate or itemize) and vec for items (String)
#[derive(Debug, Clone)]
pub struct List(pub ListMode, pub Vec<Item>);

pub type Comment = UserDefined;

// Enums //

/// Elements enum that contains all elements
/// - Part
/// - Chapter
/// - Section (Headers)
/// - Paragraph
/// - Text (Italic, bold, verbatim, etc.)
/// - Input
/// - Environment `(\begin{}...\end{})`
/// - UserDefiend (any kind of custom code)
/// - List (enumerate or itemize)
#[derive(Debug, Clone)]
pub enum Element {
    Part(Part),
    Chapter(Chapter),
    Section(Section),
    Paragraph(Paragraph),
    Text(Text),
    Input(Input),
    Environment(Environment),
    UserDefined(UserDefined),
    List(List),
}

/// TextType enum that contains the different kind
/// of text like;
/// - Normal `(\par ...)`
/// - Bold `(\textbf{...})`
/// - Italic `(\textit{...})`
/// - Underline `(\underline{...})`
/// - Verbatim `(\verb!...!)`
/// - Roman `(\textrm{...})`
#[derive(Debug, Clone)]
pub enum TextType {
    Normal,
    Bold,
    Italic,
    Underline,
    Verbatim,
    Roman,
}
/// ListMode enum that contains the two different lists
/// - Itemize `(\begin{itemize}...\end{itemize})`
/// - Enumerate `(\begin{enumerate}...\end{enumerate})`
#[derive(Debug, Clone)]
pub enum ListMode {
    Itemize,
    Enumerate,
}
/// Level determines where the userdefined command goes
/// - Meta: Where metadata goes
/// - Package: Where packages goes
/// - Body: Inside the document environment
#[derive(Debug, Clone)]
pub enum Level {
    Meta,
    Body,
    Package,
}

// Implementations //
impl Element {
    /// Determines Prioirty rank
    pub fn rank(&self) -> u8 {
        match self {
            Element::Part(_) => 0,
            Element::Chapter(_) => 1,
            Element::Section(_) => 2,
            Element::Paragraph(_) => 3,
            Element::UserDefined(_) => 4,
            Element::Text(_) => 8,
            Element::Input(_) => 7,
            Element::Environment(_) => 6,
            Element::List(_) => 5,
        }
    }
    /// Gets the Vec element inside the struct
    pub fn get_vec(&self) -> Option<Vec<Element>> {
        match &self {
            Element::Part(p) => Some(p.to_owned().1),
            Element::Chapter(c) => Some(c.to_owned().1),
            Element::Section(s) => Some(s.to_owned().1),
            Element::Paragraph(p) => Some(p.to_owned().1.unwrap()),
            Element::Text(_) => None,
            Element::Input(_) => None,
            Element::Environment(_) => None,
            Element::UserDefined(_) => None,
            Element::List(_) => None,
        }
    }
    /// Recursive function to get latex string for all elements inside struct
    pub fn loop_through(&self) -> String {
        let vec = match self.get_vec() {
            Some(a) => a,
            None => return "".to_string(),
        };
        let mut s = Vec::new();
        if vec.is_empty() {
            return "".to_string();
        } else {
            for i in &vec {
                s.push(i.to_latex_string());
                s.push(i.loop_through())
            }
        }
        s.join(" ")
    }
    /// Parallel version of loop_through using rayon
    pub fn loop_through_parallel(&self) -> String {
        let vec = match self.get_vec() {
            Some(a) => a,
            None => return "".to_string(),
        };
        let mut s = Vec::new();
        if vec.is_empty() {
            return "".to_string();
        } else {
            for i in &vec {
                let r = rayon::join(|| i.clone().to_latex_string(), || i.clone().loop_through());
                s.push(r.0);
                s.push(r.1);
            }
        }
        s.join(" ")
    }
}

impl Part {
    pub fn new(text: &str) -> Self {
        Self(text.to_string(), Vec::new())
    }
}
impl Chapter {
    pub fn new(text: &str) -> Self {
        Self(text.to_string(), Vec::new())
    }
}
impl Section {
    pub fn new(text: &str) -> Self {
        Self(text.to_string(), Vec::new())
    }
}
impl Paragraph {
    pub fn new(text: &str) -> Self {
        Self(text.to_string(), Some(Vec::new()))
    }
}
impl Text {
    pub fn new(text: &str, text_type: TextType) -> Self {
        Self(text.to_string(), text_type)
    }
}
impl Input {
    pub fn new(text: &str) -> Self {
        Self(text.to_string())
    }
}

impl Environment {
    pub fn new(text: &str) -> Self {
        Self(text.to_string(), Vec::new())
    }
    /// Attach string to environment, alternative to attach which requires an Element
    pub fn attach_string(&mut self, item: Item) {
        self.1.push(item);
    }
}

impl List {
    pub fn new(list_mode: ListMode, items: &Vec<Item>) -> Self {
        Self(list_mode, items.to_owned())
    }
}

impl UserDefined {
    pub fn new(text: &str, level: Level) -> Self {
        Self(text.to_string(), level)
    }
    /// Evaluates a userdefined, and puts it's string in the appropriate level
    pub fn evaluate(&self) -> UDTuple {
        match &self.1 {
            Level::Body => (self.0.clone(), "".to_owned(), "".to_owned()),
            Level::Meta => ("".to_owned(), self.0.clone(), "".to_owned()),
            Level::Package => ("".to_owned(), "".to_owned(), self.0.clone()),
        }
    }
    pub fn new_comment(text: &str, level: Level) -> Self {
        Self(format!("% {}", text), level)
    }
}

// Macros
#[macro_export]
/// Creates a vector of elements instead of creating `vec![Element::from(foo)]`
macro_rules! elements {
   ($($x:expr),+) => ({
       let mut v = Vec::new();
       $( v.push(Element::from($x)); )+
       v
   });
}

// Trait Implementations //
impl Convert for Part {
    fn to_latex_string(&self) -> String {
        format!("\\part{{{}}}", &self.0)
    }
    /*fn to_matex_string(&self) -> String {
        format!("part: {}", &self.0)
    } */
}

impl Convert for Chapter {
    fn to_latex_string(&self) -> String {
        format!("\\chapter{{{}}}", &self.0)
    }
    /* fn to_matex_string(&self) -> String {
        format!("chapter: {}", &self.0)
    } */
}

impl Convert for Section {
    fn to_latex_string(&self) -> String {
        format!("\\section{{{}}}", &self.0)
    }
    /* fn to_matex_string(&self) -> String {
        format!("section: {}", &self.0)
    } */
}

impl Convert for Paragraph {
    fn to_latex_string(&self) -> String {
        format!("\\paragraph{{{}}}", &self.0)
    }
    /* fn to_matex_string(&self) -> String {
        format!("paragraph: {}", &self.0)
    } */
}

impl Convert for Text {
    fn to_latex_string(&self) -> String {
        let text: String;
        match &self.1 {
            TextType::Normal => text = self.0.clone(),
            TextType::Bold => {
                text = format!("\\textbf{{{}}}", &self.0);
            }
            TextType::Italic => {
                text = format!("\\textit{{{}}}", &self.0);
            }
            TextType::Roman => {
                text = format!("\\textrm{{{}}}", &self.0);
            }
            TextType::Underline => {
                text = format!("\\underline{{{}}}", &self.0);
            }
            TextType::Verbatim => {
                text = format!("\\verb!{}!", &self.0);
            }
        }
        format!("{}", &text)
    }
    /* fn to_matex_string(&self) -> String {
        match &self.1{
            TextType::Normal => {format!("par: {}", &self.0)}
            TextType::Bold => {format!("textbf: {}", &self.0)}
            TextType::Italic => {format!("textit: {}", &self.0)}
            TextType::Underline => {format!("underline: {}", &self.0)}
            TextType::Verbatim => {format!("verb: {}", &self.0)}
            TextType::Roman => {format!("textrm: {}", &self.0)}
        }
    } */
}

impl Convert for Input {
    fn to_latex_string(&self) -> String {
        format!("\\input{{{}}}", &self.0)
    }
    /* fn to_matex_string(&self) -> String {
        format!("import: {}", &self.0)
    } */
}

impl Convert for Environment {
    fn to_latex_string(&self) -> String {
        let begin = format!("\\begin{{{}}}", &self.0);
        let end = format!("\\end{{{}}}", &self.0);
        let mut s = Vec::new();
        s.push(begin);
        for i in &self.1 {
            s.push(i.to_owned());
        }
        s.push(end);
        s.join("\n")
    }
    /* fn to_matex_string(&self) -> String {
        let begin = format!("{} > begin", &self.0);
        let end = format!("{} > end", &self.0);
        let mut s = Vec::new();
        s.push(begin);
        for i in &self.1{
            s.push(i.to_owned());
        }
        s.push(end);
        s.join("\n")
    } */
}

impl Convert for UserDefined {
    fn to_latex_string(&self) -> String {
        self.0.clone()
    }
    /* fn to_matex_string(&self) -> String {
        self.0.clone()
    } */
}

impl Convert for List {
    fn to_latex_string(&self) -> String {
        let mode: &str = match &self.0 {
            ListMode::Enumerate => "enumerate",
            ListMode::Itemize => "itemize",
        };

        let (begin, end) = (format!("\\begin{{{}}}", mode), format!("\\end{{{}}}", mode));
        let mut s = Vec::new();
        s.push(begin);
        for i in &self.1 {
            s.push(format!("\\item {}", &i));
        }
        s.push(end);
        s.join("\n")
    }
}

impl Convert for Element {
    fn to_latex_string(&self) -> String {
        match self {
            Element::Part(e) => e.to_latex_string(),
            Element::Chapter(e) => e.to_latex_string(),
            Element::Section(e) => e.to_latex_string(),
            Element::Paragraph(e) => e.to_latex_string(),
            Element::Text(e) => e.to_latex_string(),
            Element::Input(e) => e.to_latex_string(),
            Element::Environment(e) => e.to_latex_string(),
            Element::UserDefined(e) => e.to_latex_string(),
            Element::List(e) => e.to_latex_string(),
        }
    }
}

impl From<Part> for Element {
    fn from(p: Part) -> Self {
        Element::Part(p)
    }
}

impl From<Chapter> for Element {
    fn from(c: Chapter) -> Self {
        Element::Chapter(c)
    }
}

impl From<Section> for Element {
    fn from(s: Section) -> Self {
        Element::Section(s)
    }
}

impl From<Paragraph> for Element {
    fn from(p: Paragraph) -> Self {
        Element::Paragraph(p)
    }
}

impl From<Text> for Element {
    fn from(t: Text) -> Self {
        Element::Text(t)
    }
}

impl From<Input> for Element {
    fn from(i: Input) -> Self {
        Element::Input(i)
    }
}

impl From<Environment> for Element {
    fn from(e: Environment) -> Self {
        Element::Environment(e)
    }
}

impl From<UserDefined> for Element {
    fn from(u: UserDefined) -> Self {
        Element::UserDefined(u)
    }
}

impl From<List> for Element {
    fn from(l: List) -> Self {
        Element::List(l)
    }
}

impl From<Element> for String {
    fn from(e: Element) -> Self {
        e.to_latex_string()
    }
}