f-office-docx 0.2.1

DOCX model and Word's layout rules.
Documentation
//! The one reader of the enumerations of WordprocessingML: the simple types whose values are a list
//! of names, such as `ST_Jc` and `ST_HeightRule`. Each place reads its type through a table of all
//! its names ([`f_office_xml::enumeration::Values`]), which stands beside the type of its cases. A
//! [`PartReader`] reads a value through its place's table: a name of the table reads as its case,
//! and a name that F-Office draws as another value's case also gives the warning
//! `value-not-supported`. Any other text, a name in another case, a name with space around it and
//! an empty value count as missing, with the warning `invalid-value`, as Word reads them or refuses
//! the document. What a missing value means stays at each place. A place of another format whose
//! present reading of such text is a value of the type that a missing value does not mean, such as
//! the place of an n-ary operator's limits in OMML, keeps that fallback reading, and its warning
//! names the value ([`PartReader::enumeration_or`]).

use f_office_vml::css::Style;
use f_office_warnings::Name;
use f_office_xml::NodeId;
use f_office_xml::enumeration::{Drawn, Values};

use crate::ns::W;
use crate::reader::{PartReader, Value, attribute};
use crate::warnings::{ReadAs, ValuePlace};

/// Checks that `table` holds each name of its simple type once and no other name, with
/// `names` the type's names as the schema lists them, for the test beside each table.
#[cfg(test)]
pub(crate) fn assert_names<T>(table: &Values<T>, names: &str) {
    let mut read: Vec<&str> = table.names().into_iter().map(|(name, _)| name).collect();
    let mut schema: Vec<&str> = names.split_whitespace().collect();
    read.sort_unstable();
    schema.sort_unstable();
    assert_eq!(read, schema);
}

impl PartReader<'_> {
    /// The case of the attribute `name` of `node` in the WordprocessingML namespace, as the
    /// type's table `table` reads it: `None` when the attribute is missing, and when its value
    /// is no name of the table, with a warning that it counts as missing. A name that F-Office
    /// draws as another value's case reads as that case, with a warning.
    pub(crate) fn enumeration<T: Clone>(
        &mut self,
        node: NodeId,
        name: &str,
        table: &'static Values<T>,
    ) -> Option<T> {
        self.enumeration_in(node, (Some(W), name), table)
    }

    /// The case of the attribute `name` of `node` in the namespace `namespace`, or in none,
    /// such as an attribute of a drawing, as [`PartReader::enumeration`] reads it.
    pub(crate) fn enumeration_in<T: Clone>(
        &mut self,
        node: NodeId,
        (namespace, name): (Option<&str>, &str),
        table: &'static Values<T>,
    ) -> Option<T> {
        let found = self.or_missing_in(node, (namespace, name), |value| table.find(value));
        self.case_of(node, || attribute(name), found)
    }

    /// The case of the attribute `name` of `node` in the namespace `namespace`, as
    /// [`PartReader::enumeration_in`] reads it, except text outside the type: it reads as the
    /// case of the table's name `text_as`, with a warning that names it, at a place whose
    /// fallback reading for unrecognized text differs from what a missing value means. A `text_as`
    /// that is no name of the table reads as missing, and the warning says so, so that the warning
    /// always gives the reading.
    pub(crate) fn enumeration_or<T: Clone>(
        &mut self,
        node: NodeId,
        (namespace, name): (Option<&str>, &str),
        table: &'static Values<T>,
        text_as: &'static str,
    ) -> Option<T> {
        let value = Value::of(self.doc.attribute(node, namespace, name), |value| {
            table.find(value)
        });
        match value {
            Value::Missing => None,
            Value::Invalid => {
                let read = table.find(text_as);
                let read_as =
                    read.map_or(ReadAs::Missing, |(_, spelled, _)| ReadAs::Value(spelled));
                self.invalid(node, attribute(name), read_as);
                read.map(|(case, _, _)| case.clone())
            }
            Value::Read(found) => self.case_of(node, || attribute(name), Some(found)),
        }
    }

    /// The case of the text of `node`, such as `wp:align`, as [`PartReader::enumeration`]
    /// reads an attribute, with the space around it left out, as the reader before the one
    /// reader left it out; the warnings name no attribute.
    pub(crate) fn enumeration_text<T: Clone>(
        &mut self,
        node: NodeId,
        table: &'static Values<T>,
    ) -> Option<T> {
        let found = self.text_or_missing(node, |text| table.find(text.trim()));
        self.case_of(node, || ValuePlace::Text, found)
    }

    /// The case of the property `property` of the CSS declarations `style`, which the
    /// attribute `style` of `node` holds, such as a keyword of a VML shape's place, as the table
    /// `table` reads a keyword in any ASCII case (CSS2 4.1.3, which ECMA-376 Part 4 19.1.2.19
    /// takes): `None` when the style does not declare it, and when its value is no name of the
    /// table, with a warning that it counts as missing (CSS2 4.2: a declaration with an illegal
    /// value is ignored). A name that F-Office draws as another value's case reads as that
    /// case, with a warning. Both warnings name the property.
    pub(crate) fn style_keyword<T: Clone>(
        &mut self,
        node: NodeId,
        style: &Style,
        property: &str,
        table: &'static Values<T>,
    ) -> Option<T> {
        let found = self.style_value(node, style, property, |value| table.find_any_case(value));
        self.case_of(node, || style_place(property), found)
    }

    /// The value of the property `property` of the CSS declarations `style`, which the
    /// attribute `style` of `node` holds, read by `parse`, such as a VML shape's `rotation`:
    /// `None` when the style does not declare it, and when its value does not read, with a
    /// warning that names the property and that the value counts as missing (CSS2 4.2: a
    /// declaration with an illegal value is ignored).
    pub(crate) fn style_value<T>(
        &mut self,
        node: NodeId,
        style: &Style,
        property: &str,
        parse: impl FnOnce(&str) -> Option<T>,
    ) -> Option<T> {
        let value = Value::of(style.get(property), parse);
        self.missing_if_invalid(node, || style_place(property), value)
    }

    /// The case that `found` holds, with a warning when F-Office draws its name, the value at
    /// the place that `place` builds in `node`, as another value's case.
    fn case_of<T: Clone>(
        &mut self,
        node: NodeId,
        place: impl FnOnce() -> ValuePlace,
        found: Option<(&T, &'static str, Drawn)>,
    ) -> Option<T> {
        let (case, spelling, drawn) = found?;
        if drawn == Drawn::AsAnother {
            self.not_supported(node, place(), spelling);
        }
        Some(case.clone())
    }
}

/// The place of the property `property` of a `style` attribute, for a warning.
pub(crate) fn style_place(property: &str) -> ValuePlace {
    ValuePlace::Property {
        attribute: Name::new("style"),
        property: Name::new(property),
    }
}

#[cfg(test)]
mod tests;