AIP Filter Parser with SeaORM Support
A Rust implementation of Google AIP-160 filtering standard with SeaORM integration.
Features
- π Full AIP-160 Support - Parse Google AIP-160 filter expressions
- ποΈ SeaORM Integration - Convert filters directly to SeaORM conditions (3 easy ways!)
- π― Type Safe - Strongly typed AST and error handling
- β‘ Fast - Built with the Pest parser for performance
- π§ͺ Well Tested - Comprehensive test coverage
- πͺ Zero Boilerplate - One-line macro for instant SeaORM integration
Installation
[]
# Basic parsing only
= "0.1"
# With SeaORM support
= { = "0.1", = ["sea-orm"] }
Quick Start
Basic Parsing
use parse_filter;
SeaORM Integration
There are 3 ways to integrate with SeaORM, from simplest to most flexible:
Method 1: Using the Macro (Recommended - Simplest)
use ;
use *;
// Define your entity as usual
// That's it! One line to enable filtering
impl_field_mapper!;
// Use it
async
Method 2: Using column_from_str Helper
use ;
use *;
Method 3: Manual Mapping (Most Flexible)
use ;
use *;
Supported Syntax
Comparison Operators
| Operator | Description | Example |
|---|---|---|
= |
Equal | name = "John" |
!= |
Not equal | status != "inactive" |
> |
Greater than | age > 18 |
>= |
Greater than or equal | age >= 18 |
< |
Less than | age < 65 |
<= |
Less than or equal | age <= 65 |
: |
Contains/Has | email : "@gmail.com" |
Logical Operators
| Operator | Description | Example |
|---|---|---|
AND |
Logical AND | active = true AND age > 18 |
OR |
Logical OR | status = "active" OR status = "pending" |
NOT |
Logical NOT | NOT deleted = true |
( ) |
Grouping | (a = 1 OR a = 2) AND b = 3 |
Value Types
| Type | Example | Notes |
|---|---|---|
| String | "value" or 'value' |
Double or single quotes |
| Number | 42, 3.14, -10, 1e5 |
Integer or floating point |
| Boolean | true, false |
Case insensitive |
| Null | null |
Case insensitive |
Example Filters
# Simple equality
name = "John Doe"
# Numeric comparison
age > 18 AND age < 65
# Multiple conditions
active = true AND age > 18 AND verified = true
# OR conditions
status = "active" OR status = "pending"
# Grouped expressions
(status = "active" OR status = "pending") AND age > 18
# Contains/substring matching
email : "@example.com"
# NOT operator
NOT deleted = true
# Complex query
(active = true OR status = "trial") AND age >= 18 AND email : "@company.com"
# Null checks
middle_name = null
Architecture
βββββββββββββββ
β Filter Text β
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββ
β Parser β (Pest PEG Parser)
β filter.pest β
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββ
β AST β (Abstract Syntax Tree)
β ast.rs β
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββββββ
β SeaORM Converterβ (Feature: sea-orm)
β sea_orm.rs β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β SeaORM Conditionβ
βββββββββββββββββββ
API Reference
Main Functions
parse_filter(input: &str) -> Result<Filter>
Parse a filter string into an AST.
Filter::to_condition<M: FieldMapper>(&self, mapper: &M) -> Result<Condition>
Convert a filter to a SeaORM condition.
Traits
FieldMapper
Implement this trait to map filter field names to SeaORM columns.
Types
Filter- Root filter structureExpression- Filter expression (AND, OR, NOT, Restriction)Restriction- A comparison (field, comparator, value)Comparator- Comparison operatorValue- Filter value (String, Number, Boolean, Null)
Error Handling
The library uses the FilterError enum for all errors:
Testing
Run the tests:
Run the demo:
Run the SeaORM example:
Features
default- Includessea-ormsea-orm- Enable SeaORM integration
To use without SeaORM:
[]
= { = "path/to/aip-filter", = false }
Google AIP-160 Compliance
This implementation follows the Google AIP-160 standard for filtering. Key features:
- β Comparison operators (=, !=, <, <=, >, >=)
- β Logical operators (AND, OR, NOT)
- β Grouping with parentheses
- β String, number, boolean, and null values
- β Has/contains operator (:)
- β οΈ Partial: Function calls (not yet implemented)
- β οΈ Partial: Nested field access (parsed but not fully supported in SeaORM conversion)
License
MIT or Apache-2.0
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.