aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 4839de28a6f4794aafb7ccff6049ffbfbc65a484 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Parses and evaluates [AIP-160](https://google.aip.dev/160) filter expressions.
//!
//! ## Example
//!
//! ```rust
//! use aif::{parse, Value, Filterable};
//!
//! let expr = parse("state = \"open\" AND priority > 3").unwrap().unwrap();
//! println!("{}", expr);
//! ```
//!
//! ## Evaluating filters
//!
//! Implement [`Filterable`] for your type, then call [`Expr::evaluate`]:
//!
//! ```rust
//! use aif::{parse, Value, Filterable};
//!
//! struct Issue {
//!     state: String,
//!     priority: i64,
//!     labels: Vec<String>,
//! }
//!
//! impl Filterable for Issue {
//!     fn field(&self, name: &str) -> Option<Value<'_>> {
//!         match name {
//!             "state"    => Some(Value::String(&self.state)),
//!             "priority" => Some(Value::Int(self.priority)),
//!             "labels"   => Some(Value::List(
//!                 self.labels.iter().map(|l| Value::String(l)).collect()
//!             )),
//!             _ => None,
//!         }
//!     }
//! }
//!
//! let expr = parse("state = \"open\"").unwrap().unwrap();
//! let issue = Issue { state: "open".into(), priority: 1, labels: vec![] };
//! assert!(expr.evaluate(&issue));
//! ```

pub mod ast;
mod compiled;
pub mod display;
pub mod eval;
pub mod lex;
pub mod parser;

pub use ast::{BinaryOp, CompareOp, Expr, Position, UnaryOp};
pub use compiled::CompiledFilter;
pub use eval::{Filterable, Value};

/// Parse a filter expression string into an [`Expr`] AST.
///
/// Returns `Ok(None)` for empty/whitespace-only input.
/// Returns `Ok(Some(expr))` on success.
/// Returns `Err(message)` on parse errors.
pub fn parse(input: &str) -> Result<Option<Expr>, String> {
    parser::parse_filter(input)
}