1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! 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(),
},
}
}
}