From 7c07312c3da7e4d6eb6ad379df8df29040c751b3 Mon Sep 17 00:00:00 2001 From: Elie ROUDNINSKI Date: Thu, 10 Sep 2026 15:03:27 +0200 Subject: [PATCH] Rename RHS value types to literal types --- engine/src/ast/field_expr.rs | 101 +++++++++++++------------- engine/src/ast/function_expr.rs | 36 +++++----- engine/src/functions/mod.rs | 8 +-- engine/src/lib.rs | 4 +- engine/src/rhs_types/array.rs | 2 +- engine/src/rhs_types/bool.rs | 2 +- engine/src/rhs_types/map.rs | 2 +- engine/src/scheme.rs | 14 ++-- engine/src/types.rs | 122 ++++++++++++++++---------------- 9 files changed, 147 insertions(+), 144 deletions(-) diff --git a/engine/src/ast/field_expr.rs b/engine/src/ast/field_expr.rs index e908752f..88e1061e 100644 --- a/engine/src/ast/field_expr.rs +++ b/engine/src/ast/field_expr.rs @@ -11,7 +11,7 @@ use crate::rhs_types::{BytesExpr, ExplicitIpRange, ListName, Regex, Wildcard}; use crate::scheme::{Field, Identifier, List}; use crate::searcher::{EmptySearcher, MemmemSearcher}; use crate::strict_partial_ord::StrictPartialOrd; -use crate::types::{GetType, LhsValue, RhsValue, RhsValues, Type}; +use crate::types::{GetType, LhsValue, LiteralSet, LiteralValue, Type}; use crate::{ExecutionContext, Scheme}; use serde::{Serialize, Serializer}; use sliceslice::MemchrSearcher; @@ -130,7 +130,7 @@ pub enum ComparisonOpExpr { /// * "lt" | "<" op: OrderingOp, /// Right-hand side literal - rhs: RhsValue, + rhs: LiteralValue, }, /// Integer comparison @@ -160,7 +160,7 @@ pub enum ComparisonOpExpr { /// "in {...}" comparison #[serde(serialize_with = "serialize_one_of")] - OneOf(RhsValues), + OneOf(LiteralSet), /// "contains {...}" comparison #[serde(serialize_with = "serialize_contains_one_of")] @@ -216,7 +216,7 @@ fn serialize_strict_wildcard( serialize_op_rhs("Strict Wildcard", rhs, ser) } -fn serialize_one_of(rhs: &RhsValues, ser: S) -> Result { +fn serialize_one_of(rhs: &LiteralSet, ser: S) -> Result { serialize_op_rhs("OneOf", rhs, ser) } @@ -353,14 +353,14 @@ impl ComparisonExpr { input, ) } else { - let (rhs, input) = RhsValues::lex_with(input, lhs_type)?; + let (rhs, input) = LiteralSet::lex_with(input, lhs_type)?; (ComparisonOpExpr::OneOf(rhs), input) } } (Type::Ip, ComparisonOp::Ordering(op)) | (Type::Bytes, ComparisonOp::Ordering(op)) | (Type::Int, ComparisonOp::Ordering(op)) => { - let (rhs, input) = RhsValue::lex_with(input, lhs_type)?; + let (rhs, input) = LiteralValue::lex_with(input, lhs_type)?; (ComparisonOpExpr::Ordering { op, rhs }, input) } (Type::Int, ComparisonOp::Int(op)) => { @@ -468,7 +468,7 @@ impl Expr for ComparisonExpr { macro_rules! gen_ordering { ($op:tt, $def:ident) => { match rhs { - RhsValue::Bytes(bytes) => { + LiteralValue::Bytes(bytes) => { struct BytesOp(BytesExpr); impl Compare for BytesOp { @@ -480,7 +480,7 @@ impl Expr for ComparisonExpr { lhs.compile_with(compiler, $def, BytesOp(bytes)) } - RhsValue::Int(int) => { + LiteralValue::Int(int) => { struct IntOp(i64); impl Compare for IntOp { @@ -492,7 +492,7 @@ impl Expr for ComparisonExpr { lhs.compile_with(compiler, $def, IntOp(int)) } - RhsValue::Ip(ip) => { + LiteralValue::Ip(ip) => { struct IpOp { op: OrderingOp, ip: IpAddr, @@ -507,7 +507,7 @@ impl Expr for ComparisonExpr { lhs.compile_with(compiler, $def, IpOp { op, ip }) } - RhsValue::Bool(_) | RhsValue::Array(_) | RhsValue::Map(_) => unreachable!(), + LiteralValue::Bool(_) | LiteralValue::Array(_) | LiteralValue::Map(_) => unreachable!(), } }; } @@ -691,7 +691,7 @@ impl Expr for ComparisonExpr { lhs.compile_with(compiler, false, wildcard) } ComparisonOpExpr::OneOf(values) => match values { - RhsValues::Ip(ranges) => { + LiteralSet::Ip(ranges) => { let mut v4 = Vec::new(); let mut v6 = Vec::new(); for range in ranges.into_iter() { @@ -724,7 +724,7 @@ impl Expr for ComparisonExpr { lhs.compile_with(compiler, false, OneOfIp { v4, v6 }) } - RhsValues::Int(values) => { + LiteralSet::Int(values) => { let values: RangeSet<_> = values.into_iter().map(Into::into).collect(); struct OneOfInt(RangeSet); @@ -742,7 +742,7 @@ impl Expr for ComparisonExpr { lhs.compile_with(compiler, false, OneOfInt(values)) } - RhsValues::Bytes(values) => { + LiteralSet::Bytes(values) => { let values: BTreeSet> = values.into_iter().map(Into::into).collect(); struct Contains(BTreeSet>); @@ -760,9 +760,9 @@ impl Expr for ComparisonExpr { lhs.compile_with(compiler, false, Contains(values)) } - RhsValues::Bool(_) => unreachable!(), - RhsValues::Map(_) => unreachable!(), - RhsValues::Array(_) => unreachable!(), + LiteralSet::Bool(_) => unreachable!(), + LiteralSet::Map(_) => unreachable!(), + LiteralSet::Array(_) => unreachable!(), }, ComparisonOpExpr::ContainsOneOf(_values) => { unreachable!("Node should not be constructed as there is no syntax to do so") @@ -1094,7 +1094,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::LessThanEqual, - rhs: RhsValue::Ip(IpAddr::from([ + rhs: LiteralValue::Ip(IpAddr::from([ 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80 ])) }, @@ -1149,7 +1149,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::GreaterThanEqual, - rhs: RhsValue::Bytes( + rhs: LiteralValue::Bytes( vec![0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80].into() ), }, @@ -1187,7 +1187,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::LessThan, - rhs: RhsValue::Bytes(vec![0x12, 0x13].into()), + rhs: LiteralValue::Bytes(vec![0x12, 0x13].into()), }, } ); @@ -1211,7 +1211,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("example.org".to_owned().into()) + rhs: LiteralValue::Bytes("example.org".to_owned().into()) } } ); @@ -1281,7 +1281,7 @@ mod tests { identifier: IdentifierExpr::Field(field("tcp.port").to_owned()), indexes: vec![], }, - op: ComparisonOpExpr::OneOf(RhsValues::Int(vec![ + op: ComparisonOpExpr::OneOf(LiteralSet::Int(vec![ 80.into(), 443.into(), (2082..=2083).into() @@ -1336,7 +1336,7 @@ mod tests { identifier: IdentifierExpr::Field(field("http.host").to_owned()), indexes: vec![], }, - op: ComparisonOpExpr::OneOf(RhsValues::Bytes( + op: ComparisonOpExpr::OneOf(LiteralSet::Bytes( ["example.org", "example.com",] .iter() .map(|s| (*s).to_string().into()) @@ -1383,7 +1383,7 @@ mod tests { identifier: IdentifierExpr::Field(field("ip.addr").to_owned()), indexes: vec![], }, - op: ComparisonOpExpr::OneOf(RhsValues::Ip(vec![ + op: ComparisonOpExpr::OneOf(LiteralSet::Ip(vec![ IpRange::Cidr(IpCidr::new([127, 0, 0, 0].into(), 8).unwrap()), IpRange::Cidr(IpCidr::new_host([0, 0, 0, 0, 0, 0, 0, 1].into())), IpRange::Explicit(ExplicitIpRange::V4( @@ -1509,7 +1509,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::LessThan, - rhs: RhsValue::Int(8000) + rhs: LiteralValue::Int(8000) }, } ); @@ -1625,7 +1625,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("example.org".to_owned().into()) + rhs: LiteralValue::Bytes("example.org".to_owned().into()) } } ); @@ -1681,7 +1681,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("example.org".to_owned().into()) + rhs: LiteralValue::Bytes("example.org".to_owned().into()) } } ); @@ -1730,7 +1730,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("example.org".to_owned().into()) + rhs: LiteralValue::Bytes("example.org".to_owned().into()) } } ); @@ -1766,7 +1766,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::NotEqual, - rhs: RhsValue::Bytes("example.org".to_owned().into()) + rhs: LiteralValue::Bytes("example.org".to_owned().into()) } } ); @@ -1802,7 +1802,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("example.org".to_owned().into()) + rhs: LiteralValue::Bytes("example.org".to_owned().into()) } } ); @@ -1838,7 +1838,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::NotEqual, - rhs: RhsValue::Bytes("example.org".to_owned().into()) + rhs: LiteralValue::Bytes("example.org".to_owned().into()) } } ); @@ -1881,7 +1881,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("example.org".to_owned().into()) + rhs: LiteralValue::Bytes("example.org".to_owned().into()) } } ); @@ -1929,7 +1929,7 @@ mod tests { identifier: IdentifierExpr::Field(field("http.host").to_owned()), indexes: vec![], }), - FunctionCallArgExpr::Literal(RhsValue::Bytes(BytesExpr::from( + FunctionCallArgExpr::Literal(LiteralValue::Bytes(BytesExpr::from( ".org".to_owned() ))), ], @@ -1939,7 +1939,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("example.org".to_owned().into()) + rhs: LiteralValue::Bytes("example.org".to_owned().into()) } } ); @@ -2007,7 +2007,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("three".to_owned().into()) + rhs: LiteralValue::Bytes("three".to_owned().into()) } } ); @@ -2069,7 +2069,7 @@ mod tests { identifier: IdentifierExpr::Field(field("http.cookies").to_owned()), indexes: vec![FieldIndex::MapEach], }), - FunctionCallArgExpr::Literal(RhsValue::Bytes(BytesExpr::from( + FunctionCallArgExpr::Literal(LiteralValue::Bytes(BytesExpr::from( "-cf".to_owned() ))), ], @@ -2079,7 +2079,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("three-cf".to_owned().into()) + rhs: LiteralValue::Bytes("three-cf".to_owned().into()) } } ); @@ -2138,7 +2138,7 @@ mod tests { identifier: IdentifierExpr::Field(field("http.headers").to_owned()), indexes: vec![FieldIndex::MapEach], }), - FunctionCallArgExpr::Literal(RhsValue::Bytes(BytesExpr::from( + FunctionCallArgExpr::Literal(LiteralValue::Bytes(BytesExpr::from( "-cf".to_owned() ))), ], @@ -2146,7 +2146,7 @@ mod tests { }), indexes: vec![FieldIndex::ArrayIndex(2)], }, - op: ComparisonOpExpr::OneOf(RhsValues::Bytes(vec![ + op: ComparisonOpExpr::OneOf(LiteralSet::Bytes(vec![ "one-cf".to_owned().into(), "two-cf".to_owned().into(), "three-cf".to_owned().into() @@ -2291,7 +2291,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("three".to_owned().into()) + rhs: LiteralValue::Bytes("three".to_owned().into()) } } ); @@ -2325,7 +2325,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("three".to_owned().into()) + rhs: LiteralValue::Bytes("three".to_owned().into()) } } ); @@ -2384,7 +2384,7 @@ mod tests { identifier: IdentifierExpr::Field(field("http.cookies").to_owned()), indexes: vec![FieldIndex::MapEach], }), - FunctionCallArgExpr::Literal(RhsValue::Bytes(BytesExpr::from( + FunctionCallArgExpr::Literal(LiteralValue::Bytes(BytesExpr::from( "-cf".to_owned() ))), ], @@ -2394,7 +2394,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("three-cf".to_owned().into()) + rhs: LiteralValue::Bytes("three-cf".to_owned().into()) } } ); @@ -2457,7 +2457,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::GreaterThan, - rhs: RhsValue::Int(3), + rhs: LiteralValue::Int(3), } } ); @@ -2732,7 +2732,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("[5][5]".to_owned().into()) + rhs: LiteralValue::Bytes("[5][5]".to_owned().into()) } } ); @@ -2759,7 +2759,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("[5][5]".to_owned().into()) + rhs: LiteralValue::Bytes("[5][5]".to_owned().into()) } } ); @@ -2786,7 +2786,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("[5][5]".to_owned().into()) + rhs: LiteralValue::Bytes("[5][5]".to_owned().into()) } } ); @@ -2863,7 +2863,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes(BytesExpr::new("ab".as_bytes(), BytesFormat::Raw(3))), + rhs: LiteralValue::Bytes(BytesExpr::new("ab".as_bytes(), BytesFormat::Raw(3))), }, } ); @@ -3019,7 +3019,7 @@ mod tests { identifier: IdentifierExpr::Field(field("http.host").to_owned()), indexes: vec![], }), - FunctionCallArgExpr::Literal(RhsValue::Bytes(BytesExpr::new( + FunctionCallArgExpr::Literal(LiteralValue::Bytes(BytesExpr::new( "cd".as_bytes(), BytesFormat::Raw(1) ))) @@ -3030,7 +3030,10 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes(BytesExpr::new("abcd".as_bytes(), BytesFormat::Raw(2))) + rhs: LiteralValue::Bytes(BytesExpr::new( + "abcd".as_bytes(), + BytesFormat::Raw(2) + )) } } ); diff --git a/engine/src/ast/function_expr.rs b/engine/src/ast/function_expr.rs index f6706efc..8161637c 100644 --- a/engine/src/ast/function_expr.rs +++ b/engine/src/ast/function_expr.rs @@ -14,7 +14,7 @@ use crate::functions::{ use crate::lex::{Lex, LexError, LexErrorKind, LexResult, LexWith, expect, skip_space, span}; use crate::lhs_types::Array; use crate::scheme::Function; -use crate::types::{GetType, LhsValue, RhsValue, Type}; +use crate::types::{GetType, LhsValue, LiteralValue, Type}; use serde::Serialize; use std::hash::{Hash, Hasher}; use std::iter::once; @@ -26,7 +26,7 @@ pub enum FunctionCallArgExpr { /// A sub-expression which evaluates to a value. IndexExpr(IndexExpr), /// A literal value. - Literal(RhsValue), + Literal(LiteralValue), /// A sub-expression which evaluates to either `true`/`false` /// or a list of `true`/`false`. It compiles to a [`CompiledExpr`] /// and is coerced into a [`CompiledValueExpr`]`. @@ -141,7 +141,7 @@ impl<'i, 's> LexWith<'i, &FilterParser<'s>> for FunctionCallArgExpr { let c2 = chars.next(); let c3 = chars.next(); if c == '"' || (c == 'r' && (c2 == Some('#') || c2 == Some('"'))) { - return RhsValue::lex_with(input, Type::Bytes) + return LiteralValue::lex_with(input, Type::Bytes) .map(|(literal, input)| (FunctionCallArgExpr::Literal(literal), input)); } else if c == '(' || UnaryOp::lex(input).is_ok() @@ -187,16 +187,16 @@ impl<'i, 's> LexWith<'i, &FilterParser<'s>> for FunctionCallArgExpr { } } - RhsValue::lex_with(input, Type::Ip) + LiteralValue::lex_with(input, Type::Ip) .map(|(literal, input)| (FunctionCallArgExpr::Literal(literal), input)) .or_else(|_| { - RhsValue::lex_with(input, Type::Int) + LiteralValue::lex_with(input, Type::Int) .map(|(literal, input)| (FunctionCallArgExpr::Literal(literal), input)) }) // try to parse Bytes after Int because digit literals < 255 are wrongly // interpreted as Bytes .or_else(|_| { - RhsValue::lex_with(input, Type::Bytes) + LiteralValue::lex_with(input, Type::Bytes) .map(|(literal, input)| (FunctionCallArgExpr::Literal(literal), input)) }) .map_err(|_| (LexErrorKind::EOF, _initial_input)) @@ -584,7 +584,7 @@ mod tests { }; use crate::rhs_types::{BytesExpr, BytesFormat}; use crate::scheme::{FieldIndex, IndexAccessError, Scheme}; - use crate::types::{RhsValues, Type, TypeMismatchError}; + use crate::types::{LiteralSet, Type, TypeMismatchError}; use std::convert::TryFrom; use std::sync::LazyLock; @@ -756,8 +756,8 @@ mod tests { ), indexes: vec![], }), - FunctionCallArgExpr::Literal(RhsValue::Int(1)), - FunctionCallArgExpr::Literal(RhsValue::Int(2)), + FunctionCallArgExpr::Literal(LiteralValue::Int(1)), + FunctionCallArgExpr::Literal(LiteralValue::Int(2)), ], context: None, }, @@ -831,8 +831,8 @@ mod tests { ), indexes: vec![], }), - FunctionCallArgExpr::Literal(RhsValue::Int(1)), - FunctionCallArgExpr::Literal(RhsValue::Int(2)), + FunctionCallArgExpr::Literal(LiteralValue::Int(1)), + FunctionCallArgExpr::Literal(LiteralValue::Int(2)), ], context: None, }, @@ -1079,7 +1079,7 @@ mod tests { }, op: ComparisonOpExpr::Ordering { op: OrderingOp::Equal, - rhs: RhsValue::Bytes("test".to_owned().into()) + rhs: LiteralValue::Bytes("test".to_owned().into()) } })), "" @@ -1193,7 +1193,7 @@ mod tests { ), indexes: vec![FieldIndex::MapEach], }, - op: ComparisonOpExpr::OneOf(RhsValues::Bytes(vec![ + op: ComparisonOpExpr::OneOf(LiteralSet::Bytes(vec![ "Cookie".to_owned().into(), "Cookies".to_owned().into(), ])), @@ -1250,7 +1250,7 @@ mod tests { ), indexes: vec![FieldIndex::MapEach], }, - op: ComparisonOpExpr::OneOf(RhsValues::Bytes(vec![ + op: ComparisonOpExpr::OneOf(LiteralSet::Bytes(vec![ "Cookie".to_owned().into(), "Cookies".to_owned().into(), ])), @@ -1301,8 +1301,8 @@ mod tests { identifier: IdentifierExpr::Field(SCHEME.get_field("http.host").unwrap().to_owned()), indexes: vec![], }), - FunctionCallArgExpr::Literal(RhsValue::Bytes(BytesExpr::new("this is a r##raw## string".as_bytes(), BytesFormat::Raw(0)))), - FunctionCallArgExpr::Literal(RhsValue::Bytes(BytesExpr::new("this is a new r##raw## string".as_bytes(), BytesFormat::Raw(0)))) + FunctionCallArgExpr::Literal(LiteralValue::Bytes(BytesExpr::new("this is a r##raw## string".as_bytes(), BytesFormat::Raw(0)))), + FunctionCallArgExpr::Literal(LiteralValue::Bytes(BytesExpr::new("this is a new r##raw## string".as_bytes(), BytesFormat::Raw(0)))) ], context: None, }, @@ -1342,8 +1342,8 @@ mod tests { identifier: IdentifierExpr::Field(SCHEME.get_field("http.host").unwrap().to_owned()), indexes: vec![], }), - FunctionCallArgExpr::Literal(RhsValue::Bytes(BytesExpr::new("this is a r##\"raw\"## string".as_bytes(), BytesFormat::Raw(3)))), - FunctionCallArgExpr::Literal(RhsValue::Bytes(BytesExpr::new("this is a new r##\"raw\"## string".as_bytes(), BytesFormat::Raw(3)))) + FunctionCallArgExpr::Literal(LiteralValue::Bytes(BytesExpr::new("this is a r##\"raw\"## string".as_bytes(), BytesFormat::Raw(3)))), + FunctionCallArgExpr::Literal(LiteralValue::Bytes(BytesExpr::new("this is a new r##\"raw\"## string".as_bytes(), BytesFormat::Raw(3)))) ], context: None, }, diff --git a/engine/src/functions/mod.rs b/engine/src/functions/mod.rs index e236a0f9..851b3eb7 100644 --- a/engine/src/functions/mod.rs +++ b/engine/src/functions/mod.rs @@ -4,7 +4,7 @@ pub use self::concat::ConcatFunction; use crate::ParserSettings; use crate::filter::CompiledValueResult; use crate::types::{ - ExpectedType, ExpectedTypeList, GetType, LhsValue, RhsValue, Type, TypeMismatchError, + ExpectedType, ExpectedTypeList, GetType, LhsValue, LiteralValue, Type, TypeMismatchError, }; use std::any::Any; use std::convert::TryFrom; @@ -174,7 +174,7 @@ impl From for FunctionParamError { #[derive(Clone, Debug)] pub enum FunctionParam<'a> { /// Contant function parameter (literal value) - Constant(&'a RhsValue), + Constant(&'a LiteralValue), /// Variable function parameter (field, or complex expressions) Variable(Type), } @@ -196,7 +196,7 @@ impl GetType for FunctionParam<'_> { impl<'a> FunctionParam<'a> { /// Returns the underlying value if the current parameter is a constant, otherwise an error. - pub fn as_constant(&self) -> Result<&'a RhsValue, FunctionArgKindMismatchError> { + pub fn as_constant(&self) -> Result<&'a LiteralValue, FunctionArgKindMismatchError> { match self { Self::Constant(value) => Ok(value), Self::Variable(_) => Err(FunctionArgKindMismatchError { @@ -255,7 +255,7 @@ impl<'a> FunctionParam<'a> { /// Checks that the parameter is a constant of a certain type /// and call the closure `op` to verify its value pub fn expect_const_value< - U: TryFrom<&'a RhsValue, Error = TypeMismatchError>, + U: TryFrom<&'a LiteralValue, Error = TypeMismatchError>, F: FnOnce(U) -> Result<(), String>, >( &self, diff --git a/engine/src/lib.rs b/engine/src/lib.rs index 3f1b5c60..8ea557f6 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -122,6 +122,6 @@ pub use self::scheme::{ SchemeBuilder, SchemeMismatchError, UnknownFieldError, }; pub use self::types::{ - CompoundType, ExpectedType, ExpectedTypeList, GetType, LhsValue, RhsValue, RhsValues, Type, - TypeMismatchError, + CompoundType, ExpectedType, ExpectedTypeList, GetType, LhsValue, LiteralSet, LiteralValue, + Type, TypeMismatchError, }; diff --git a/engine/src/rhs_types/array.rs b/engine/src/rhs_types/array.rs index f3ef2771..d5fe9750 100644 --- a/engine/src/rhs_types/array.rs +++ b/engine/src/rhs_types/array.rs @@ -7,7 +7,7 @@ use std::borrow::Borrow; use std::cmp::Ordering; /// [Uninhabited / empty type](https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types) -/// for `array` with traits we need for RHS values. +/// for `array` with traits we need for literal values. #[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize)] pub enum UninhabitedArray {} diff --git a/engine/src/rhs_types/bool.rs b/engine/src/rhs_types/bool.rs index e44b4211..70c49ed9 100644 --- a/engine/src/rhs_types/bool.rs +++ b/engine/src/rhs_types/bool.rs @@ -5,7 +5,7 @@ use std::borrow::Borrow; use std::cmp::Ordering; /// [Uninhabited / empty type](https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types) -/// for `bool` with traits we need for RHS values. +/// for `bool` with traits we need for literal values. #[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize)] pub enum UninhabitedBool {} diff --git a/engine/src/rhs_types/map.rs b/engine/src/rhs_types/map.rs index 72baa80b..285c44ec 100644 --- a/engine/src/rhs_types/map.rs +++ b/engine/src/rhs_types/map.rs @@ -7,7 +7,7 @@ use std::borrow::Borrow; use std::cmp::Ordering; /// [Uninhabited / empty type](https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types) -/// for `map` with traits we need for RHS values. +/// for `map` with traits we need for literal values. #[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize)] pub enum UninhabitedMap {} diff --git a/engine/src/scheme.rs b/engine/src/scheme.rs index 303fc29f..854ef386 100644 --- a/engine/src/scheme.rs +++ b/engine/src/scheme.rs @@ -3,7 +3,7 @@ use crate::ast::{FilterAst, FilterValueAst}; use crate::functions::FunctionDefinition; use crate::lex::{Lex, LexErrorKind, LexResult, LexWith, expect, span, take_while}; use crate::list_matcher::ListDefinition; -use crate::types::{GetType, RhsValue, Type}; +use crate::types::{GetType, LiteralValue, Type}; use fnv::FnvBuildHasher; use serde::de::Visitor; use serde::ser::SerializeMap; @@ -56,9 +56,9 @@ impl<'i> Lex<'i> for FieldIndex { // The token inside an [] can be either an integer index into an Array // or a string key into a Map. The token is a key into a Map if it // starts and ends with "\"", otherwise an integer index or an error. - let (rhs, rest) = match expect(input, "\"") { - Ok(_) => RhsValue::lex_with(input, Type::Bytes), - Err(_) => RhsValue::lex_with(input, Type::Int).map_err(|_| { + let (literal, rest) = match expect(input, "\"") { + Ok(_) => LiteralValue::lex_with(input, Type::Bytes), + Err(_) => LiteralValue::lex_with(input, Type::Int).map_err(|_| { ( LexErrorKind::ExpectedLiteral( "expected quoted utf8 string or positive integer", @@ -68,15 +68,15 @@ impl<'i> Lex<'i> for FieldIndex { }), }?; - match rhs { - RhsValue::Int(i) => match u32::try_from(i) { + match literal { + LiteralValue::Int(i) => match u32::try_from(i) { Ok(u) => Ok((FieldIndex::ArrayIndex(u), rest)), Err(_) => Err(( LexErrorKind::ExpectedLiteral("expected positive integer as index"), input, )), }, - RhsValue::Bytes(b) => match String::from_utf8(b.into()) { + LiteralValue::Bytes(b) => match String::from_utf8(b.into()) { Ok(s) => Ok((FieldIndex::MapKey(s), rest)), Err(_) => Err((LexErrorKind::ExpectedLiteral("expected utf8 string"), input)), }, diff --git a/engine/src/types.rs b/engine/src/types.rs index 3eff652f..c31d65c3 100644 --- a/engine/src/types.rs +++ b/engine/src/types.rs @@ -17,7 +17,7 @@ use std::iter::once; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use thiserror::Error; -fn lex_rhs_values<'i, T: Lex<'i>>(input: &'i str) -> LexResult<'i, Vec> { +fn lex_literal_set<'i, T: Lex<'i>>(input: &'i str) -> LexResult<'i, Vec> { let mut input = expect(input, "{")?; let mut res = Vec::new(); loop { @@ -170,12 +170,12 @@ macro_rules! specialized_try_from { }; } -// This macro generates `Type`, `LhsValue`, `RhsValue`, `RhsValues`. +// This macro generates `Type`, `LhsValue`, `LiteralValue`, `LiteralSet`. // // Before the parenthesis is the variant for the `Type` enum (`Type::Ip`). // First argument is the corresponding `LhsValue` variant (`LhsValue::Ip(IpAddr)`). -// Second argument is the corresponding `RhsValue` variant (`RhsValue::Ip(IpAddr)`). -// Third argument is the corresponding `RhsValues` variant (`RhsValues::Ip(Vec)`) for the curly bracket syntax. eg `num in {1, 5}` +// Second argument is the corresponding `LiteralValue` variant (`LiteralValue::Ip(IpAddr)`). +// Third argument is the corresponding `LiteralSet` variant (`LiteralSet::Ip(Vec)`) for the curly bracket syntax. eg `num in {1, 5}` // // ``` // declare_types! { @@ -209,7 +209,7 @@ macro_rules! declare_types { }; // This is the entry point for the macro. - ($($(# $attrs:tt)* $name:ident $([$val_ty:ty])? ( $(# $lhs_attrs:tt)* $lhs_ty:ty | $rhs_ty:ty | $multi_rhs_ty:ty ) , )*) => { + ($($(# $attrs:tt)* $name:ident $([$val_ty:ty])? ( $(# $lhs_attrs:tt)* $lhs_ty:ty | $literal_ty:ty | $literal_set_ty:ty ) , )*) => { /// Enumeration of supported types for field values. #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, Hash, PartialOrd, Ord)] pub enum Type { @@ -257,29 +257,29 @@ macro_rules! declare_types { })* declare_types! { - /// An RHS value parsed from a filter string. + /// A literal value parsed from a filter string. #[derive(PartialEq, Eq, Clone, Hash, Serialize)] #[serde(untagged)] - enum RhsValue { - $($(# $attrs)* $name($rhs_ty),)* + enum LiteralValue { + $($(# $attrs)* $name($literal_ty),)* } } - impl<'i> LexWith<'i, Type> for RhsValue { + impl<'i> LexWith<'i, Type> for LiteralValue { fn lex_with(input: &str, ty: Type) -> LexResult<'_, Self> { Ok(match ty { $(replace_underscore!($name $(($val_ty))?) => { - let (value, input) = <$rhs_ty>::lex(input)?; - (RhsValue::$name(value), input) + let (value, input) = <$literal_ty>::lex(input)?; + (LiteralValue::$name(value), input) })* }) } } - impl<'a> PartialOrd for LhsValue<'a> { - fn partial_cmp(&self, other: &RhsValue) -> Option { + impl<'a> PartialOrd for LhsValue<'a> { + fn partial_cmp(&self, other: &LiteralValue) -> Option { match (self, other) { - $((LhsValue::$name(lhs), RhsValue::$name(rhs)) => { + $((LhsValue::$name(lhs), LiteralValue::$name(rhs)) => { lhs.strict_partial_cmp(rhs) },)* _ => None, @@ -287,12 +287,12 @@ macro_rules! declare_types { } } - $(impl<'a> TryFrom for $rhs_ty { + $(impl<'a> TryFrom for $literal_ty { type Error = TypeMismatchError; - fn try_from(value: RhsValue) -> Result<$rhs_ty, TypeMismatchError> { + fn try_from(value: LiteralValue) -> Result<$literal_ty, TypeMismatchError> { match value { - RhsValue::$name(value) => Ok(value), + LiteralValue::$name(value) => Ok(value), _ => Err(TypeMismatchError { expected: specialized_try_from!($name).into(), actual: value.get_type(), @@ -301,12 +301,12 @@ macro_rules! declare_types { } })* - $(impl<'a> TryFrom<&'a RhsValue> for &'a $rhs_ty { + $(impl<'a> TryFrom<&'a LiteralValue> for &'a $literal_ty { type Error = TypeMismatchError; - fn try_from(value: &'a RhsValue) -> Result<&'a $rhs_ty, TypeMismatchError> { + fn try_from(value: &'a LiteralValue) -> Result<&'a $literal_ty, TypeMismatchError> { match value { - RhsValue::$name(value) => Ok(value), + LiteralValue::$name(value) => Ok(value), _ => Err(TypeMismatchError { expected: specialized_try_from!($name).into(), actual: value.get_type(), @@ -322,34 +322,34 @@ macro_rules! declare_types { /// only same-typed values in a list. #[derive(PartialEq, Eq, Clone, Hash, Serialize)] #[serde(untagged)] - enum RhsValues { - $($(# $attrs)* $name(Vec<$multi_rhs_ty>),)* + enum LiteralSet { + $($(# $attrs)* $name(Vec<$literal_set_ty>),)* } } - impl From for RhsValues { - fn from(rhs: RhsValue) -> Self { - match rhs { - $(RhsValue::$name(rhs) => { + impl From for LiteralSet { + fn from(literal: LiteralValue) -> Self { + match literal { + $(LiteralValue::$name(literal) => { #[allow(unreachable_code)] - RhsValues::$name(vec![rhs.into()]) + LiteralSet::$name(vec![literal.into()]) })* } } } - impl RhsValues { + impl LiteralSet { /// Appends a value to the back of the collection. - pub fn push(&mut self, rhs: RhsValue) -> Result<(), TypeMismatchError> { + pub fn push(&mut self, literal: LiteralValue) -> Result<(), TypeMismatchError> { match self { - $(RhsValues::$name(vec) => match rhs { - RhsValue::$name(rhs) => { + $(LiteralSet::$name(vec) => match literal { + LiteralValue::$name(literal) => { #[allow(unreachable_code)] - Ok(vec.push(rhs.into())) + Ok(vec.push(literal.into())) } _ => Err(TypeMismatchError { expected: self.get_type().into(), - actual: rhs.get_type(), + actual: literal.get_type(), }), },)* } @@ -358,8 +358,8 @@ macro_rules! declare_types { /// Moves all the values of `other` into `self`, leaving `other` empty. pub fn append(&mut self, other: &mut Self) -> Result<(), TypeMismatchError> { match self { - $(RhsValues::$name(vec) => match other { - RhsValues::$name(other) => Ok(vec.append(other)), + $(LiteralSet::$name(vec) => match other { + LiteralSet::$name(other) => Ok(vec.append(other)), _ => Err(TypeMismatchError { expected: self.get_type().into(), actual: other.get_type(), @@ -371,8 +371,8 @@ macro_rules! declare_types { /// Extends the collection with the values of another collection. pub fn extend(&mut self, other: Self) -> Result<(), TypeMismatchError> { match self { - $(RhsValues::$name(vec) => match other { - RhsValues::$name(other) => Ok(vec.extend(other)), + $(LiteralSet::$name(vec) => match other { + LiteralSet::$name(other) => Ok(vec.extend(other)), _ => Err(TypeMismatchError { expected: self.get_type().into(), actual: other.get_type(), @@ -382,12 +382,12 @@ macro_rules! declare_types { } } - impl<'i> LexWith<'i, Type> for RhsValues { + impl<'i> LexWith<'i, Type> for LiteralSet { fn lex_with(input: &str, ty: Type) -> LexResult<'_, Self> { Ok(match ty { $(replace_underscore!($name $(($val_ty))?) => { - let (value, input) = lex_rhs_values(input)?; - (RhsValues::$name(value), input) + let (value, input) = lex_literal_set(input)?; + (LiteralSet::$name(value), input) })* }) } @@ -461,10 +461,10 @@ impl PartialEq<&LhsValue<'_>> for LhsValue<'_> { } } -impl StrictPartialOrd for LhsValue<'_> {} +impl StrictPartialOrd for LhsValue<'_> {} -impl PartialEq for LhsValue<'_> { - fn eq(&self, other: &RhsValue) -> bool { +impl PartialEq for LhsValue<'_> { + fn eq(&self, other: &LiteralValue) -> bool { self.strict_partial_cmp(other) == Some(Ordering::Equal) } } @@ -649,28 +649,28 @@ impl<'a> TryFrom<&'a LhsValue<'a>> for &'a [u8] { } } -impl<'a> From<&'a RhsValue> for LhsValue<'a> { - fn from(rhs_value: &'a RhsValue) -> Self { - match rhs_value { - RhsValue::Ip(ip) => LhsValue::Ip(*ip), - RhsValue::Bytes(bytes) => LhsValue::Bytes(Bytes::Borrowed(bytes)), - RhsValue::Int(integer) => LhsValue::Int(*integer), - RhsValue::Bool(b) => match *b {}, - RhsValue::Array(a) => match *a {}, - RhsValue::Map(m) => match *m {}, +impl<'a> From<&'a LiteralValue> for LhsValue<'a> { + fn from(literal: &'a LiteralValue) -> Self { + match literal { + LiteralValue::Ip(ip) => LhsValue::Ip(*ip), + LiteralValue::Bytes(bytes) => LhsValue::Bytes(Bytes::Borrowed(bytes)), + LiteralValue::Int(integer) => LhsValue::Int(*integer), + LiteralValue::Bool(b) => match *b {}, + LiteralValue::Array(a) => match *a {}, + LiteralValue::Map(m) => match *m {}, } } } -impl From for LhsValue<'_> { - fn from(rhs_value: RhsValue) -> Self { - match rhs_value { - RhsValue::Ip(ip) => LhsValue::Ip(ip), - RhsValue::Bytes(bytes) => LhsValue::Bytes(Bytes::Owned(bytes.into())), - RhsValue::Int(integer) => LhsValue::Int(integer), - RhsValue::Bool(b) => match b {}, - RhsValue::Array(a) => match a {}, - RhsValue::Map(m) => match m {}, +impl From for LhsValue<'_> { + fn from(literal: LiteralValue) -> Self { + match literal { + LiteralValue::Ip(ip) => LhsValue::Ip(ip), + LiteralValue::Bytes(bytes) => LhsValue::Bytes(Bytes::Owned(bytes.into())), + LiteralValue::Int(integer) => LhsValue::Int(integer), + LiteralValue::Bool(b) => match b {}, + LiteralValue::Array(a) => match a {}, + LiteralValue::Map(m) => match m {}, } } }