docx-templating 1.0.0

Rust library for DOCX templating with variable replacement, conditionals, and loops
Documentation
//! Canonical DOCX part-name constants and classification helpers.
//!
//! OPC part names are case-sensitive. The constants here match the casing used
//! by Microsoft Word and the OOXML specification.

// ── Required parts ────────────────────────────────────────────────────────────

/// `[Content_Types].xml` — required in every DOCX/OPC package.
pub const CONTENT_TYPES: &str = "[Content_Types].xml";

/// `_rels/.rels` — top-level relationship file, required in every DOCX package.
pub const ROOT_RELS: &str = "_rels/.rels";

/// `word/document.xml` — main document body, required in every DOCX package.
pub const DOCUMENT: &str = "word/document.xml";

/// All parts that must be present for the library to process a package.
pub const REQUIRED_PARTS: &[&str] = &[CONTENT_TYPES, ROOT_RELS, DOCUMENT];

// ── Optional renderable parts ─────────────────────────────────────────────────

/// Prefix shared by all `word/header*.xml` parts.
pub const HEADER_PREFIX: &str = "word/header";

/// Prefix shared by all `word/footer*.xml` parts.
pub const FOOTER_PREFIX: &str = "word/footer";

/// `word/footnotes.xml` — footnotes part (optional).
pub const FOOTNOTES: &str = "word/footnotes.xml";

/// `word/endnotes.xml` — endnotes part (optional).
pub const ENDNOTES: &str = "word/endnotes.xml";

/// `word/styles.xml` — style definitions (optional, copied unchanged for now).
pub const STYLES: &str = "word/styles.xml";

/// `word/settings.xml` — document settings (optional, copied unchanged for now).
pub const SETTINGS: &str = "word/settings.xml";

// ── Classification helpers ─────────────────────────────────────────────────────

/// Return `true` if `name` is the main document part.
pub fn is_main_document(name: &str) -> bool {
    name == DOCUMENT
}

/// Return `true` if `name` is a header part (`word/header*.xml`).
pub fn is_header(name: &str) -> bool {
    name.starts_with(HEADER_PREFIX) && name.ends_with(".xml")
}

/// Return `true` if `name` is a footer part (`word/footer*.xml`).
pub fn is_footer(name: &str) -> bool {
    name.starts_with(FOOTER_PREFIX) && name.ends_with(".xml")
}

/// Return `true` if `name` is the footnotes part.
pub fn is_footnotes(name: &str) -> bool {
    name == FOOTNOTES
}

/// Return `true` if `name` is the endnotes part.
pub fn is_endnotes(name: &str) -> bool {
    name == ENDNOTES
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]

    use super::*;

    #[test]
    fn required_parts_contains_three_entries() {
        assert_eq!(REQUIRED_PARTS.len(), 3);
        assert!(REQUIRED_PARTS.contains(&CONTENT_TYPES));
        assert!(REQUIRED_PARTS.contains(&ROOT_RELS));
        assert!(REQUIRED_PARTS.contains(&DOCUMENT));
    }

    #[test]
    fn is_main_document_matches_only_document() {
        assert!(is_main_document("word/document.xml"));
        assert!(!is_main_document("word/header1.xml"));
        assert!(!is_main_document("word/document.xml.rels"));
    }

    #[test]
    fn is_header_matches_numbered_headers() {
        assert!(is_header("word/header1.xml"));
        assert!(is_header("word/header2.xml"));
        assert!(is_header("word/header10.xml"));
        assert!(!is_header("word/footer1.xml"));
        assert!(!is_header("word/header1.xml.rels"));
    }

    #[test]
    fn is_footer_matches_numbered_footers() {
        assert!(is_footer("word/footer1.xml"));
        assert!(is_footer("word/footer2.xml"));
        assert!(!is_footer("word/header1.xml"));
    }

    #[test]
    fn is_footnotes_and_endnotes() {
        assert!(is_footnotes("word/footnotes.xml"));
        assert!(!is_footnotes("word/endnotes.xml"));
        assert!(is_endnotes("word/endnotes.xml"));
        assert!(!is_endnotes("word/footnotes.xml"));
    }
}