Skip to main content

docx_templating/domain/engine/
error.rs

1//! Evaluation errors.
2//!
3//! These errors are raised while a bound template is evaluated against a
4//! context. They carry the data path and the document part so the caller can
5//! point at the template text that failed.
6
7/// Render error type.
8#[derive(Debug, thiserror::Error)]
9#[non_exhaustive]
10pub enum RenderError {
11    /// Value is missing for a placeholder.
12    #[error("missing value for placeholder '{path}'{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
13    MissingValue {
14        /// Path that could not be resolved.
15        path: String,
16        /// DOCX part where the error occurred, if known.
17        part: Option<String>,
18    },
19
20    /// Expected an iterable value but found something else.
21    #[error("expected iterable value at '{path}', found {actual}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
22    ExpectedIterable {
23        /// Path to the value.
24        path: String,
25        /// Actual value kind found.
26        actual: String,
27        /// DOCX part where the error occurred, if known.
28        part: Option<String>,
29    },
30
31    /// Type mismatch during path resolution.
32    #[error("type mismatch at path '{path}': expected {expected}, found {actual}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
33    TypeMismatch {
34        /// Path being resolved.
35        path: String,
36        /// Expected type.
37        expected: &'static str,
38        /// Actual type found.
39        actual: &'static str,
40        /// DOCX part where the error occurred, if known.
41        part: Option<String>,
42    },
43
44    /// Current item (`.`) used outside loop context.
45    #[error("current item '.' can only be used inside loops")]
46    CurrentItemOutsideLoop,
47
48    /// Loop metadata used outside loop context.
49    #[error("loop metadata '{name}' can only be used inside a loop")]
50    MetaOutsideLoop {
51        /// The metadata name, for example `@index`.
52        name: String,
53    },
54
55    /// Cannot convert value to text for insertion.
56    #[error("cannot convert {value_type} to text")]
57    CannotConvertToText {
58        /// Type of value that cannot be converted.
59        value_type: String,
60    },
61
62    /// A filter named in the template is not registered.
63    #[error("unknown filter '{name}'{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
64    UnknownFilter {
65        /// The filter name as written in the template.
66        name: String,
67        /// DOCX part where the error occurred, if known.
68        part: Option<String>,
69    },
70
71    /// A filter rejected the value it received.
72    #[error("filter '{name}' failed: {message}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
73    FilterFailed {
74        /// The filter name as written in the template.
75        name: String,
76        /// The reason reported by the filter.
77        message: String,
78        /// DOCX part where the error occurred, if known.
79        part: Option<String>,
80    },
81
82    /// Two values cannot be ordered with the requested operator.
83    #[error("cannot compare {left} and {right} with '{operator}'{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
84    InvalidComparison {
85        /// The operator as written in the template.
86        operator: String,
87        /// Type of the left value.
88        left: String,
89        /// Type of the right value.
90        right: String,
91        /// DOCX part where the error occurred, if known.
92        part: Option<String>,
93    },
94
95    /// An arithmetic operator received a value that is not a number.
96    #[error("cannot apply '{operator}' to {actual}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
97    ArithmeticType {
98        /// The operator as written in the template.
99        operator: String,
100        /// The type that is not a number.
101        actual: String,
102        /// DOCX part where the error occurred, if known.
103        part: Option<String>,
104    },
105
106    /// Division or remainder by zero.
107    #[error("cannot apply '{operator}' by zero{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
108    DivisionByZero {
109        /// The operator as written in the template.
110        operator: String,
111        /// DOCX part where the error occurred, if known.
112        part: Option<String>,
113    },
114
115    /// Arithmetic produced a value outside the supported number range.
116    #[error("arithmetic '{operator}' produced a value outside the supported range{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
117    ArithmeticOverflow {
118        /// The operator as written in the template.
119        operator: String,
120        /// DOCX part where the error occurred, if known.
121        part: Option<String>,
122    },
123
124    /// A number literal in the template is not a valid number.
125    #[error("invalid number literal '{literal}'{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
126    InvalidLiteral {
127        /// The literal as written in the template.
128        literal: String,
129        /// DOCX part where the error occurred, if known.
130        part: Option<String>,
131    },
132
133    /// An expression cannot be used in this position.
134    #[error("{message}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
135    ExpressionNotSupported {
136        /// Explanation of the unsupported use.
137        message: String,
138        /// DOCX part where the error occurred, if known.
139        part: Option<String>,
140    },
141
142    /// Block placement is not supported in DOCX structure.
143    #[error("unsupported block placement: {message}{}", .part.as_deref().map(|p| format!(" in {p}")).unwrap_or_default())]
144    UnsupportedBlockPlacement {
145        /// Description of why placement is unsupported.
146        message: String,
147        /// DOCX part where the error occurred, if known.
148        part: Option<String>,
149    },
150
151    /// A placeholder spans two inline containers.
152    #[error("text span in {part} at paragraph {paragraph:?} crosses inline containers")]
153    TextSpansContainers {
154        /// The part name.
155        part: String,
156        /// The paragraph path.
157        paragraph: Vec<usize>,
158        /// The container identities the span touched.
159        containers: Vec<Option<usize>>,
160    },
161
162    /// A resolved edit points outside the region it belongs to.
163    #[error("plan target {path:?} is outside the region it belongs to")]
164    InvalidPlanTarget {
165        /// The path that could not be mapped.
166        path: Vec<usize>,
167    },
168
169    /// The evaluator produced edits that overlap.
170    #[error("invalid plan: {message}")]
171    InvalidPlan {
172        /// The plan validation message.
173        message: String,
174    },
175}
176
177impl From<crate::domain::plan::ApplyError> for RenderError {
178    fn from(error: crate::domain::plan::ApplyError) -> Self {
179        match error {
180            crate::domain::plan::ApplyError::MissingTarget { path }
181            | crate::domain::plan::ApplyError::MissingSlice { path } => {
182                RenderError::InvalidPlanTarget { path }
183            }
184            other => RenderError::InvalidPlan {
185                message: other.to_string(),
186            },
187        }
188    }
189}