Skip to main content

FilterValue

Enum FilterValue 

Source
pub enum FilterValue<'a> {
    Null,
    Bool(bool),
    Number(f64),
    String(Cow<'a, str>),
    Tuple(Vec<FilterValue<'a>>),
}
Expand description

A value which may appear within a filter expression, either as a literal or as the result of resolving a property on a Filterable object.

FilterValue implements From for most primitive Rust types (booleans, numbers, strings, Options, and vectors of values), making it easy to construct from your own data within a Filterable::get implementation.

use filt_rs::FilterValue;

let value: FilterValue<'_> = 42.into();
assert_eq!(value, FilterValue::Number(42.0));

let value: FilterValue<'_> = Some("hello").into();
assert_eq!(value, FilterValue::String("hello".into()));

let value: FilterValue<'_> = None::<bool>.into();
assert_eq!(value, FilterValue::Null);

Note that string equality comparisons between FilterValues are case-insensitive, mirroring the behaviour of the filter language itself. Case is folded character-by-character using the language’s Unicode case-folding rules (with all Greek sigma forms treated as equivalent), so multi-character folds such as ßss compare equal too.

use filt_rs::FilterValue;

let a: FilterValue<'_> = "Hello".into();
let b: FilterValue<'_> = "hello".into();
assert_eq!(a, b);

let a: FilterValue<'_> = "STRASSE".into();
let b: FilterValue<'_> = "straße".into();
assert_eq!(a, b);

Variants§

§

Null

The absence of a value, also returned for unknown property keys.

§

Bool(bool)

A boolean value (true or false).

§

Number(f64)

A numeric value; all numbers are represented as 64-bit floats.

§

String(Cow<'a, str>)

A string value, compared case-insensitively by the filter language.

The string may borrow from the object being filtered (when produced by a Filterable::get implementation) or own its data, courtesy of the Cow.

§

Tuple(Vec<FilterValue<'a>>)

An ordered list of values, written as [a, b, c] in filter expressions.

Implementations§

Source§

impl<'a> FilterValue<'a>

Source

pub fn is_truthy(&self) -> bool

Determines whether this value is considered “truthy” by the filter language.

Filters match an object when their expression evaluates to a truthy value. FilterValue::Null, false, 0, empty strings, and empty tuples are falsy; everything else is truthy.

With the chrono feature enabled, datetimes are always truthy, while durations are truthy if (and only if) they are non-zero.

use filt_rs::FilterValue;

assert!(FilterValue::Bool(true).is_truthy());
assert!(FilterValue::String("hello".into()).is_truthy());
assert!(!FilterValue::Null.is_truthy());
assert!(!FilterValue::Number(0.0).is_truthy());
Source

pub fn contains(&self, other: &FilterValue<'a>) -> bool

Determines whether this value contains the provided value.

For tuples, this checks whether any element is equal to other; for strings, it performs a case-insensitive substring search. All other combinations return false. This powers the contains and in operators in the filter language.

The string comparison case-folds both operands character-by-character without allocating, using the same Unicode case-folding rules as the rest of the filter language: all Greek sigma forms (Σ, σ, and the final-position ς) are treated as equivalent regardless of where they appear in a word, and multi-character folds such as ßss participate fully.

use filt_rs::FilterValue;

let haystack: FilterValue<'_> ="Hello World".into();
assert!(haystack.contains(&"world".into()));

let tuple = FilterValue::Tuple(vec!["a".into(), "b".into()]);
assert!(tuple.contains(&"a".into()));
assert!(!tuple.contains(&"c".into()));
Source

pub fn startswith(&self, other: &FilterValue<'a>) -> bool

Determines whether this value starts with the provided value.

For strings, this performs a case-insensitive prefix test; for tuples, it checks whether any element is equal to other. This powers the startswith operator in the filter language.

The string comparison case-folds both operands character-by-character without allocating, using the same Unicode case-folding rules as the rest of the filter language (see FilterValue::contains).

use filt_rs::FilterValue;

let value: FilterValue<'_> ="Hello World".into();
assert!(value.startswith(&"hello".into()));
assert!(!value.startswith(&"world".into()));
Source

pub fn endswith(&self, other: &FilterValue<'a>) -> bool

Determines whether this value ends with the provided value.

For strings, this performs a case-insensitive suffix test; for tuples, it checks whether any element is equal to other. This powers the endswith operator in the filter language.

The string comparison case-folds both operands character-by-character without allocating, using the same Unicode case-folding rules as the rest of the filter language (see FilterValue::contains).

use filt_rs::FilterValue;

let value: FilterValue<'_> ="Hello World".into();
assert!(value.endswith(&"WORLD".into()));
assert!(!value.endswith(&"hello".into()));
Source

pub fn eq_cs(&self, other: &FilterValue<'a>) -> bool

Determines whether this value is equal to the provided value, comparing strings case-sensitively.

This is the case-sensitive counterpart of the == operator (and the PartialEq implementation): tuples compare their elements with eq_cs recursively, and all other variants behave exactly as == does. It underpins tuple membership for the contains_cs and in_cs operators in the filter language.

use filt_rs::FilterValue;

let value: FilterValue<'_> ="Hello".into();
assert!(value.eq_cs(&"Hello".into()));
assert!(!value.eq_cs(&"hello".into()));
Source

pub fn contains_cs(&self, other: &FilterValue<'a>) -> bool

Determines whether this value contains the provided value, comparing strings case-sensitively.

This is the case-sensitive counterpart of FilterValue::contains: tuples check whether any element is eq_cs to other, strings perform an exact substring search, and all other combinations return false. This powers the contains_cs and in_cs operators in the filter language.

use filt_rs::FilterValue;

let haystack: FilterValue<'_> ="Hello World".into();
assert!(haystack.contains_cs(&"World".into()));
assert!(!haystack.contains_cs(&"world".into()));
Source

pub fn startswith_cs(&self, other: &FilterValue<'a>) -> bool

Determines whether this value starts with the provided value, comparing strings case-sensitively.

This is the case-sensitive counterpart of FilterValue::startswith, powering the startswith_cs operator in the filter language.

use filt_rs::FilterValue;

let value: FilterValue<'_> ="Hello World".into();
assert!(value.startswith_cs(&"Hello".into()));
assert!(!value.startswith_cs(&"hello".into()));
Source

pub fn endswith_cs(&self, other: &FilterValue<'a>) -> bool

Determines whether this value ends with the provided value, comparing strings case-sensitively.

This is the case-sensitive counterpart of FilterValue::endswith, powering the endswith_cs operator in the filter language.

use filt_rs::FilterValue;

let value: FilterValue<'_> ="Hello World".into();
assert!(value.endswith_cs(&"World".into()));
assert!(!value.endswith_cs(&"WORLD".into()));

Trait Implementations§

Source§

impl<'a> Clone for FilterValue<'a>

Source§

fn clone(&self) -> FilterValue<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for FilterValue<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for FilterValue<'a>

Source§

fn default() -> FilterValue<'a>

Returns the “default value” for a type. Read more
Source§

impl<'a> Display for FilterValue<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value as it would appear within a filter expression.

use filt_rs::FilterValue;

let value = FilterValue::Tuple(vec!["a".into(), 1.into(), FilterValue::Null]);
assert_eq!(value.to_string(), r#"["a", 1, null]"#);

Secret values (available with the secrecy feature) are always formatted as [REDACTED], never as their underlying string.

Source§

impl<'a> From<&'a str> for FilterValue<'a>

Source§

fn from(s: &'a str) -> Self

Borrows the string slice rather than copying it, so a Filterable::get implementation returning self.field.as_str().into() performs no allocation. Use From<String> (or .to_string().into()) when you need the value to own its data instead.

Source§

impl<'a, T> From<Option<T>> for FilterValue<'a>
where T: Into<FilterValue<'a>>,

Source§

fn from(o: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<String> for FilterValue<'a>

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Vec<FilterValue<'a>>> for FilterValue<'a>

Source§

fn from(v: Vec<FilterValue<'a>>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<bool> for FilterValue<'a>

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<f32> for FilterValue<'a>

Source§

fn from(n: f32) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<f64> for FilterValue<'a>

Source§

fn from(n: f64) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<i8> for FilterValue<'a>

Source§

fn from(n: i8) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<i16> for FilterValue<'a>

Source§

fn from(n: i16) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<i32> for FilterValue<'a>

Source§

fn from(n: i32) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<i64> for FilterValue<'a>

Source§

fn from(n: i64) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<u8> for FilterValue<'a>

Source§

fn from(n: u8) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<u16> for FilterValue<'a>

Source§

fn from(n: u16) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<u32> for FilterValue<'a>

Source§

fn from(n: u32) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<u64> for FilterValue<'a>

Source§

fn from(n: u64) -> Self

Converts to this type from the input type.
Source§

impl<'a> PartialEq for FilterValue<'a>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialOrd for FilterValue<'a>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Source§

fn lt(&self, other: &Self) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Source§

fn le(&self, other: &Self) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

fn gt(&self, other: &Self) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Source§

fn ge(&self, other: &Self) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for FilterValue<'a>

§

impl<'a> RefUnwindSafe for FilterValue<'a>

§

impl<'a> Send for FilterValue<'a>

§

impl<'a> Sync for FilterValue<'a>

§

impl<'a> Unpin for FilterValue<'a>

§

impl<'a> UnsafeUnpin for FilterValue<'a>

§

impl<'a> UnwindSafe for FilterValue<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.