f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
//! What each target's profile says about itself and the values that it sets, for the checks of
//! the target rules ([`Facts`]).

use super::{Error, Layer, Merged, Target, built_in, chain};

/// The kinds of values that a profile holds, by the table that holds them.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Section {
    /// A default of the configuration, such as the printer's resolution (`[configuration]`).
    Configuration,
    /// The layout device, such as the step of a tab leader (`[layout.*]`).
    Layout,
    /// How the PDF writer writes numbers, such as the page size (`[writer.*]`). Only the
    /// output stage reads it.
    Writer,
    /// The variant of a quirk (`[quirks.*]`).
    Quirks,
}

/// One value of a profile's file.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Entry {
    /// The table that holds it.
    pub section: Section,
    /// Its name, such as `page_steps_per_inch`, or its path in the configuration, such as
    /// `printer.dots_per_inch`.
    pub name: String,
    /// Its value as written: a number, or a text in double quotes; a quirk's variant without its
    /// quotes.
    pub value: String,
}

/// The entries of a layer's `configuration`, `layout`, `writer` and `quirks` tables.
pub(super) fn entries(layer: &Layer) -> Vec<Entry> {
    let entry = |section, (name, value): (String, String)| Entry {
        section,
        name,
        value,
    };
    let owned = |(name, value): (&str, String)| (name.to_owned(), value);
    let mut entries: Vec<Entry> = (layer.configuration.values().into_iter())
        .map(|(name, value)| entry(Section::Configuration, (name, value.to_string())))
        .collect();
    entries.extend(
        (layer.layout.entries().into_iter()).map(|pair| entry(Section::Layout, owned(pair))),
    );
    entries.extend(
        (layer.writer.entries().into_iter()).map(|pair| entry(Section::Writer, owned(pair))),
    );
    entries.extend(
        layer
            .quirks
            .iter()
            .map(|(name, setting)| entry(Section::Quirks, (name.clone(), setting.value.clone()))),
    );
    entries
}

/// What a target's own profile says about itself and the values that it sets.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Facts {
    /// The target's ID.
    pub id: String,
    /// The ID of the profile that it inherits from, if any.
    pub inherits: Option<String>,
    /// The product, such as its name and version.
    pub product: String,
    /// The values that the file sets, with the value that each one overrides, if the chain that
    /// it inherits sets it.
    pub entries: Vec<(Entry, Option<String>)>,
}

impl Facts {
    /// The facts of every target's profile, for the checks of the target rules.
    ///
    /// # Errors
    ///
    /// Returns an error when a profile does not read.
    pub fn all() -> Result<Vec<Self>, Error> {
        Target::all().map(Self::of).collect()
    }

    pub(super) fn of(target: Target) -> Result<Self, Error> {
        let own = built_in(target.name())?;
        let mut base = Merged::default();
        if let Some(inherits) = &own.inherits {
            for (name, layer) in chain(inherits)? {
                base.take(name, layer)?;
            }
        }
        let inherited = entries(&base.layer);
        let entries = entries(own)
            .into_iter()
            .map(|entry| {
                let overridden = inherited
                    .iter()
                    .find(|b| b.section == entry.section && b.name == entry.name)
                    .map(|b| b.value.clone());
                (entry, overridden)
            })
            .collect();
        Ok(Self {
            id: target.name().to_owned(),
            inherits: own.inherits.clone(),
            product: own.product.clone().ok_or(Error::Missing("product"))?,
            entries,
        })
    }
}