Skip to content

Commit

Permalink
Merge pull request #99 from GuillaumeGomez/small-improvement
Browse files Browse the repository at this point in the history
Change the error message to make it start at the `as` keyword
  • Loading branch information
Kijewski authored Jul 28, 2024
2 parents bf03e44 + e74f0d2 commit 85fed57
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
10 changes: 5 additions & 5 deletions rinja_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ impl<'a> Expr<'a> {

fn is_as(i: &'a str, level: Level) -> ParseResult<'a, WithSpan<'a, Self>> {
let start = i;
let (i, lhs) = Self::filtered(i, level)?;
let (j, rhs) = opt(ws(identifier))(i)?;
let (before_keyword, lhs) = Self::filtered(i, level)?;
let (j, rhs) = opt(ws(identifier))(before_keyword)?;
let i = match rhs {
Some("is") => j,
Some("as") => {
Expand All @@ -207,19 +207,19 @@ impl<'a> Expr<'a> {
} else if target.is_empty() {
return Err(nom::Err::Failure(ErrorContext::new(
"`as` operator expects the name of a primitive type on its right-hand side",
start,
before_keyword.trim_start(),
)));
} else {
return Err(nom::Err::Failure(ErrorContext::new(
format!(
"`as` operator expects the name of a primitive type on its right-hand \
side, found `{target}`"
),
start,
before_keyword.trim_start(),
)));
}
}
_ => return Ok((i, lhs)),
_ => return Ok((before_keyword, lhs)),
};

let (i, rhs) = opt(terminated(opt(keyword("not")), ws(keyword("defined"))))(i)?;
Expand Down
4 changes: 4 additions & 0 deletions testing/tests/ui/as-primitive-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ struct D;
#[template(source = r#"{{ 1234 as int32_t }}"#, ext = "html")]
struct E;

#[derive(Template)]
#[template(source = r#"{{ (1234 + 4 * 12 / 45675445 - 13) as int32_t }}"#, ext = "html")]
struct F;

fn main() {
}
30 changes: 20 additions & 10 deletions testing/tests/ui/as-primitive-type.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error: `as` operator expects the name of a primitive type on its right-hand side
failed to parse template source at row 1, column 3 near:
"1234 as 4567 }}"
failed to parse template source at row 1, column 8 near:
"as 4567 }}"
--> tests/ui/as-primitive-type.rs:3:10
|
3 | #[derive(Template)]
Expand All @@ -9,8 +9,8 @@ error: `as` operator expects the name of a primitive type on its right-hand side
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `as` operator expects the name of a primitive type on its right-hand side
failed to parse template source at row 1, column 3 near:
"1234 as ? }}"
failed to parse template source at row 1, column 8 near:
"as ? }}"
--> tests/ui/as-primitive-type.rs:7:10
|
7 | #[derive(Template)]
Expand All @@ -19,8 +19,8 @@ error: `as` operator expects the name of a primitive type on its right-hand side
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `as` operator expects the name of a primitive type on its right-hand side, found `u1234`
failed to parse template source at row 1, column 3 near:
"1234 as u1234 }}"
failed to parse template source at row 1, column 8 near:
"as u1234 }}"
--> tests/ui/as-primitive-type.rs:11:10
|
11 | #[derive(Template)]
Expand All @@ -29,8 +29,8 @@ error: `as` operator expects the name of a primitive type on its right-hand side
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `as` operator expects the name of a primitive type on its right-hand side, found `core`
failed to parse template source at row 1, column 3 near:
"1234 as core::primitive::u32 }}"
failed to parse template source at row 1, column 8 near:
"as core::primitive::u32 }}"
--> tests/ui/as-primitive-type.rs:15:10
|
15 | #[derive(Template)]
Expand All @@ -39,11 +39,21 @@ error: `as` operator expects the name of a primitive type on its right-hand side
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `as` operator expects the name of a primitive type on its right-hand side, found `int32_t`
failed to parse template source at row 1, column 3 near:
"1234 as int32_t }}"
failed to parse template source at row 1, column 8 near:
"as int32_t }}"
--> tests/ui/as-primitive-type.rs:19:10
|
19 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `as` operator expects the name of a primitive type on its right-hand side, found `int32_t`
failed to parse template source at row 1, column 35 near:
"as int32_t }}"
--> tests/ui/as-primitive-type.rs:23:10
|
23 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 85fed57

Please sign in to comment.