docx-templating 1.0.0

Rust library for DOCX templating with variable replacement, conditionals, and loops
Documentation
//! Evaluation errors.
//!
//! These errors are raised while a bound template is evaluated against a
//! context. They carry the data path and the document part so the caller can
//! point at the template text that failed.

/// Render error type.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum RenderError {
    /// Value is missing for a placeholder.
    #[error("missing value for placeholder '{path}'{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    MissingValue {
        /// Path that could not be resolved.
        path: String,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// Expected an iterable value but found something else.
    #[error("expected iterable value at '{path}', found {actual}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    ExpectedIterable {
        /// Path to the value.
        path: String,
        /// Actual value kind found.
        actual: String,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// Type mismatch during path resolution.
    #[error("type mismatch at path '{path}': expected {expected}, found {actual}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    TypeMismatch {
        /// Path being resolved.
        path: String,
        /// Expected type.
        expected: &'static str,
        /// Actual type found.
        actual: &'static str,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// Current item (`.`) used outside loop context.
    #[error("current item '.' can only be used inside loops")]
    CurrentItemOutsideLoop,

    /// Loop metadata used outside loop context.
    #[error("loop metadata '{name}' can only be used inside a loop")]
    MetaOutsideLoop {
        /// The metadata name, for example `@index`.
        name: String,
    },

    /// Cannot convert value to text for insertion.
    #[error("cannot convert {value_type} to text")]
    CannotConvertToText {
        /// Type of value that cannot be converted.
        value_type: String,
    },

    /// A filter named in the template is not registered.
    #[error("unknown filter '{name}'{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    UnknownFilter {
        /// The filter name as written in the template.
        name: String,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// A filter rejected the value it received.
    #[error("filter '{name}' failed: {message}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    FilterFailed {
        /// The filter name as written in the template.
        name: String,
        /// The reason reported by the filter.
        message: String,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// Two values cannot be ordered with the requested operator.
    #[error("cannot compare {left} and {right} with '{operator}'{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    InvalidComparison {
        /// The operator as written in the template.
        operator: String,
        /// Type of the left value.
        left: String,
        /// Type of the right value.
        right: String,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// An arithmetic operator received a value that is not a number.
    #[error("cannot apply '{operator}' to {actual}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    ArithmeticType {
        /// The operator as written in the template.
        operator: String,
        /// The type that is not a number.
        actual: String,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// Division or remainder by zero.
    #[error("cannot apply '{operator}' by zero{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    DivisionByZero {
        /// The operator as written in the template.
        operator: String,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// Arithmetic produced a value outside the supported number range.
    #[error("arithmetic '{operator}' produced a value outside the supported range{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    ArithmeticOverflow {
        /// The operator as written in the template.
        operator: String,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// A number literal in the template is not a valid number.
    #[error("invalid number literal '{literal}'{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    InvalidLiteral {
        /// The literal as written in the template.
        literal: String,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// An expression cannot be used in this position.
    #[error("{message}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    ExpressionNotSupported {
        /// Explanation of the unsupported use.
        message: String,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// Block placement is not supported in DOCX structure.
    #[error("unsupported block placement: {message}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
    UnsupportedBlockPlacement {
        /// Description of why placement is unsupported.
        message: String,
        /// DOCX part where the error occurred, if known.
        part: Option<String>,
    },

    /// A placeholder spans two inline containers.
    #[error("text span in {part} at paragraph {paragraph:?} crosses inline containers")]
    TextSpansContainers {
        /// The part name.
        part: String,
        /// The paragraph path.
        paragraph: Vec<usize>,
        /// The container identities the span touched.
        containers: Vec<Option<usize>>,
    },

    /// A resolved edit points outside the region it belongs to.
    #[error("plan target {path:?} is outside the region it belongs to")]
    InvalidPlanTarget {
        /// The path that could not be mapped.
        path: Vec<usize>,
    },

    /// The evaluator produced edits that overlap.
    #[error("invalid plan: {message}")]
    InvalidPlan {
        /// The plan validation message.
        message: String,
    },
}

impl From<crate::domain::plan::ApplyError> for RenderError {
    fn from(error: crate::domain::plan::ApplyError) -> Self {
        match error {
            crate::domain::plan::ApplyError::MissingTarget { path }
            | crate::domain::plan::ApplyError::MissingSlice { path } => {
                RenderError::InvalidPlanTarget { path }
            }
            other => RenderError::InvalidPlan {
                message: other.to_string(),
            },
        }
    }
}