Skip to content

Commit ebb257b

Browse files
Improve error for type mismatch
1 parent 27a99d9 commit ebb257b

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

engine/src/rhs_types/bytes.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use crate::{Type, TypeMismatchError};
12
use crate::{
23
lex::{Lex, LexErrorKind, LexResult, take},
34
strict_partial_ord::StrictPartialOrd,
45
};
56
use serde::{Serialize, Serializer};
7+
use std::num::IntErrorKind;
68
use std::{
79
fmt::{self, Debug, Formatter},
810
hash::{Hash, Hasher},
@@ -160,6 +162,26 @@ fn fixed_byte(input: &str, digits: usize, radix: u32) -> LexResult<'_, u8> {
160162
let (digits, rest) = take(input, digits)?;
161163
match u8::from_str_radix(digits, radix) {
162164
Ok(b) => Ok((b, rest)),
165+
Err(err)
166+
if err.kind() == &IntErrorKind::InvalidDigit
167+
&& !digits.chars().any(|c| {
168+
if radix == 8 {
169+
matches!(c, '0'..='7')
170+
} else if radix == 16 {
171+
c.is_ascii_hexdigit()
172+
} else {
173+
false
174+
}
175+
}) =>
176+
{
177+
Err((
178+
LexErrorKind::TypeMismatch(TypeMismatchError {
179+
expected: Type::Int.into(),
180+
actual: Type::Bytes,
181+
}),
182+
digits,
183+
))
184+
}
163185
Err(err) => Err((LexErrorKind::ParseInt { err, radix }, digits)),
164186
}
165187
}

engine/src/scheme.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,7 @@ fn test_parse_error() {
12681268

12691269
#[test]
12701270
fn test_parse_error_in_op() {
1271+
use crate::TypeMismatchError;
12711272
use cidr::errors::NetworkParseError;
12721273
use indoc::indoc;
12731274
use std::{net::IpAddr, str::FromStr};
@@ -1479,6 +1480,33 @@ fn test_parse_error_in_op() {
14791480
);
14801481
}
14811482

1483+
{
1484+
let err = scheme.parse("str in {gh}").unwrap_err();
1485+
assert_eq!(
1486+
err,
1487+
ParseError {
1488+
kind: LexErrorKind::TypeMismatch(TypeMismatchError {
1489+
expected: Type::Int.into(),
1490+
actual: Type::Bytes,
1491+
}),
1492+
input: "str in {gh}",
1493+
line_number: 0,
1494+
span_start: 8,
1495+
span_len: 2
1496+
}
1497+
);
1498+
assert_eq!(
1499+
err.to_string(),
1500+
indoc!(
1501+
r#"
1502+
Filter parsing error (1:9):
1503+
str in {gh}
1504+
^^ expected value of type Int, but got Bytes
1505+
"#
1506+
)
1507+
);
1508+
}
1509+
14821510
{
14831511
let err = scheme.parse("str in {127.0.0.1}").unwrap_err();
14841512
assert_eq!(

0 commit comments

Comments
 (0)