tabled/features/style/
line.rs

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
/// The structure represent a vertical or horizontal line.
#[derive(Debug, Default, Clone, Copy)]
pub struct Line {
    pub(crate) main: Option<char>,
    pub(crate) intersection: Option<char>,
    pub(crate) connector1: Option<char>,
    pub(crate) connector2: Option<char>,
}

impl Line {
    /// Creates a new [`Line`] object.
    pub const fn new(
        main: Option<char>,
        intersection: Option<char>,
        connector1: Option<char>,
        connector2: Option<char>,
    ) -> Self {
        Self {
            main,
            intersection,
            connector1,
            connector2,
        }
    }

    /// Creates a new [`Line`] object with all chars set.
    pub const fn full(main: char, intersection: char, connector1: char, connector2: char) -> Self {
        Self::new(
            Some(main),
            Some(intersection),
            Some(connector1),
            Some(connector2),
        )
    }

    /// Creates a new [`Line`] object with all chars set to the provided one.
    pub const fn filled(c: char) -> Self {
        Self::full(c, c, c, c)
    }

    /// Creates a new [`Line`] object with all chars not set.
    pub const fn empty() -> Self {
        Self::new(None, None, None, None)
    }

    /// Checks if the line has nothing set.
    pub const fn is_empty(&self) -> bool {
        self.main.is_none()
            && self.intersection.is_none()
            && self.connector1.is_none()
            && self.connector2.is_none()
    }
}

impl From<Line> for papergrid::HorizontalLine<char> {
    fn from(l: Line) -> Self {
        Self {
            main: l.main,
            intersection: l.intersection,
            left: l.connector1,
            right: l.connector2,
        }
    }
}

impl From<Line> for papergrid::VerticalLine<char> {
    fn from(l: Line) -> Self {
        Self {
            main: l.main,
            intersection: l.intersection,
            top: l.connector1,
            bottom: l.connector2,
        }
    }
}