Struct fltrs::Query

source · []
pub struct Query<PR> { /* private fields */ }
Expand description

The Query is an builder to configure the query(). It is possible, to extend the Operators in the modul: crate::operator.

Example

Create your own operator:

use fltrs::{value::Value, PathResolver, Predicate, Query, Result, query};

fn upper_eq<PR: PathResolver>(idx: usize, v: Value) -> Result<Predicate<PR>> {
    Ok(Box::new(
        move |pr| {
          if let Value::Text(t) = &v {
              return pr.value(idx).as_string().to_uppercase().eq(&t.to_uppercase());
          }
          false
        }
     ))
}

let query = Query::build()
             .operators(&[("upper_eq", upper_eq)])
             .query(r#" upper_eq "ab" "#)
             .unwrap();

let result: Vec<&str> = ["yz", "aB", "Ab", "xY"].into_iter().filter(query).collect();

assert_eq!(vec!["aB", "Ab"], result);

Create your own as [convert function] (for example: conversion of units):

use fltrs::{value::Value, PathResolver, Predicate, Query, Result, query};

let query = Query::build()
             .as_value_fn(&[("kbyte", |v| {
                    if let Value::Int(x) = v {
                        return Value::Int(x * 1024);
                    }
                    v
                  }
                )])
             .query(r#" > 1 as kbyte and < 6 as kbyte "#)
             .unwrap();

// list of bytes
let result: Vec<i32> = [100, 1025, 7000, 4001].into_iter().filter(query).collect();

assert_eq!(vec![1025, 4001], result);

Implementations

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.