Skip to main content

f_office_xml/
enumeration.rs

1//! The tables of the enumerations of Office Open XML: the simple types whose values are a list of
2//! names, such as `ST_Jc` and `ST_HeightRule`. Each type that a place reads is a table of all its
3//! names ([`Values`]), each with the case that F-Office reads it as and how F-Office draws it
4//! ([`Case`], [`Drawn`]). The table stands beside the type of its cases in the format crate, and
5//! the format crate's reader reads a value through it and gives its own warnings: a name that
6//! F-Office draws as another value's case, and any other text, which counts as missing.
7
8/// How F-Office draws a value of a simple type.
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum Drawn {
11    /// As its own case.
12    Own,
13    /// As the case of another value of the type, such as a double underline as a single one,
14    /// with the warning `value-not-supported`.
15    AsAnother,
16}
17
18/// One name of a simple type: the name as the schema spells it, the case that F-Office reads
19/// it as (`reads_as`), and how F-Office draws it.
20#[derive(Clone, Copy, Debug)]
21pub struct Case<T: 'static> {
22    name: &'static str,
23    reads_as: T,
24    drawn: Drawn,
25}
26
27/// A name that F-Office draws as its own case.
28pub const fn own<T>(name: &'static str, reads_as: T) -> Case<T> {
29    Case {
30        name,
31        reads_as,
32        drawn: Drawn::Own,
33    }
34}
35
36/// A name that F-Office draws as the case of another value.
37pub const fn another<T>(name: &'static str, reads_as: T) -> Case<T> {
38    Case {
39        name,
40        reads_as,
41        drawn: Drawn::AsAnother,
42    }
43}
44
45/// The names of a simple type, as one place reads them: the cases that it tells apart, and
46/// for a type with many names, such as `ST_NumberFormat`, all the type's names, of which those
47/// that are no case are drawn as one case.
48#[derive(Debug)]
49pub struct Values<T: 'static> {
50    cases: &'static [Case<T>],
51    others: Option<(&'static [&'static str], T)>,
52}
53
54impl<T> Values<T> {
55    /// A table whose cases are all the type's names.
56    #[must_use]
57    pub const fn new(cases: &'static [Case<T>]) -> Self {
58        Self {
59            cases,
60            others: None,
61        }
62    }
63
64    /// A table of the cases `cases` and of the type's names `names`, which hold the names of
65    /// the cases too: each name that is no case is drawn as `other`.
66    #[must_use]
67    pub const fn with_others(
68        cases: &'static [Case<T>],
69        names: &'static [&'static str],
70        other: T,
71    ) -> Self {
72        Self {
73            cases,
74            others: Some((names, other)),
75        }
76    }
77
78    /// The case of the name `value`, its spelling and how F-Office draws it, or `None` when it
79    /// is no name of the type: the exact spelling, as XML Schema compares the values of an
80    /// enumeration.
81    #[must_use]
82    pub fn find(&self, value: &str) -> Option<(&T, &'static str, Drawn)> {
83        self.find_by(|name| name == value)
84    }
85
86    /// The case of the name `value` in any ASCII case, as [`Values::find`] reads the exact
87    /// spelling, with the name as the table spells it: for a keyword of CSS, such as a
88    /// property of the `style` of a VML shape, which CSS2 (4.1.3) reads in any case.
89    #[must_use]
90    pub fn find_any_case(&self, value: &str) -> Option<(&T, &'static str, Drawn)> {
91        self.find_by(|name| name.eq_ignore_ascii_case(value))
92    }
93
94    fn find_by(&self, matches: impl Fn(&str) -> bool) -> Option<(&T, &'static str, Drawn)> {
95        if let Some(case) = self.cases.iter().find(|case| matches(case.name)) {
96            return Some((&case.reads_as, case.name, case.drawn));
97        }
98        let (names, other) = self.others.as_ref()?;
99        let name = names.iter().find(|name| matches(name))?;
100        Some((other, name, Drawn::AsAnother))
101    }
102
103    /// Each name of the type with how F-Office draws it, such as for the test beside each
104    /// table that checks it against the schema's names.
105    #[must_use]
106    pub fn names(&self) -> Vec<(&'static str, Drawn)> {
107        let mut names: Vec<(&'static str, Drawn)> = self
108            .cases
109            .iter()
110            .map(|case| (case.name, case.drawn))
111            .collect();
112        if let Some((others, _)) = &self.others {
113            names.extend(
114                others
115                    .iter()
116                    .filter(|name| self.cases.iter().all(|case| case.name != **name))
117                    .map(|name| (*name, Drawn::AsAnother)),
118            );
119        }
120        names
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use super::{Drawn, Values, another, own};
127
128    /// An alignment as a test type reads it, with `start` drawn as left.
129    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
130    enum Side {
131        Left,
132        Right,
133    }
134
135    const SIDES: &Values<Side> = &Values::new(&[
136        own("left", Side::Left),
137        own("right", Side::Right),
138        another("start", Side::Left),
139    ]);
140
141    /// A test type of many names, of which F-Office tells one apart.
142    const MANY: &Values<Option<Side>> = &Values::with_others(
143        &[own("left", Some(Side::Left))],
144        &["up", "left", "down"],
145        None,
146    );
147
148    #[test]
149    fn finds_a_name_with_its_case_spelling_and_drawing() {
150        assert_eq!(SIDES.find("left"), Some((&Side::Left, "left", Drawn::Own)));
151        assert_eq!(
152            SIDES.find("start"),
153            Some((&Side::Left, "start", Drawn::AsAnother))
154        );
155        assert_eq!(MANY.find("down"), Some((&None, "down", Drawn::AsAnother)));
156        assert_eq!(
157            ["Left", " left", "", "middle"].map(|text| SIDES.find(text)),
158            [None; 4]
159        );
160        assert_eq!(MANY.find("Up"), None);
161    }
162
163    #[test]
164    fn finds_a_keyword_in_any_case_with_the_table_spelling() {
165        assert_eq!(
166            SIDES.find_any_case("LEFT"),
167            Some((&Side::Left, "left", Drawn::Own))
168        );
169        assert_eq!(
170            SIDES.find_any_case("Start"),
171            Some((&Side::Left, "start", Drawn::AsAnother))
172        );
173        assert_eq!(
174            MANY.find_any_case("Down"),
175            Some((&None, "down", Drawn::AsAnother))
176        );
177        assert_eq!(
178            [" left", "", "middle"].map(|text| SIDES.find_any_case(text)),
179            [None; 3]
180        );
181    }
182
183    #[test]
184    fn names_each_name_of_the_type_with_how_it_is_drawn() {
185        assert_eq!(
186            SIDES.names(),
187            [
188                ("left", Drawn::Own),
189                ("right", Drawn::Own),
190                ("start", Drawn::AsAnother)
191            ]
192        );
193        assert_eq!(
194            MANY.names(),
195            [
196                ("left", Drawn::Own),
197                ("up", Drawn::AsAnother),
198                ("down", Drawn::AsAnother)
199            ]
200        );
201    }
202}