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
use crate::error::TexError;
use crate::Element;
use crate::*;

/// Used to attach elements to eachother
pub trait Attach {
    /// Takes an elemennt and pushes to &self.1
    /// Returns Result with either `()` or rank error
    fn attach(&mut self, element: Element) -> Result<(), TexError>;
}

impl Attach for Part {
    fn attach(&mut self, element: Element) -> Result<(), TexError> {
        if element.rank() > 0 {
            return Ok(self.1.push(element));
        }
        return Err(TexError::RankError);
    }
}

impl Attach for Chapter {
    fn attach(&mut self, element: Element) -> Result<(), TexError> {
        if element.rank() > 1 {
            return Ok(self.1.push(element));
        }
        return Err(TexError::RankError);
    }
}

impl Attach for Section {
    fn attach(&mut self, element: Element) -> Result<(), TexError> {
        if element.rank() > 2 {
            return Ok(self.1.push(element));
        }
        return Err(TexError::RankError);
    }
}

impl Attach for Paragraph {
    fn attach(&mut self, element: Element) -> Result<(), TexError> {
        if element.rank() > 3 {
            return Ok(self.1.as_mut().unwrap().push(element));
        }
        return Err(TexError::RankError);
    }
}

impl Attach for Environment {
    fn attach(&mut self, element: Element) -> Result<(), TexError> {
        if element.rank() > 7 {
            return Ok(self.1.push(String::from(element)));
        }
        return Err(TexError::RankError);
    }
}