pub struct Predicate { /* private fields */ }Expand description
A compiled, type-erased test that evaluates a single Value against an
Operation and a reference value (or list of reference values).
A Predicate is the lowest-level building block. It is normally created
indirectly through Condition, but you can build one
directly when you need fine-grained control (e.g. when constructing a
Condition::new or
Condition::with_index).
§Choosing a constructor
| Constructor | Reference value source | Type information |
|---|---|---|
with_value | A single T: Into<Value> | Inferred from the value |
with_list | A slice of T: Into<Value> + Clone | Inferred from the first element |
with_value_list | A slice of Value | Inferred from the first element |
with_str | A &str that is parsed at build time | Supplied explicitly as ValueKind |
with_str_list | A slice of &str parsed at build time | Supplied explicitly as ValueKind |
Negated operations (e.g. Operation::IsNot, Operation::NotContains) are
handled transparently: the constructor stores a negated flag so that the
result of evaluate is automatically flipped.
Implementations§
Source§impl Predicate
impl Predicate
Sourcepub fn with_value<'a, T>(op: Operation, value: T) -> Result<Self, Error>
pub fn with_value<'a, T>(op: Operation, value: T) -> Result<Self, Error>
Creates a predicate from a single typed value.
The value type is determined by the T: Into<Value> conversion, so you can
pass Rust primitives directly wherever a From/Into impl exists (e.g.
&str, u32, bool, IpAddr). You can also pass a Value variant
directly.
Negating operations (IsNot, NotContains, …) are fully supported; the
predicate stores a negated flag internally and flips the result at
evaluation time.
§Errors
Returns an Error if the operation is incompatible with the value type
(e.g. applying Contains to a numeric value).
§Examples
use whereexpr::{Predicate, Operation, Value};
// String equality
let p = Predicate::with_value(Operation::Is, Value::String("hello")).unwrap();
// Numeric greater-than
let p = Predicate::with_value(Operation::GreaterThan, Value::U32(42)).unwrap();
// Negated: "is not"
let p = Predicate::with_value(Operation::IsNot, Value::U32(0)).unwrap();Sourcepub fn with_list<'a, T>(op: Operation, values: &[T]) -> Result<Self, Error>
pub fn with_list<'a, T>(op: Operation, values: &[T]) -> Result<Self, Error>
Creates a predicate from a slice of typed values.
This is a convenience wrapper around with_value_list
for use when the values are stored as T: Into<Value> + Clone rather than as
Value directly. Elements are cloned and converted internally; for lists
already in Value form prefer with_value_list to avoid the extra copy.
The value type of the predicate is inferred from the first element; all subsequent elements must be of the same type.
§Errors
Error::EmptyListForOperation–valuesis empty.Error::InvalidOperationForValue– the operation is incompatible with the inferred type (e.g.Boolcannot be used with list operations).- Any other type-specific parse or range error.
§Examples
use whereexpr::{Predicate, Operation, Value};
// "is one of" check against a list of u32 values
let allowed: &[u32] = &[1, 2, 3];
// u32 implements Into<Value> via Value::U32
let values: Vec<Value> = allowed.iter().map(|v| Value::U32(*v)).collect();
let p = Predicate::with_value_list(Operation::IsOneOf, &values).unwrap();
// Convenience form using with_list (same result)
let p2 = Predicate::with_list(Operation::IsOneOf, &values).unwrap();Sourcepub fn with_value_list(
op: Operation,
values: &[Value<'_>],
) -> Result<Self, Error>
pub fn with_value_list( op: Operation, values: &[Value<'_>], ) -> Result<Self, Error>
Creates a predicate from a slice of Values.
The value type is inferred from the first element; all elements must share
the same ValueKind. This constructor is best suited for list-based
operations such as Operation::IsOneOf, Operation::IsNotOneOf,
Operation::ContainsOneOf, etc.
For slices of T: Into<Value> see the ergonomic wrapper
with_list.
§Errors
Error::EmptyListForOperation–valuesis empty.Error::InvalidOperationForValue–Boolvalues cannot be used with list operations.- Any other type-specific or operation-compatibility error.
§Examples
use whereexpr::{Predicate, Operation, Value};
let values = [Value::String("foo"), Value::String("bar"), Value::String("baz")];
let p = Predicate::with_value_list(Operation::IsOneOf, &values).unwrap();
// Negated variant: "is not one of"
let p_neg = Predicate::with_value_list(Operation::IsNotOneOf, &values).unwrap();Sourcepub fn with_str(
op: Operation,
value: &str,
value_kind: ValueKind,
ignore_case: bool,
) -> Result<Self, Error>
pub fn with_str( op: Operation, value: &str, value_kind: ValueKind, ignore_case: bool, ) -> Result<Self, Error>
Creates a predicate from a string representation of a single value.
Because the string carries no inherent type information, the target type must
be supplied explicitly via value_kind. The string is parsed into the
corresponding internal representation at construction time, not at
evaluation time.
The ignore_case flag is only meaningful for string-like types
(ValueKind::String, ValueKind::Path); it is silently ignored for all
other kinds.
This constructor is used internally by Condition::from_str
during ExpressionBuilder::build, but you
can call it directly when you have a dynamic string value and know the target
type at call site.
§Errors
Error::FailToParseValue– the string cannot be parsed intovalue_kind.Error::InvalidOperationForValue– the operation is incompatible withvalue_kind.
§Examples
use whereexpr::{Predicate, Operation, ValueKind};
// Parse "42" as a u32 and create a "greater than" predicate
let p = Predicate::with_str(Operation::GreaterThan, "42", ValueKind::U32, false).unwrap();
// Case-insensitive string equality
let p = Predicate::with_str(Operation::Is, "Alice", ValueKind::String, true).unwrap();
// Parse will fail if the string does not match the expected type
let err = Predicate::with_str(Operation::Is, "not_a_number", ValueKind::U32, false);
assert!(err.is_err());Sourcepub fn with_str_list(
op: Operation,
values: &[&str],
value_kind: ValueKind,
ignore_case: bool,
) -> Result<Self, Error>
pub fn with_str_list( op: Operation, values: &[&str], value_kind: ValueKind, ignore_case: bool, ) -> Result<Self, Error>
Creates a predicate from a slice of string representations of values.
Each element of values is parsed into value_kind at construction time.
This is the multi-value counterpart of with_str and
is intended for list-based operations such as Operation::IsOneOf,
Operation::StartsWithOneOf, Operation::ContainsOneOf, etc.
As with with_str, ignore_case applies only to string-like kinds and is
silently ignored for numeric or other types.
§Errors
Error::FailToParseValue– any element cannot be parsed intovalue_kind.Error::InvalidOperationForValue–Boolvalues cannot be used with list operations, or the chosen operation is otherwise incompatible with the type.Error::EmptyListForIsOneOf/Error::EmptyListForGlobREMatch– the slice is empty for operations that require at least one value.
§Examples
use whereexpr::{Predicate, Operation, ValueKind};
// "is one of" check against a list of string values (case-insensitive)
let p = Predicate::with_str_list(
Operation::IsOneOf,
&["alice", "bob", "carol"],
ValueKind::String,
true,
).unwrap();
// "is not one of" for integers
let p = Predicate::with_str_list(
Operation::IsNotOneOf,
&["0", "1", "2"],
ValueKind::U32,
false,
).unwrap();Auto Trait Implementations§
impl Freeze for Predicate
impl RefUnwindSafe for Predicate
impl Send for Predicate
impl Sync for Predicate
impl Unpin for Predicate
impl UnsafeUnpin for Predicate
impl UnwindSafe for Predicate
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more