Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 69c4bf8

Browse files
committed
parser: track ErrorKind in ErrorContext
1 parent 5771fea commit 69c4bf8

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

askama_parser/src/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ impl<'a> Expr<'a> {
115115
message: Some(Cow::Borrowed(
116116
"named arguments must always be passed last",
117117
)),
118+
kind: ErrorKind::Fail,
118119
}))
119120
} else {
120121
Ok((i, expr))
@@ -150,6 +151,7 @@ impl<'a> Expr<'a> {
150151
message: Some(Cow::Owned(format!(
151152
"named argument `{argument}` was passed more than once"
152153
))),
154+
kind: ErrorKind::Count,
153155
}))
154156
}
155157
}

askama_parser/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,15 @@ pub(crate) type ParseResult<'a, T = &'a str> = Result<(&'a str, T), nom::Err<Err
140140
pub(crate) struct ErrorContext<'a> {
141141
pub(crate) input: &'a str,
142142
pub(crate) message: Option<Cow<'static, str>>,
143+
pub(crate) kind: ErrorKind,
143144
}
144145

145146
impl<'a> nom::error::ParseError<&'a str> for ErrorContext<'a> {
146-
fn from_error_kind(input: &'a str, _code: ErrorKind) -> Self {
147+
fn from_error_kind(input: &'a str, kind: ErrorKind) -> Self {
147148
Self {
148149
input,
149150
message: None,
151+
kind,
150152
}
151153
}
152154

@@ -156,10 +158,11 @@ impl<'a> nom::error::ParseError<&'a str> for ErrorContext<'a> {
156158
}
157159

158160
impl<'a, E: std::fmt::Display> FromExternalError<&'a str, E> for ErrorContext<'a> {
159-
fn from_external_error(input: &'a str, _kind: ErrorKind, e: E) -> Self {
161+
fn from_external_error(input: &'a str, kind: ErrorKind, e: E) -> Self {
160162
Self {
161163
input,
162164
message: Some(Cow::Owned(e.to_string())),
165+
kind,
163166
}
164167
}
165168
}
@@ -168,13 +171,15 @@ impl<'a> ErrorContext<'a> {
168171
pub(crate) fn from_err(error: nom::Err<Error<&'a str>>) -> nom::Err<Self> {
169172
match error {
170173
nom::Err::Incomplete(i) => nom::Err::Incomplete(i),
171-
nom::Err::Failure(Error { input, .. }) => nom::Err::Failure(Self {
174+
nom::Err::Failure(Error { input, code }) => nom::Err::Failure(Self {
172175
input,
173176
message: None,
177+
kind: code,
174178
}),
175-
nom::Err::Error(Error { input, .. }) => nom::Err::Error(Self {
179+
nom::Err::Error(Error { input, code }) => nom::Err::Error(Self {
176180
input,
177181
message: None,
182+
kind: code,
178183
}),
179184
}
180185
}

askama_parser/src/node.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ impl<'a> Target<'a> {
263263
"self" | "writer" => Err(nom::Err::Failure(ErrorContext {
264264
input,
265265
message: Some(Cow::Owned(format!("Cannot use `{name}` as a name"))),
266+
kind: ErrorKind::NoneOf,
266267
})),
267268
_ => Ok(Self::Name(name)),
268269
}
@@ -343,6 +344,7 @@ impl<'a> Cond<'a> {
343344
message: Some(Cow::Borrowed(
344345
"unknown `elif` keyword; did you mean `else if`?",
345346
)),
347+
kind: ErrorKind::Tag,
346348
}))
347349
}))),
348350
cut(tuple((
@@ -789,6 +791,7 @@ fn check_end_name<'a>(
789791
Err(nom::Err::Failure(ErrorContext {
790792
input: before,
791793
message: Some(Cow::Owned(message)),
794+
kind: ErrorKind::Tag,
792795
}))
793796
}
794797

@@ -991,6 +994,7 @@ impl<'a> Extends<'a> {
991994
message: Some(Cow::Borrowed(
992995
"whitespace control is not allowed on `extends`",
993996
)),
997+
kind: ErrorKind::NoneOf,
994998
})),
995999
}
9961000
}

0 commit comments

Comments
 (0)