docx-templating 1.0.0

Rust library for DOCX templating with variable replacement, conditionals, and loops
Documentation
//! # docx-templating
//!
//! A Rust library for generating Microsoft Word documents (`.docx`) from templates
//! with variable placeholders, conditional sections, and loops.
//!
//! ## Features
//!
//! - **Variable replacement**: `{{user.name}}` with nested path support
//! - **Conditionals**: `{% if condition %} ... {% elif condition %} ... {% else %} ... {% endif %}`
//! - **Comparisons and boolean logic**: `{% if total > 100 and paid %}`
//! - **Arithmetic**: `{{price * quantity}}`, `{{subtotal - discount}}`
//! - **Loops**: `{% for item in items %} ... {% endfor %}`
//! - **Filters**: `{{name | upper}}`, `{{nickname | default: "friend"}}`,
//!   `{{total | round: 2}}`, `{{created_at | date: "%Y-%m-%d"}}`, and custom
//!   filters on [`TemplateOptions`]
//! - **Headers, footers, footnotes and endnotes**: opt in with
//!   [`TemplateOptions::headers_and_footers`] and [`TemplateOptions::footnotes`]
//! - **Safe XML handling**: Automatic escaping, no injection vulnerabilities
//! - **Typed context**: render accepts JSON or any borrowed `serde::Serialize` value
//!
//! ## Architecture
//!
//! The crate is a pipeline with typed boundaries:
//!
//! 1. **Domain** (`domain`) - the lossless document tree, template binding,
//!    placement, resolved plan and evaluator. It never touches XML, ZIP or the
//!    filesystem.
//! 2. **Adapters** (`adapters`) - parse XML into the document model and write it
//!    back, and read and write the DOCX package.
//! 3. **Public API** - `Template`, `Context`, `IntoContext`, `RenderedDocument`,
//!    options and errors.
//!
//! `tests/architecture.rs` enforces the boundary between the domain and the
//! adapters.
//!
//! ## Example
//!
//! ```rust,no_run
//! use docx_templating::Template;
//! use serde_json::json;
//!
//! let data = json!({
//!     "user": {
//!         "name": "Ada Lovelace"
//!     }
//! });
//!
//! let output = Template::from_path("template.docx")?
//!     .render(data)?;
//!
//! output.write_to_file("output.docx")?;
//! # Ok::<(), docx_templating::Error>(())
//! ```
//!
//! Any borrowed [`serde::Serialize`] value works as the context, so a caller can
//! pass their own struct instead of building JSON:
//!
//! ```rust,no_run
//! use docx_templating::Template;
//! use serde::Serialize;
//!
//! #[derive(Serialize)]
//! struct Invoice {
//!     total: u32,
//! }
//!
//! # fn example() -> docx_templating::Result<()> {
//! let template = Template::from_path("template.docx")?;
//! let rendered = template.render(&Invoice { total: 42 })?;
//! rendered.write_to_file("output.docx")?;
//! # Ok(())
//! # }
//! ```
//!
//! For a template that is rendered once, [`render`] loads and renders it in a
//! single call:
//!
//! ```rust,no_run
//! # fn example() -> docx_templating::Result<()> {
//! let rendered = docx_templating::render(
//!     "template.docx",
//!     serde_json::json!({ "name": "Ada" }),
//! )?;
//! rendered.write_to_file("output.docx")?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Status
//!
//! **Current version: 1.0.0**, with every phase of the roadmap implemented.
//! Variable replacement, conditionals, paragraph loops, table-row loops,
//! multi-part rendering and hardening are production-ready.
//! The public API is frozen for 1.x: new features are additive and breaking
//! changes wait for 2.0.
//!
//! The main document is always rendered. Headers, footers, footnotes and
//! endnotes are rendered when
//! [`TemplateOptions::headers_and_footers`] and [`TemplateOptions::footnotes`]
//! ask for it; otherwise those parts are copied through unchanged.
//!

#![forbid(unsafe_code)]
#![deny(
    missing_docs,
    missing_debug_implementations,
    clippy::expect_used,
    clippy::unwrap_used,
    clippy::panic,
    clippy::indexing_slicing
)]

// Public modules
pub mod docx;
pub mod error;
pub mod package;

// Pipeline layers. The domain is pure; the adapters translate formats.
mod adapters;
mod api;
mod domain;
mod syntax;

// Public API surface
mod context;
mod diagnostic;
mod filters;
mod options;
mod rendered;
mod template;

// Public re-exports
pub use context::{Context, ContextError, IntoContext};
pub use diagnostic::{Diagnostic, diagnose};
pub use domain::binding::BindingError;
pub use domain::engine::RenderError;
pub use domain::plan::PlanError;
pub use error::{Error, Result, XmlError};
pub use filters::{FilterError, FilterRegistry};
pub use options::{LineBreakPolicy, MissingValuePolicy, TemplateOptions};
pub use rendered::RenderedDocument;
pub use template::{Template, render};