Skip to content

Commit 7cf53ec

Browse files
committed
feat(path): Show location in parse error messages
1 parent 586306f commit 7cf53ec

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

src/path/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl FromStr for Expression {
2727
struct ParseError(String);
2828

2929
impl ParseError {
30-
fn new(inner: winnow::error::ContextError) -> Self {
30+
fn new(inner: winnow::error::ParseError<&str, winnow::error::ContextError>) -> Self {
3131
Self(inner.to_string())
3232
}
3333
}

src/path/parser.rs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use winnow::combinator::opt;
99
use winnow::combinator::repeat;
1010
use winnow::combinator::seq;
1111
use winnow::error::ContextError;
12+
use winnow::error::ParseError;
1213
use winnow::error::StrContext;
1314
use winnow::error::StrContextValue;
1415
use winnow::prelude::*;
@@ -17,9 +18,8 @@ use winnow::token::take_while;
1718

1819
use crate::path::Expression;
1920

20-
pub(crate) fn from_str(mut input: &str) -> Result<Expression, ContextError> {
21-
let input = &mut input;
22-
path.parse(input).map_err(|e| e.into_inner())
21+
pub(crate) fn from_str(input: &str) -> Result<Expression, ParseError<&str, ContextError>> {
22+
path.parse(input)
2323
}
2424

2525
fn path(i: &mut &str) -> PResult<Expression> {
@@ -93,6 +93,8 @@ fn integer(i: &mut &str) -> PResult<isize> {
9393

9494
#[cfg(test)]
9595
mod test {
96+
use snapbox::{assert_data_eq, str};
97+
9698
use super::Expression::*;
9799
use super::*;
98100

@@ -143,36 +145,70 @@ mod test {
143145
#[test]
144146
fn test_invalid_identifier() {
145147
let err = from_str("!").unwrap_err();
146-
assert_eq!(
147-
"invalid identifier\nexpected ASCII alphanumeric, `_`, `-`",
148-
err.to_string()
148+
assert_data_eq!(
149+
err.to_string(),
150+
str![[r#"
151+
!
152+
^
153+
invalid identifier
154+
expected ASCII alphanumeric, `_`, `-`
155+
"#]]
149156
);
150157
}
151158

152159
#[test]
153160
fn test_invalid_child() {
154161
let err = from_str("a..").unwrap_err();
155-
assert_eq!(
156-
"invalid identifier\nexpected ASCII alphanumeric, `_`, `-`",
157-
err.to_string()
162+
assert_data_eq!(
163+
err.to_string(),
164+
str![[r#"
165+
a..
166+
^
167+
invalid identifier
168+
expected ASCII alphanumeric, `_`, `-`
169+
"#]]
158170
);
159171
}
160172

161173
#[test]
162174
fn test_invalid_subscript() {
163175
let err = from_str("a[b]").unwrap_err();
164-
assert_eq!("invalid subscript\nexpected integer", err.to_string());
176+
assert_data_eq!(
177+
err.to_string(),
178+
str![[r#"
179+
a[b]
180+
^
181+
invalid subscript
182+
expected integer
183+
"#]]
184+
);
165185
}
166186

167187
#[test]
168188
fn test_incomplete_subscript() {
169189
let err = from_str("a[0").unwrap_err();
170-
assert_eq!("invalid subscript\nexpected `]`", err.to_string());
190+
assert_data_eq!(
191+
err.to_string(),
192+
str![[r#"
193+
a[0
194+
^
195+
invalid subscript
196+
expected `]`
197+
"#]]
198+
);
171199
}
172200

173201
#[test]
174202
fn test_invalid_postfix() {
175203
let err = from_str("a!b").unwrap_err();
176-
assert_eq!("invalid postfix\nexpected `[`, `.`", err.to_string());
204+
assert_data_eq!(
205+
err.to_string(),
206+
str![[r#"
207+
a!b
208+
^
209+
invalid postfix
210+
expected `[`, `.`
211+
"#]]
212+
);
177213
}
178214
}

0 commit comments

Comments
 (0)