Crates.io | fltrs |
lib.rs | fltrs |
version | 0.2.0 |
source | src |
created_at | 2022-05-01 15:11:39.981529 |
updated_at | 2022-06-01 15:20:23.001726 |
description | Filter for querying lists. |
homepage | |
repository | https://github.com/lima1909/fltrs |
max_upload_size | |
id | 578633 |
size | 106,624 |
Easy to define filters for querying lists. Fltrs
has no dependencies!
Fltrs want to support creating easy, fast and expandable filters for iterable things (like Vec, Array, Map, Set, ...) in rust. A filter is created based on an input string (query). This has particular advantages if the filter is created at runtime, i.e. in a GUI or command line tool (CLI).
It is possible, to expand the filter/query to your own needs:
You can find examples on the Query builder page.
use fltrs::query;
let result: Vec<_> = [3, 2, 1, 4, 5, 7, 5, 4, 3]
.into_iter()
.filter(query("> 1 and < 5").unwrap())
.collect();
assert_eq!(vec![3, 2, 4, 4, 3], result);
use fltrs::query;
let result: Vec<_> = ["Inge", "Petra", "Paul", "Egon", "Peter"]
.into_iter()
.filter(query("contains 'e'").unwrap())
.collect();
assert_eq!(vec!["Inge", "Petra", "Peter"], result);
use fltrs::query;
let result: Vec<Option<char>> = [None, Some('a'), None, Some('b'), Some('c'), Some('a')]
.into_iter()
.filter(query(" != 'a' and not = none ").unwrap())
.collect();
assert_eq!(vec![Some('b'), Some('c')], result);
use fltrs::query;
let result: Vec<_> = [3, 2, 1, 4, 5, 7, 5, 4, 3]
.into_iter()
.filter(query("(= 1 or = 5) and > 1").unwrap())
.collect();
assert_eq!(vec![5, 5], result);
use fltrs::query;
let result: Vec<_> = [3, 2, 1, 4, 5, 7, 5, 4, 3]
.into_iter()
.filter(query("not( (= 1 or = 5) and > 1)").unwrap())
.collect();
assert_eq!(vec![3, 2, 1, 4, 7, 4, 3], result);
This is possible, if the struct implement the trait: [PathResolver
].
use fltrs::{PathResolver, Filterable, query};
#[derive(PartialEq, Debug)]
struct Point {
name: &'static str,
x: i32,
y: i32,
}
impl PathResolver for Point {
fn path_to_index(path: &str) -> Option<usize> {
match path {
"name" => Some(0),
"x" => Some(1),
"y" => Some(2),
_ => None,
}
}
fn value(&self, idx: usize) -> &dyn Filterable {
match idx {
0 => &self.name,
1 => &self.x,
_ => &self.y,
}
}
}
let result: Vec<Point> =
[
Point { name: "Point_1_3", x: 1, y: 3},
Point { name: "Point_3_3", x: 3, y: 3},
Point { name: "Point_2_6", x: 2, y: 6},
]
.into_iter()
.filter(query(r#"x one_of [3, 7]"#).unwrap())
.collect();
assert_eq!(vec![Point { name: "Point_3_3", x: 3, y: 3}], result);