OData V4 Query String Parser for Rust
A Rust library that parses OData V4 query strings into an AST (Abstract Syntax Tree) and renders them into multiple SQL dialects.
Features
- 🚀 Parse OData V4 query strings (
$select,$top,$skip) - 🎯 Type-safe AST representation
- 🔄 Render to multiple SQL dialects:
- MSSQL/SQL Server - Uses
TOPandOFFSET...ROWSsyntax - SQLite - Uses
LIMITandOFFSETsyntax - PostgreSQL - Uses
LIMITandOFFSETsyntax - SurrealQL - Uses
STARTandLIMITsyntax
- MSSQL/SQL Server - Uses
- ⚡ Zero-cost abstractions with compile-time safety
- 📝 Informative error messages with position tracking
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Example
use ;
Parsing Individual Options
use parse;
// Parse $select
let options = parse.unwrap;
assert_eq!;
// Parse $top
let options = parse.unwrap;
assert_eq!;
// Parse $skip
let options = parse.unwrap;
assert_eq!;
Error Handling
use ;
match parse
Supported OData V4 Features
Currently implemented:
- ✅
$select- Field selection - ✅
$top- Limit number of results - ✅
$skip- Skip N results (pagination)
Coming soon:
- 🔜
$filter- Filter expressions - 🔜
$orderby- Sorting - 🔜
$expand- Navigation properties - 🔜
$count- Include total count - 🔜
$search- Full-text search
Architecture
The library follows a classic compiler architecture:
Query String → Lexer → Tokens → Parser → AST → Renderer → SQL
- Lexer (
lexer.rs) - Tokenizes the input string - Parser (
parser.rs) - Builds an AST from tokens - AST (
ast.rs) - Type-safe representation of query options - Renderers (
renderers/*.rs) - Generate SQL for specific dialects
SQL Dialect Differences
| Feature | MSSQL | SQLite | PostgreSQL | SurrealQL |
|---|---|---|---|---|
| Limit | TOP N (before SELECT) |
LIMIT N |
LIMIT N |
LIMIT N |
| Offset | OFFSET N ROWS |
OFFSET N |
OFFSET N |
START N |
| Identifier Quote | [name] |
"name" |
"name" |
name |
Development
Running Tests
Running Tests with Output
Linting
Formatting
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT