diff --git a/filter-parser/src/lib.rs b/filter-parser/src/lib.rs index 0ae8a1532..11ffbb148 100644 --- a/filter-parser/src/lib.rs +++ b/filter-parser/src/lib.rs @@ -472,6 +472,77 @@ pub fn parse_filter(input: Span) -> IResult { terminated(|input| parse_expression(input, 0), eof)(input) } +impl<'a> std::fmt::Display for FilterCondition<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + FilterCondition::Not(filter) => { + write!(f, "NOT ({filter})") + } + FilterCondition::Condition { fid, op } => { + write!(f, "{fid} {op}") + } + FilterCondition::In { fid, els } => { + write!(f, "{fid} IN[")?; + for el in els { + write!(f, "{el}, ")?; + } + write!(f, "]") + } + FilterCondition::Or(els) => { + write!(f, "OR[")?; + for el in els { + write!(f, "{el}, ")?; + } + write!(f, "]") + } + FilterCondition::And(els) => { + write!(f, "AND[")?; + for el in els { + write!(f, "{el}, ")?; + } + write!(f, "]") + } + FilterCondition::GeoLowerThan { point, radius } => { + write!(f, "_geoRadius({}, {}, {})", point[0], point[1], radius) + } + FilterCondition::GeoBoundingBox { + top_right_point: top_left_point, + bottom_left_point: bottom_right_point, + } => { + write!( + f, + "_geoBoundingBox([{}, {}], [{}, {}])", + top_left_point[0], + top_left_point[1], + bottom_right_point[0], + bottom_right_point[1] + ) + } + } + } +} +impl<'a> std::fmt::Display for Condition<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Condition::GreaterThan(token) => write!(f, "> {token}"), + Condition::GreaterThanOrEqual(token) => write!(f, ">= {token}"), + Condition::Equal(token) => write!(f, "= {token}"), + Condition::NotEqual(token) => write!(f, "!= {token}"), + Condition::Null => write!(f, "IS NULL"), + Condition::Empty => write!(f, "IS EMPTY"), + Condition::Exists => write!(f, "EXISTS"), + Condition::LowerThan(token) => write!(f, "< {token}"), + Condition::LowerThanOrEqual(token) => write!(f, "<= {token}"), + Condition::Between { from, to } => write!(f, "{from} TO {to}"), + } + } +} +impl<'a> std::fmt::Display for Token<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{{{}}}", self.value()) + } +} + #[cfg(test)] pub mod tests { use super::*; @@ -852,74 +923,3 @@ pub mod tests { assert_eq!(token.value(), s); } } - -impl<'a> std::fmt::Display for FilterCondition<'a> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - FilterCondition::Not(filter) => { - write!(f, "NOT ({filter})") - } - FilterCondition::Condition { fid, op } => { - write!(f, "{fid} {op}") - } - FilterCondition::In { fid, els } => { - write!(f, "{fid} IN[")?; - for el in els { - write!(f, "{el}, ")?; - } - write!(f, "]") - } - FilterCondition::Or(els) => { - write!(f, "OR[")?; - for el in els { - write!(f, "{el}, ")?; - } - write!(f, "]") - } - FilterCondition::And(els) => { - write!(f, "AND[")?; - for el in els { - write!(f, "{el}, ")?; - } - write!(f, "]") - } - FilterCondition::GeoLowerThan { point, radius } => { - write!(f, "_geoRadius({}, {}, {})", point[0], point[1], radius) - } - FilterCondition::GeoBoundingBox { - top_right_point: top_left_point, - bottom_left_point: bottom_right_point, - } => { - write!( - f, - "_geoBoundingBox([{}, {}], [{}, {}])", - top_left_point[0], - top_left_point[1], - bottom_right_point[0], - bottom_right_point[1] - ) - } - } - } -} -impl<'a> std::fmt::Display for Condition<'a> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Condition::GreaterThan(token) => write!(f, "> {token}"), - Condition::GreaterThanOrEqual(token) => write!(f, ">= {token}"), - Condition::Equal(token) => write!(f, "= {token}"), - Condition::NotEqual(token) => write!(f, "!= {token}"), - Condition::Null => write!(f, "IS NULL"), - Condition::Empty => write!(f, "IS EMPTY"), - Condition::Exists => write!(f, "EXISTS"), - Condition::LowerThan(token) => write!(f, "< {token}"), - Condition::LowerThanOrEqual(token) => write!(f, "<= {token}"), - Condition::Between { from, to } => write!(f, "{from} TO {to}"), - } - } -} -impl<'a> std::fmt::Display for Token<'a> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{{{}}}", self.value()) - } -} diff --git a/meili-snap/src/lib.rs b/meili-snap/src/lib.rs index dea0d750b..32d3385d9 100644 --- a/meili-snap/src/lib.rs +++ b/meili-snap/src/lib.rs @@ -199,6 +199,30 @@ macro_rules! snapshot { }; } +/// Create a string from the value by serializing it as Json, optionally +/// redacting some parts of it. +/// +/// The second argument to the macro can be an object expression for redaction. +/// It's in the form { selector => replacement }. For more information about redactions +/// refer to the redactions feature in the `insta` guide. +#[macro_export] +macro_rules! json_string { + ($value:expr, {$($k:expr => $v:expr),*$(,)?}) => { + { + let (_, snap) = meili_snap::insta::_prepare_snapshot_for_redaction!($value, {$($k => $v),*}, Json, File); + snap + } + }; + ($value:expr) => {{ + let value = meili_snap::insta::_macro_support::serialize_value( + &$value, + meili_snap::insta::_macro_support::SerializationFormat::Json, + meili_snap::insta::_macro_support::SnapshotLocation::File + ); + value + }}; +} + #[cfg(test)] mod tests { use crate as meili_snap; @@ -250,27 +274,3 @@ mod tests { } } } - -/// Create a string from the value by serializing it as Json, optionally -/// redacting some parts of it. -/// -/// The second argument to the macro can be an object expression for redaction. -/// It's in the form { selector => replacement }. For more information about redactions -/// refer to the redactions feature in the `insta` guide. -#[macro_export] -macro_rules! json_string { - ($value:expr, {$($k:expr => $v:expr),*$(,)?}) => { - { - let (_, snap) = meili_snap::insta::_prepare_snapshot_for_redaction!($value, {$($k => $v),*}, Json, File); - snap - } - }; - ($value:expr) => {{ - let value = meili_snap::insta::_macro_support::serialize_value( - &$value, - meili_snap::insta::_macro_support::SerializationFormat::Json, - meili_snap::insta::_macro_support::SnapshotLocation::File - ); - value - }}; -}