summaryrefslogtreecommitdiff
path: root/src/geom/gen.rs
blob: c530ff2b141b7f430e074ef40d22cfdd934b5b1a (plain) (blame)
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
use super::*;

/// A container with a main and cross component.
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Gen<T> {
    /// The cross component.
    pub cross: T,
    /// The main component.
    pub main: T,
}

impl<T> Gen<T> {
    /// Create a new instance from the two components.
    pub fn new(cross: T, main: T) -> Self {
        Self { cross, main }
    }

    /// Create a new instance with two equal components.
    pub fn splat(value: T) -> Self
    where
        T: Clone,
    {
        Self { cross: value.clone(), main: value }
    }
}

impl Gen<Length> {
    /// The zero value.
    pub fn zero() -> Self {
        Self {
            main: Length::zero(),
            cross: Length::zero(),
        }
    }
}

impl<T> Get<GenAxis> for Gen<T> {
    type Component = T;

    fn get(self, axis: GenAxis) -> T {
        match axis {
            GenAxis::Main => self.main,
            GenAxis::Cross => self.cross,
        }
    }

    fn get_mut(&mut self, axis: GenAxis) -> &mut T {
        match axis {
            GenAxis::Main => &mut self.main,
            GenAxis::Cross => &mut self.cross,
        }
    }
}

impl<T> Switch for Gen<T> {
    type Other = Spec<T>;

    fn switch(self, main: SpecAxis) -> Self::Other {
        match main {
            SpecAxis::Horizontal => Spec::new(self.main, self.cross),
            SpecAxis::Vertical => Spec::new(self.cross, self.main),
        }
    }
}

impl<T: Debug> Debug for Gen<T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "Gen({:?}, {:?})", self.main, self.cross)
    }
}

/// The two generic layouting axes.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum GenAxis {
    /// The axis pages and paragraphs are set along.
    Main,
    /// The axis words and lines are set along.
    Cross,
}

impl GenAxis {
    /// The other axis.
    pub fn other(self) -> Self {
        match self {
            Self::Main => Self::Cross,
            Self::Cross => Self::Main,
        }
    }
}

impl Switch for GenAxis {
    type Other = SpecAxis;

    fn switch(self, main: SpecAxis) -> Self::Other {
        match self {
            Self::Main => main,
            Self::Cross => main.other(),
        }
    }
}

impl Display for GenAxis {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.pad(match self {
            Self::Main => "main",
            Self::Cross => "cross",
        })
    }
}