document_tree/
attribute_types.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::str::FromStr;

use anyhow::{bail, format_err, Error};
use regex::Regex;
use serde_derive::Serialize;

use crate::url::Url;

#[derive(Debug, PartialEq, Eq, Hash, Serialize, Clone)]
pub enum EnumeratedListType {
    Arabic,
    LowerAlpha,
    UpperAlpha,
    LowerRoman,
    UpperRoman,
}

#[derive(Default, Debug, PartialEq, Eq, Hash, Serialize, Clone)]
pub enum FixedSpace {
    Default,
    // yes, default really is not “Default”
    #[default]
    Preserve,
}

#[derive(Debug, PartialEq, Eq, Hash, Serialize, Clone)]
pub enum AlignH {
    Left,
    Center,
    Right,
}
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Clone)]
pub enum AlignHV {
    Top,
    Middle,
    Bottom,
    Left,
    Center,
    Right,
}
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Clone)]
pub enum AlignV {
    Top,
    Middle,
    Bottom,
}

#[derive(Debug, PartialEq, Eq, Hash, Serialize, Clone)]
pub enum TableAlignH {
    Left,
    Right,
    Center,
    Justify,
    Char,
}
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Clone)]
pub enum TableBorder {
    Top,
    Bottom,
    TopBottom,
    All,
    Sides,
    None,
}

#[derive(Debug, PartialEq, Eq, Hash, Serialize, Clone)]
pub struct ID(pub String);
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Clone)]
pub struct NameToken(pub String);

// The table DTD has the cols attribute of tgroup as required, but having
// TableGroupCols not implement Default would leave no possible implementation
// for TableGroup::with_children.
#[derive(Default, Debug, PartialEq, Eq, Hash, Serialize, Clone)]
pub struct TableGroupCols(pub usize);

// no eq for f64
#[derive(Debug, PartialEq, Serialize, Clone)]
pub enum Measure {
    // http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#length-units
    Em(f64),
    Ex(f64),
    Mm(f64),
    Cm(f64),
    In(f64),
    Px(f64),
    Pt(f64),
    Pc(f64),
}

impl FromStr for AlignHV {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use self::AlignHV::*;
        Ok(match s {
            "top" => Top,
            "middle" => Middle,
            "bottom" => Bottom,
            "left" => Left,
            "center" => Center,
            "right" => Right,
            s => bail!("Invalid Alignment {}", s),
        })
    }
}

impl From<&str> for ID {
    fn from(s: &str) -> Self {
        ID(s.to_owned().replace(' ', "-"))
    }
}

impl From<&str> for NameToken {
    fn from(s: &str) -> Self {
        NameToken(s.to_owned())
    }
}

impl FromStr for Measure {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use self::Measure::*;
        let re =
            Regex::new(r"(?P<float>\d+\.\d*|\.?\d+)\s*(?P<unit>em|ex|mm|cm|in|px|pt|pc)").unwrap();
        let caps: regex::Captures = re
            .captures(s)
            .ok_or_else(|| format_err!("Invalid measure"))?;
        let value: f64 = caps["float"].parse()?;
        Ok(match &caps["unit"] {
            "em" => Em(value),
            "ex" => Ex(value),
            "mm" => Mm(value),
            "cm" => Cm(value),
            "in" => In(value),
            "px" => Px(value),
            "pt" => Pt(value),
            "pc" => Pc(value),
            _ => unreachable!(),
        })
    }
}

#[cfg(test)]
mod parse_tests {
    use super::*;

    #[test]
    fn measure() {
        let _a: Measure = "1.5em".parse().unwrap();
        let _b: Measure = "20 mm".parse().unwrap();
        let _c: Measure = ".5in".parse().unwrap();
        let _d: Measure = "1.pc".parse().unwrap();
    }
}

pub(crate) trait CanBeEmpty {
    fn is_empty(&self) -> bool;
}

/* Specialization necessary
impl<T> CanBeEmpty for T {
    fn is_empty(&self) -> bool { false }
}
*/
macro_rules! impl_cannot_be_empty {
    ($t:ty) => {
        impl CanBeEmpty for $t {
            fn is_empty(&self) -> bool { false }
        }
    };
    ($t:ty, $($ts:ty),*) => {
        impl_cannot_be_empty!($t);
        impl_cannot_be_empty!($($ts),*);
    };
}
impl_cannot_be_empty!(Url);
impl_cannot_be_empty!(TableGroupCols);

impl<T> CanBeEmpty for Option<T> {
    fn is_empty(&self) -> bool {
        self.is_none()
    }
}

impl<T> CanBeEmpty for Vec<T> {
    fn is_empty(&self) -> bool {
        self.is_empty()
    }
}

impl CanBeEmpty for bool {
    fn is_empty(&self) -> bool {
        !self
    }
}

impl CanBeEmpty for FixedSpace {
    fn is_empty(&self) -> bool {
        self == &FixedSpace::default()
    }
}