f-office-docx 0.2.4

DOCX model and Word's layout rules.
Documentation
//! Small DOCX packages for the tests of the crates that lay out DOCX files, behind the feature
//! `testing`, on the package builder of `f-office-opc`: the namespace of WordprocessingML, the
//! root of a part, the main document and the parts that it names, written once.

pub use f_office_opc::testing::{Builder, PACKAGE_RELATIONSHIPS, RELATIONSHIPS, zip};

/// The source of paragraph IDs, for a test that builds a placed line
/// ([`PlacedLine`](crate::PlacedLine)) without a layout.
pub use crate::body::ParagraphIds;

/// The namespace of the elements of WordprocessingML (ECMA-376 Part 1, 17).
pub const MAIN: &str = crate::ns::W;

/// The main document part of a DOCX package.
pub const DOCUMENT: &str = "word/document.xml";

/// The content of a styles part whose document defaults are empty, so that text keeps the
/// defaults of ECMA-376 and not those of Word's template.
pub const PLAIN_STYLES: &str = "<w:docDefaults><w:rPrDefault/><w:pPrDefault/></w:docDefaults>";

/// The root element `w:{root}` of a WordprocessingML part, which holds `content` and declares
/// the prefixes `w` and `r`.
#[must_use]
pub fn word(root: &str, content: &str) -> String {
    format!(r#"<w:{root} xmlns:w="{MAIN}" xmlns:r="{RELATIONSHIPS}">{content}</w:{root}>"#)
}

/// A DOCX package with the main document `document` and the package's relationship to it.
#[must_use]
pub fn package(document: impl Into<Vec<u8>>) -> Builder {
    Builder::default()
        .relationship("", "main", "officeDocument", DOCUMENT)
        .part(DOCUMENT, document)
}

/// A DOCX package whose main document holds `body` in its `w:body`.
#[must_use]
pub fn docx(body: &str) -> Builder {
    package(word("document", &format!("<w:body>{body}</w:body>")))
}

/// The parts of a DOCX package that its main document names.
pub trait WordParts {
    /// Adds the part `word/{target}`, whose root `w:{root}` holds `content`, and the main
    /// document's relationship of the type `kind` to it under the ID `id`.
    #[must_use]
    fn word_part(self, id: &str, kind: &str, target: &str, root: &str, content: &str) -> Self;

    /// Adds a styles part of [`PLAIN_STYLES`] and the main document's relationship to it.
    #[must_use]
    fn plain_styles(self) -> Self;
}

impl WordParts for Builder {
    fn word_part(self, id: &str, kind: &str, target: &str, root: &str, content: &str) -> Self {
        self.relationship(DOCUMENT, id, kind, target)
            .part(&format!("word/{target}"), word(root, content))
    }

    fn plain_styles(self) -> Self {
        self.word_part(
            "plainStyles",
            "styles",
            "styles.xml",
            "styles",
            PLAIN_STYLES,
        )
    }
}

/// Edits of the XML text of a test that fail the test when the text that they change is not
/// there, so that a test never passes on XML that it did not change.
pub trait Replaced {
    /// The text with its one `from` replaced by `to`.
    ///
    /// # Panics
    ///
    /// When the text holds `from` other than once.
    #[must_use]
    fn replaced(&self, from: &str, to: &str) -> String;

    /// The text with each `from` replaced by `to`.
    ///
    /// # Panics
    ///
    /// When the text does not hold `from`.
    #[must_use]
    fn replaced_all(&self, from: &str, to: &str) -> String;
}

impl Replaced for str {
    #[track_caller]
    fn replaced(&self, from: &str, to: &str) -> String {
        let count = self.matches(from).count();
        assert_eq!(count, 1, "the XML holds {from:?} {count} times, not once");
        self.replacen(from, to, 1)
    }

    #[track_caller]
    fn replaced_all(&self, from: &str, to: &str) -> String {
        assert!(self.contains(from), "the XML does not hold {from:?}");
        self.replace(from, to)
    }
}

#[cfg(test)]
mod tests;