f-office-docx 0.1.0

DOCX model and Word's layout rules.
Documentation
use f_office_xml::enumeration::{Values, another, own};
use f_office_xml::{Document, Limits};

use crate::ns::W;
use crate::reader::PartReader;
use crate::warnings::ReadAs;

/// A rule as a test type reads it, with `auto` drawn as at least.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Rule {
    Exact,
    AtLeast,
}

const RULES: &Values<Rule> = &Values::new(&[
    own("exact", Rule::Exact),
    own("atLeast", Rule::AtLeast),
    another("auto", Rule::AtLeast),
]);

#[test]
fn reads_a_name_of_the_type_as_its_case_and_warns_of_one_drawn_as_another() {
    let xml =
        format!(r#"<w:trHeight xmlns:w="{W}" w:a="exact" w:b="atLeast" w:c="auto" w:d="auto"/>"#);
    let doc = Document::parse(xml.as_bytes(), &Limits::default()).unwrap();
    let mut reader = PartReader::new(&doc);
    let root = reader.doc.root().unwrap();
    let values = ["a", "b", "c", "d"].map(|name| reader.enumeration(root, name, RULES));
    assert_eq!(
        values,
        [
            Some(Rule::Exact),
            Some(Rule::AtLeast),
            Some(Rule::AtLeast),
            Some(Rule::AtLeast)
        ]
    );
    assert_eq!(
        reader.unsupported_values(),
        [
            ("trHeight".to_owned(), Some("c".to_owned()), "auto", 1),
            ("trHeight".to_owned(), Some("d".to_owned()), "auto", 1),
        ]
    );
    assert_eq!(reader.invalid_values().len(), 0);
}

#[test]
fn counts_text_another_case_space_and_an_empty_value_as_missing_with_a_warning() {
    let xml =
        format!(r#"<w:trHeight xmlns:w="{W}" w:a="Exact" w:b=" exact " w:c="" w:d="fixed"/>"#);
    let doc = Document::parse(xml.as_bytes(), &Limits::default()).unwrap();
    let mut reader = PartReader::new(&doc);
    let root = reader.doc.root().unwrap();
    let values = ["a", "b", "c", "d", "e"].map(|name| reader.enumeration(root, name, RULES));
    assert_eq!(values, [None; 5]);
    let missing = |name: &str| {
        (
            "trHeight".to_owned(),
            Some(name.to_owned()),
            ReadAs::Missing,
            1,
        )
    };
    assert_eq!(
        reader.invalid_values(),
        [missing("a"), missing("b"), missing("c"), missing("d")]
    );
    assert_eq!(reader.unsupported_values().len(), 0);
}

#[test]
fn reads_text_outside_the_type_as_the_named_case_of_its_place_with_a_warning_that_names_it() {
    let xml = format!(r#"<w:trHeight xmlns:w="{W}" w:a="exact" w:b="fixed" w:c="auto"/>"#);
    let doc = Document::parse(xml.as_bytes(), &Limits::default()).unwrap();
    let mut reader = PartReader::new(&doc);
    let root = reader.doc.root().unwrap();
    let values = ["a", "b", "c", "d"]
        .map(|name| reader.enumeration_or(root, (Some(W), name), RULES, "atLeast"));
    assert_eq!(
        values,
        [
            Some(Rule::Exact),
            Some(Rule::AtLeast),
            Some(Rule::AtLeast),
            None
        ]
    );
    let read_as = ReadAs::Value("atLeast");
    assert_eq!(
        reader.invalid_values(),
        [("trHeight".to_owned(), Some("b".to_owned()), read_as, 1)]
    );
    assert_eq!(
        reader.unsupported_values(),
        [("trHeight".to_owned(), Some("c".to_owned()), "auto", 1)]
    );
    assert_eq!(read_as.to_string(), "reads as atLeast");
}

#[test]
fn reads_text_as_missing_when_the_name_to_read_it_as_is_not_in_the_table() {
    // The warning gives the reading: no case, and no name that the table does not hold.
    let xml = format!(r#"<w:trHeight xmlns:w="{W}" w:a="fixed"/>"#);
    let doc = Document::parse(xml.as_bytes(), &Limits::default()).unwrap();
    let mut reader = PartReader::new(&doc);
    let root = reader.doc.root().unwrap();
    let value = reader.enumeration_or(root, (Some(W), "a"), RULES, "least");
    assert_eq!(value, None);
    assert_eq!(
        reader.invalid_values(),
        [(
            "trHeight".to_owned(),
            Some("a".to_owned()),
            ReadAs::Missing,
            1
        )]
    );
}

/// A test type of many names, of which F-Office tells two apart.
const FEW: &Values<Option<Rule>> = &Values::with_others(
    &[own("exact", Some(Rule::Exact)), own("none", None)],
    &["none", "exact", "wide", "narrow"],
    None,
);

#[test]
fn reads_the_other_names_of_a_type_of_many_names_as_one_case_with_a_warning() {
    let xml = format!(r#"<w:x xmlns:w="{W}" w:a="exact" w:b="none" w:c="wide" w:d="tall"/>"#);
    let doc = Document::parse(xml.as_bytes(), &Limits::default()).unwrap();
    let mut reader = PartReader::new(&doc);
    let root = reader.doc.root().unwrap();
    let values = ["a", "b", "c", "d"].map(|name| reader.enumeration(root, name, FEW));
    assert_eq!(
        values,
        [Some(Some(Rule::Exact)), Some(None), Some(None), None]
    );
    assert_eq!(
        reader.unsupported_values(),
        [("x".to_owned(), Some("c".to_owned()), "wide", 1)]
    );
    assert_eq!(reader.invalid_values().len(), 1);
}