tex_rs/traits/
attatch.rs

1use crate::error::TexError;
2use crate::Element;
3use crate::*;
4
5/// Used to attach elements to eachother
6pub trait Attach {
7    /// Takes an elemennt and pushes to &self.1
8    /// Returns Result with either `()` or rank error
9    fn attach(&mut self, element: Element) -> Result<(), TexError>;
10}
11
12impl Attach for Part {
13    fn attach(&mut self, element: Element) -> Result<(), TexError> {
14        if element.rank() > 0 {
15            return Ok(self.1.push(element));
16        }
17        return Err(TexError::RankError);
18    }
19}
20
21impl Attach for Chapter {
22    fn attach(&mut self, element: Element) -> Result<(), TexError> {
23        if element.rank() > 1 {
24            return Ok(self.1.push(element));
25        }
26        return Err(TexError::RankError);
27    }
28}
29
30impl Attach for Section {
31    fn attach(&mut self, element: Element) -> Result<(), TexError> {
32        if element.rank() > 2 {
33            return Ok(self.1.push(element));
34        }
35        return Err(TexError::RankError);
36    }
37}
38
39impl Attach for Paragraph {
40    fn attach(&mut self, element: Element) -> Result<(), TexError> {
41        if element.rank() > 3 {
42            return Ok(self.1.as_mut().unwrap().push(element));
43        }
44        return Err(TexError::RankError);
45    }
46}
47
48impl Attach for Environment {
49    fn attach(&mut self, element: Element) -> Result<(), TexError> {
50        if element.rank() > 7 {
51            return Ok(self.1.push(String::from(element)));
52        }
53        return Err(TexError::RankError);
54    }
55}