Skip to content

Commit 88d15e0

Browse files
authored
Merge branch 'master' into functions-hrl
2 parents 914f227 + 1202313 commit 88d15e0

File tree

4 files changed

+77
-11
lines changed

4 files changed

+77
-11
lines changed

engine/src/functions/concat.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ fn concat_bytes<'a>(mut accumulator: Cow<'a, [u8]>, args: FunctionArgs<'_, 'a>)
4141
accumulator
4242
}
4343

44-
const EXPECTED_TYPES: [ExpectedType; 2] = [ExpectedType::Array, ExpectedType::Type(Type::Bytes)];
44+
pub(crate) const EXPECTED_TYPES: [ExpectedType; 2] =
45+
[ExpectedType::Array, ExpectedType::Type(Type::Bytes)];
4546

4647
impl FunctionDefinition for ConcatFunction {
4748
fn check_param(

engine/src/functions/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
mod all;
2-
mod any;
3-
mod concat;
1+
pub(crate) mod all;
2+
pub(crate) mod any;
3+
pub(crate) mod concat;
44

55
use crate::{
66
filter::CompiledValueResult,

engine/src/scheme.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,20 @@ macro_rules! Scheme {
628628

629629
#[test]
630630
fn test_parse_error() {
631-
use crate::types::TypeMismatchError;
631+
use crate::types::{ExpectedTypeList, TypeMismatchError};
632+
use crate::ConcatFunction;
632633
use indoc::indoc;
633634

634-
let scheme = &Scheme! {
635+
let mut scheme = Scheme! {
635636
num: Int,
636637
str: Bytes,
637638
arr: Array(Bool),
638639
};
639640

641+
scheme
642+
.add_function("concat", ConcatFunction::new())
643+
.unwrap();
644+
640645
{
641646
let err = scheme.parse("xyz").unwrap_err();
642647
assert_eq!(
@@ -768,7 +773,7 @@ fn test_parse_error() {
768773
r#"
769774
Filter parsing error (1:12):
770775
arr and arr
771-
^ expected value of type {Type(Bool)}, but got Array(Bool)
776+
^ expected value of type Bool, but got Array<Bool>
772777
"#
773778
)
774779
);
@@ -795,7 +800,7 @@ fn test_parse_error() {
795800
r#"
796801
Filter parsing error (1:2):
797802
arr[*]
798-
^^^^^^ expected value of type {Type(Bool)}, but got Array(Bool)
803+
^^^^^^ expected value of type Bool, but got Array<Bool>
799804
"#
800805
)
801806
);
@@ -872,6 +877,38 @@ fn test_parse_error() {
872877
)
873878
);
874879
}
880+
881+
{
882+
let err = scheme.parse(indoc!(r"concat(0, 0) == 0")).unwrap_err();
883+
assert_eq!(
884+
err,
885+
ParseError {
886+
kind: LexErrorKind::InvalidArgumentType {
887+
index: 0,
888+
mismatch: TypeMismatchError {
889+
expected: ExpectedTypeList::from(
890+
crate::functions::concat::EXPECTED_TYPES.into_iter()
891+
),
892+
actual: Type::Int,
893+
},
894+
},
895+
input: "concat(0, 0) == 0",
896+
line_number: 0,
897+
span_start: 7,
898+
span_len: 1,
899+
}
900+
);
901+
assert_eq!(
902+
err.to_string(),
903+
indoc!(
904+
r#"
905+
Filter parsing error (1:8):
906+
concat(0, 0) == 0
907+
^ invalid type of argument #0: expected value of type Bytes or Array<_>, but got Int
908+
"#
909+
)
910+
);
911+
}
875912
}
876913

877914
#[test]

engine/src/types.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ impl From<Type> for ExpectedType {
5656
}
5757
}
5858

59+
impl std::fmt::Display for ExpectedType {
60+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
61+
match self {
62+
ExpectedType::Array => write!(f, "Array<_>"),
63+
ExpectedType::Map => write!(f, "Map<_>"),
64+
ExpectedType::Type(ty) => write!(f, "{}", ty),
65+
}
66+
}
67+
}
68+
5969
/// A list of expected types.
6070
#[derive(Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
6171
pub struct ExpectedTypeList(BTreeSet<ExpectedType>);
@@ -96,7 +106,25 @@ impl<T: Iterator<Item = ExpectedType>> From<T> for ExpectedTypeList {
96106

97107
impl std::fmt::Display for ExpectedTypeList {
98108
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99-
self.0.fmt(f)
109+
match self.0.len() {
110+
0 => unreachable!(),
111+
1 => write!(f, "{}", self.0.first().unwrap()),
112+
2 => write!(
113+
f,
114+
"{} or {}",
115+
self.0.first().unwrap(),
116+
self.0.last().unwrap()
117+
),
118+
_ => {
119+
let mut iter = self.0.iter();
120+
let first = iter.next().unwrap();
121+
write!(f, "{{{}", first)?;
122+
for expected_type in iter {
123+
write!(f, ", {}", expected_type)?;
124+
}
125+
write!(f, "}}")
126+
}
127+
}
100128
}
101129
}
102130

@@ -399,8 +427,8 @@ impl std::fmt::Display for Type {
399427
Self::Bytes => write!(f, "Bytes"),
400428
Self::Int => write!(f, "Int"),
401429
Self::Ip => write!(f, "Ip"),
402-
Self::Array(ty) => write!(f, "Array({})", Type::from(*ty)),
403-
Self::Map(ty) => write!(f, "Map({})", Type::from(*ty)),
430+
Self::Array(ty) => write!(f, "Array<{}>", Type::from(*ty)),
431+
Self::Map(ty) => write!(f, "Map<{}>", Type::from(*ty)),
404432
}
405433
}
406434
}

0 commit comments

Comments
 (0)