Skip to main content

Predicate

Struct Predicate 

Source
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

ConstructorReference value sourceType information
with_valueA single T: Into<Value>Inferred from the value
with_listA slice of T: Into<Value> + CloneInferred from the first element
with_value_listA slice of ValueInferred from the first element
with_strA &str that is parsed at build timeSupplied explicitly as ValueKind
with_str_listA slice of &str parsed at build timeSupplied 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

Source

pub fn with_value<'a, T>(op: Operation, value: T) -> Result<Self, Error>
where T: Into<Value<'a>>,

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();
Source

pub fn with_list<'a, T>(op: Operation, values: &[T]) -> Result<Self, Error>
where T: Into<Value<'a>> + Clone,

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
§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();
Source

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
§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();
Source

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
§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());
Source

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
§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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.