f-office-xml 0.2.4

Minimal read-only XML tree for Office Open XML and SVG parts.
Documentation
//! The tables of the enumerations of Office Open XML: the simple types whose values are a list of
//! names, such as `ST_Jc` and `ST_HeightRule`. Each type that a place reads is a table of all its
//! names ([`Values`]), each with the case that F-Office reads it as and how F-Office draws it
//! ([`Case`], [`Drawn`]). The table stands beside the type of its cases in the format crate, and
//! the format crate's reader reads a value through it and gives its own warnings: a name that
//! F-Office draws as another value's case, and any other text, which counts as missing.

/// How F-Office draws a value of a simple type.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Drawn {
    /// As its own case.
    Own,
    /// As the case of another value of the type, such as a double underline as a single one,
    /// with the warning `value-not-supported`.
    AsAnother,
}

/// One name of a simple type: the name as the schema spells it, the case that F-Office reads
/// it as (`reads_as`), and how F-Office draws it.
#[derive(Clone, Copy, Debug)]
pub struct Case<T: 'static> {
    name: &'static str,
    reads_as: T,
    drawn: Drawn,
}

/// A name that F-Office draws as its own case.
pub const fn own<T>(name: &'static str, reads_as: T) -> Case<T> {
    Case {
        name,
        reads_as,
        drawn: Drawn::Own,
    }
}

/// A name that F-Office draws as the case of another value.
pub const fn another<T>(name: &'static str, reads_as: T) -> Case<T> {
    Case {
        name,
        reads_as,
        drawn: Drawn::AsAnother,
    }
}

/// The names of a simple type, as one place reads them: the cases that it tells apart, and
/// for a type with many names, such as `ST_NumberFormat`, all the type's names, of which those
/// that are no case are drawn as one case.
#[derive(Debug)]
pub struct Values<T: 'static> {
    cases: &'static [Case<T>],
    others: Option<(&'static [&'static str], T)>,
}

impl<T> Values<T> {
    /// A table whose cases are all the type's names.
    #[must_use]
    pub const fn new(cases: &'static [Case<T>]) -> Self {
        Self {
            cases,
            others: None,
        }
    }

    /// A table of the cases `cases` and of the type's names `names`, which hold the names of
    /// the cases too: each name that is no case is drawn as `other`.
    #[must_use]
    pub const fn with_others(
        cases: &'static [Case<T>],
        names: &'static [&'static str],
        other: T,
    ) -> Self {
        Self {
            cases,
            others: Some((names, other)),
        }
    }

    /// The case of the name `value`, its spelling and how F-Office draws it, or `None` when it
    /// is no name of the type: the exact spelling, as XML Schema compares the values of an
    /// enumeration.
    #[must_use]
    pub fn find(&self, value: &str) -> Option<(&T, &'static str, Drawn)> {
        self.find_by(|name| name == value)
    }

    /// The case of the name `value` in any ASCII case, as [`Values::find`] reads the exact
    /// spelling, with the name as the table spells it: for a keyword of CSS, such as a
    /// property of the `style` of a VML shape, which CSS2 (4.1.3) reads in any case.
    #[must_use]
    pub fn find_any_case(&self, value: &str) -> Option<(&T, &'static str, Drawn)> {
        self.find_by(|name| name.eq_ignore_ascii_case(value))
    }

    fn find_by(&self, matches: impl Fn(&str) -> bool) -> Option<(&T, &'static str, Drawn)> {
        if let Some(case) = self.cases.iter().find(|case| matches(case.name)) {
            return Some((&case.reads_as, case.name, case.drawn));
        }
        let (names, other) = self.others.as_ref()?;
        let name = names.iter().find(|name| matches(name))?;
        Some((other, name, Drawn::AsAnother))
    }

    /// Each name of the type with how F-Office draws it, such as for the test beside each
    /// table that checks it against the schema's names.
    #[must_use]
    pub fn names(&self) -> Vec<(&'static str, Drawn)> {
        let mut names: Vec<(&'static str, Drawn)> = self
            .cases
            .iter()
            .map(|case| (case.name, case.drawn))
            .collect();
        if let Some((others, _)) = &self.others {
            names.extend(
                others
                    .iter()
                    .filter(|name| self.cases.iter().all(|case| case.name != **name))
                    .map(|name| (*name, Drawn::AsAnother)),
            );
        }
        names
    }
}

#[cfg(test)]
mod tests {
    use super::{Drawn, Values, another, own};

    /// An alignment as a test type reads it, with `start` drawn as left.
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum Side {
        Left,
        Right,
    }

    const SIDES: &Values<Side> = &Values::new(&[
        own("left", Side::Left),
        own("right", Side::Right),
        another("start", Side::Left),
    ]);

    /// A test type of many names, of which F-Office tells one apart.
    const MANY: &Values<Option<Side>> = &Values::with_others(
        &[own("left", Some(Side::Left))],
        &["up", "left", "down"],
        None,
    );

    #[test]
    fn finds_a_name_with_its_case_spelling_and_drawing() {
        assert_eq!(SIDES.find("left"), Some((&Side::Left, "left", Drawn::Own)));
        assert_eq!(
            SIDES.find("start"),
            Some((&Side::Left, "start", Drawn::AsAnother))
        );
        assert_eq!(MANY.find("down"), Some((&None, "down", Drawn::AsAnother)));
        assert_eq!(
            ["Left", " left", "", "middle"].map(|text| SIDES.find(text)),
            [None; 4]
        );
        assert_eq!(MANY.find("Up"), None);
    }

    #[test]
    fn finds_a_keyword_in_any_case_with_the_table_spelling() {
        assert_eq!(
            SIDES.find_any_case("LEFT"),
            Some((&Side::Left, "left", Drawn::Own))
        );
        assert_eq!(
            SIDES.find_any_case("Start"),
            Some((&Side::Left, "start", Drawn::AsAnother))
        );
        assert_eq!(
            MANY.find_any_case("Down"),
            Some((&None, "down", Drawn::AsAnother))
        );
        assert_eq!(
            [" left", "", "middle"].map(|text| SIDES.find_any_case(text)),
            [None; 3]
        );
    }

    #[test]
    fn names_each_name_of_the_type_with_how_it_is_drawn() {
        assert_eq!(
            SIDES.names(),
            [
                ("left", Drawn::Own),
                ("right", Drawn::Own),
                ("start", Drawn::AsAnother)
            ]
        );
        assert_eq!(
            MANY.names(),
            [
                ("left", Drawn::Own),
                ("up", Drawn::AsAnother),
                ("down", Drawn::AsAnother)
            ]
        );
    }
}