A Rust parser for GROQ (Graph-Relational Object Queries), the query language created by Sanity.io for filtering and projecting JSON documents.
- Complete GROQ language support (version 1.3)
- Minimal dependencies
- Comprehensive error handling with source position tracking
- Extensively tested (~98 test cases)
This library may return floating point numbers that differ from the reference implementation due to differences in floating point representation and evaluation in Rust.
Add to your Cargo.toml:
[dependencies]
groq-parser = { path = "path/to/groq-parser" }use groq_parser::parser::Parser;
fn main() {
let query = r#"*[_type == "post"]{title, author->name}"#;
let mut parser = Parser::new(query);
match parser.parse() {
Ok(result) => println!("{:?}", result.expr),
Err(e) => eprintln!("Parse error: {}", e),
}
}# Debug build
cargo build
# Release build (optimized)
cargo build --release
# Run tests
cargo test --libThe comparison/ directory contains tools for comparing this parser against the reference
go-groq implementation.
# Build the Rust AST dumper
cargo build --release --bin dump_ast
# Build the Go AST dumper
cd comparison/go-dumper
go build -o go-dumper .
cd ../..Using the built GROQ test suite:
cd comparison
python3 compare.py ../groq-test-suite.ndjson ../target/release/dump_ast ./go-dumper/go-dumperFilter by GROQ spec version (excludes legacy 0.1 tests):
python3 compare.py ../groq-test-suite.ndjson ../target/release/dump_ast ./go-dumper/go-dumper --version 1.3MIT License - see LICENSE for details.