f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
//! The development override of a profile ([`Overrides`]).

use std::sync::Arc;

use super::Layer;

/// The development override: layers of a profile's values, such as quirk variants, after the
/// caller's configuration, which changes Word's own behaviors and numbers. Only a build with the
/// feature `development` lets a caller set it; a layer sets no value of the configuration and
/// no `product`, `builds`, `renderer` or `fonts.environment`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Overrides {
    /// The layers, each with the name that the profile's origins give it.
    layers: Arc<[(Arc<str>, Layer)]>,
}

impl Overrides {
    /// The override with the layer `layer` after its others, named `name` in the origins.
    #[cfg(any(test, feature = "development"))]
    #[must_use]
    pub fn with_layer(&self, name: &str, layer: Layer) -> Self {
        let mut layers = self.layers.to_vec();
        layers.push((name.into(), layer));
        Self {
            layers: layers.into(),
        }
    }

    /// The override with the quirk `name` at its variant `variant`, by their names in a profile,
    /// in a layer of its own named `quirks`.
    #[cfg(any(test, feature = "development"))]
    #[must_use]
    pub fn with_quirk(&self, name: &str, variant: &str) -> Self {
        self.with_layer("quirks", Layer::default().with_quirk(name, variant))
    }

    /// The quirks that the override chooses, each by its name and the name of its variant, as
    /// its last layer that sets the quirk gives it, sorted by name.
    #[cfg(feature = "development")]
    pub fn quirks(&self) -> impl Iterator<Item = (&str, &str)> {
        let mut chosen = std::collections::BTreeMap::new();
        for (_, layer) in self.layers() {
            for (name, variant) in &layer.quirks {
                chosen.insert(name.as_str(), variant.value.as_str());
            }
        }
        chosen.into_iter()
    }

    /// The layers, each with its name, in order.
    pub fn layers(&self) -> impl Iterator<Item = (&str, &Layer)> {
        self.layers.iter().map(|(name, layer)| (&**name, layer))
    }

    /// Whether the override has no layer.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.layers.is_empty()
    }
}