f-office-docx 0.2.1

DOCX model and Word's layout rules.
Documentation
//! The styles that the named edits name and follow: the ID, the name, the kind and the next
//! style of each style of the styles part (ECMA-376 Part 1, 17.7.4.17), and the default
//! paragraph style.

use f_office_opc::Package;
use f_office_xml::{Document, Limits as XmlLimits};

use super::{NamedFault, styles_part};
use crate::body::ParagraphIds;
use crate::ns::{self, W};
use crate::reader::PartReader;
use crate::styles::{StyleKind, is_default, style_kind};

/// One style.
struct Style {
    id: String,
    name: Option<String>,
    kind: StyleKind,
    next: Option<String>,
    default: bool,
}

/// The styles of a document's styles part, or none when it has no part that reads.
#[derive(Default)]
pub(super) struct Styles {
    styles: Vec<Style>,
}

impl Styles {
    /// The styles of the styles part of the main document part `main`.
    pub(super) fn of(package: &Package, main: &str, limits: &XmlLimits) -> Self {
        let Some((name, bytes)) = styles_part(package, main)
            .and_then(|name| package.part(&name).map(|bytes| (name, bytes)))
        else {
            return Self::default();
        };
        let Ok(doc) = Document::parse_with_aliases(bytes, limits, ns::STRICT_ALIASES) else {
            return Self::default();
        };
        let Ok(root) = doc.root() else {
            return Self::default();
        };
        let value = |node, local| {
            doc.child(node, W, local)
                .and_then(|child| doc.attribute(child, Some(W), "val"))
                .map(str::to_owned)
        };
        let ids = ParagraphIds::default();
        let mut reader = PartReader::of_part(&doc, &name, &ids);
        let styles = doc
            .child_elements(root)
            .filter(|&node| doc.is(node, W, "style"))
            .filter_map(|node| {
                Some(Style {
                    id: doc.attribute(node, Some(W), "styleId")?.to_owned(),
                    name: value(node, "name"),
                    kind: style_kind(&mut reader, node),
                    next: value(node, "next"),
                    default: is_default(&doc, node),
                })
            })
            .collect();
        Self { styles }
    }

    /// The ID of the style of kind `kind` whose ID is `given`, or else whose name is `given`
    /// in any case.
    pub(super) fn resolve(&self, given: &str, kind: StyleKind) -> Result<String, NamedFault> {
        let of_kind = || self.styles.iter().filter(|style| style.kind == kind);
        let lower = given.to_lowercase();
        of_kind()
            .find(|style| style.id == given)
            .or_else(|| {
                of_kind().find(|style| {
                    style
                        .name
                        .as_ref()
                        .is_some_and(|name| name.to_lowercase() == lower)
                })
            })
            .map(|style| style.id.clone())
            .ok_or_else(|| NamedFault::NoSuchStyle {
                style: given.to_owned(),
                kind: kind.word(),
                near: of_kind()
                    .filter(|style| style.id.to_lowercase().contains(&lower))
                    .map(|style| style.id.clone())
                    .collect(),
            })
    }

    /// The style that the paragraph style `id` names as its next one (`w:next`).
    pub(super) fn next(&self, id: &str) -> Option<&str> {
        self.styles
            .iter()
            .find(|style| style.kind == StyleKind::Paragraph && style.id == id)
            .and_then(|style| style.next.as_deref())
    }

    /// The ID of the default paragraph style: the last paragraph style with `w:default`, as the
    /// layout's style reader takes it.
    pub(super) fn default_paragraph(&self) -> Option<&str> {
        self.styles
            .iter()
            .rfind(|style| style.kind == StyleKind::Paragraph && style.default)
            .map(|style| style.id.as_str())
    }
}