docx-templating 1.0.0

Rust library for DOCX templating with variable replacement, conditionals, and loops
Documentation
//! Template syntax parsing.
//!
//! This module provides parsing of template syntax into an AST using winnow combinators.
//!
//! ## Phase 3 Implementation
//!
//! - Single-pass parser using winnow combinators
//! - AST node types (Variable, If, For, etc.)
//! - Span tracking for error reporting
//!
//! The parser is internal: it is reached through template binding, never
//! through the public API.

pub mod ast;
pub mod parser;
pub mod span;

use ast::{Expression, Node};

/// Syntax error type.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum SyntaxError {
    /// Parse error from winnow.
    #[error("parse error at {span}: {message}")]
    ParseError {
        /// Location of the parse error.
        span: span::Span,
        /// Error message.
        message: String,
    },
}

/// Parses template syntax from a string into an AST.
///
/// This is the main entry point for Phase 3. The parser operates on plain strings
/// and is independent of DOCX/XML concerns.
///
/// # Errors
///
/// Returns a `SyntaxError` if the template contains invalid syntax:
/// - Unclosed placeholders (`{{` without `}}`)
/// - Unclosed blocks (`{% if %}` without `{% endif %}`)
/// - Mismatched block terminators
/// - Invalid expressions
pub(crate) fn parse_template(input: &str) -> Result<Vec<Node>, SyntaxError> {
    parser::parse(input)
}

/// Parses a standalone expression such as `user.name` or `not items.0`.
///
/// Used by the binding layer for the condition of an `if` tag and the iterable
/// of a `for` tag.
pub(crate) fn parse_expression(input: &str) -> Result<Expression, SyntaxError> {
    parser::parse_expression(input)
}