summaryrefslogtreecommitdiff
path: root/src/geom/gen.rs
blob: ff8c881aaaaeb75e388bdcd4b426319d834f7d3a (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
110
111
112
113
114
115
116
117
use super::*;

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

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

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

    /// Maps the individual fields with `f`.
    pub fn map<F, U>(self, mut f: F) -> Gen<U>
    where
        F: FnMut(T) -> U,
    {
        Gen {
            inline: f(self.inline),
            block: f(self.block),
        }
    }

    /// Convert to the specific representation, given the current block axis.
    pub fn to_spec(self, block: SpecAxis) -> Spec<T> {
        match block {
            SpecAxis::Horizontal => Spec::new(self.block, self.inline),
            SpecAxis::Vertical => Spec::new(self.inline, self.block),
        }
    }
}

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

    /// Convert to a point.
    pub fn to_point(self, block: SpecAxis) -> Point {
        self.to_spec(block).to_point()
    }

    /// Convert to a size.
    pub fn to_size(self, block: SpecAxis) -> Size {
        self.to_spec(block).to_size()
    }
}

impl<T> Gen<Option<T>> {
    /// Unwrap the individual fields.
    pub fn unwrap_or(self, other: Gen<T>) -> Gen<T> {
        Gen {
            inline: self.inline.unwrap_or(other.inline),
            block: self.block.unwrap_or(other.block),
        }
    }
}

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

    fn get(self, axis: GenAxis) -> T {
        match axis {
            GenAxis::Inline => self.inline,
            GenAxis::Block => self.block,
        }
    }

    fn get_mut(&mut self, axis: GenAxis) -> &mut T {
        match axis {
            GenAxis::Inline => &mut self.inline,
            GenAxis::Block => &mut self.block,
        }
    }
}

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

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

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