OData v4 Params Parsing Library
This library provides a parser for OData v4 $filter expressions.
It converts these expressions into an Abstract Syntax Tree (AST),
allowing for further processing and evaluation. The parser supports
a wide range of logical operators, comparison operators, function calls,
and nested expressions, making it highly versatile for complex querying needs.
Note that this is a work in progress. A full list of specification components that are supported and not supported will be added in the next release.
Features
- Logical Operators:
and,or,not - Comparison Operators:
eq,ne,gt,lt,ge,le - Function Calls: Ex:
startswith,endswith,contains,concat - Grouping: Nested expressions with parentheses
- Data Types: String, Number, Boolean, Date, Time, DateTime with Time Zone
Data Types
The library supports the following data types in expressions:
- String: Enclosed in single quotes
'example' - Number: Integer and decimal numbers
123,45.67 - Boolean:
true,false - Date: ISO 8601 format
YYYY-MM-DD - DateTime: ISO 8601 format with time
YYYY-MM-DDTHH:MM:SSZ - Time: ISO 8601 format
HH:MM:SS
Testing
The library includes a set of tests mostly generated by AI to catch regressions.
You can run the tests using the following command:
Installation
To add this library to your project, add the following to your Cargo.toml:
[]
= "0.1.0"
Example
Here is an example of how to parse a simple filter expression:
use parse_str;
Supported Expressions
Logical Operators
use parse_str;
let filter = "name eq 'John' or isActive eq true";
let result = parse_str.expect;
// Expression: (name = 'John') OR (isActive = true)
Comparison Operators
use parse_str;
let filter = "price lt 99.99";
let result = parse_str.expect;
// Expression: price < 99.99
Function Calls
use parse_str;
let filter = "endswith(name, 'Smith')";
let result = parse_str.expect;
// Expression: endswith(name, 'Smith')
Advanced Usage
Nested Grouping
use parse_str;
let filter = "((name eq 'John' and isActive eq true) or (age gt 30 and age lt 50))";
let result = parse_str.expect;
// Expression: ((name = 'John') AND (isActive = true)) OR ((age > 30) AND (age < 50))
Functions with Comparisons
use parse_str;
let filter = "concat(concat(city, ', '), country) eq 'Berlin, Germany'";
let result = parse_str.expect;
// Expression: concat(concat(city, ', '), country) = 'Berlin, Germany'