docx-templating 1.0.0

Rust library for DOCX templating with variable replacement, conditionals, and loops
Documentation
//! # In-Memory / Bytes API
//!
//! Demonstrates the fully in-memory render workflow — no output file is
//! written to disk:
//!
//! - `Template::from_path` loads the template.
//! - `RenderedDocument::as_bytes()` — borrow without consuming.
//! - `RenderedDocument::write_to_writer(impl Write)` — stream to any sink.
//! - `RenderedDocument::into_bytes()` — consume into `Vec<u8>`.
//!
//! This is the canonical pattern for Axum/Actix handlers that stream a DOCX
//! response body without touching the filesystem.
//!
//! **First run:** `cargo run --bin gen-examples`
//! **Then:** `cargo run --example 08_bytes_api`

use docx_templating::Template;
use serde_json::json;

fn main() {
    let template = Template::from_path("examples/input/order-confirmation.docx")
        .expect("template not found — run `cargo run --bin gen-examples` first");

    let context = json!({
        "customer": "Globex Inc",
        "order_id": "ORD-98765",
        "date": "2026-08-14",
        "total": "$1,350.00"
    });

    // Render once; inspect size via as_bytes() before consuming.
    let rendered = template.render(context).expect("render");

    // ── as_bytes() ───────────────────────────────────────────────────────────
    println!(
        "Rendered size (as_bytes): {} bytes",
        rendered.as_bytes().len()
    );

    // ── write_to_writer() — stream to any Write sink ─────────────────────────
    // In a real Axum handler: rendered.write_to_writer(&mut response_body)?;
    let mut buf: Vec<u8> = Vec::new();
    let context2 = json!({
        "customer": "Acme Corp",
        "order_id": "ORD-11111",
        "date": "2026-08-14",
        "total": "$500.00"
    });
    let rendered2 = template.render(context2).expect("render2");
    rendered2
        .write_to_writer(&mut buf)
        .expect("write_to_writer");
    println!("Streamed via write_to_writer: {} bytes", buf.len());

    // ── into_bytes() ─────────────────────────────────────────────────────────
    let bytes: Vec<u8> = rendered.into_bytes();
    println!("Consumed via into_bytes: {} bytes", bytes.len());
    println!("All bytes-API paths verified — no files written to disk.");
}