Skip to main content

Module query_library

Module query_library 

Source
Expand description

Named Query Library parser for .jpx files.

This module provides parsing support for query library files that contain multiple named, reusable JMESPath expressions. The format is inspired by SQLDelight/HugSQL patterns.

§File Format

-- :name top-keywords
-- :desc Extract top keywords from text field
tokens(@) | remove_stopwords(@) | stems(@) | frequencies(@)

-- :name clean-html
-- :desc Strip HTML tags and normalize whitespace
regex_replace(@, `"<[^>]+>"`, `" "`) | collapse_whitespace(@)

§Directives

  • -- :name <name> - Starts a new query (required)
  • -- :desc <description> - Adds a description to the current query (optional)
  • -- - Other comment lines are ignored

Everything between -- :name directives becomes the query expression. Multi-line expressions are supported.

§Example

use jpx_core::query_library::QueryLibrary;

let content = r#"
-- :name greet
-- :desc Simple greeting
`"hello"`

-- :name count
length(@)
"#;

let library = QueryLibrary::parse(content).unwrap();

assert_eq!(library.names(), vec!["greet", "count"]);

let greet = library.get("greet").unwrap();
assert_eq!(greet.name, "greet");
assert_eq!(greet.description, Some("Simple greeting".to_string()));
assert_eq!(greet.expression, r#"`"hello"`"#);

let count = library.get("count").unwrap();
assert_eq!(count.expression, "length(@)");

§Detection

Use is_query_library to check if content looks like a query library:

use jpx_core::query_library::is_query_library;

assert!(is_query_library("-- :name foo\nlength(@)"));
assert!(!is_query_library("length(@)"));

Structs§

NamedQuery
A named query with optional description.
ParseError
Error type for query library parsing.
QueryLibrary
A collection of named queries parsed from a .jpx file.

Functions§

is_query_library
Check if content looks like a query library (starts with -- :name).