Trait rsonpath::result::QueryResultBuilder
source · pub trait QueryResultBuilder<'i, I: Input, R: QueryResult> {
// Required methods
fn new(input: &'i I) -> Self;
fn report(
&mut self,
index: usize,
hint: NodeTypeHint
) -> Result<(), EngineError>;
fn finish(self) -> R;
}
Expand description
A builder for a QueryResult
with access to the underlying input.
Required Methods§
sourcefn new(input: &'i I) -> Self
fn new(input: &'i I) -> Self
Create a new, empty builder, with access to the underlying JSON input.
Implementations should make sure that the result produced from an empty builder
is the same as the empty instance of that result (defined by the Default
trait).
In other words, for any builder
of result type R
it should hold that:
builder.new(input).finish() == R::default()
sourcefn report(
&mut self,
index: usize,
hint: NodeTypeHint
) -> Result<(), EngineError>
fn report( &mut self, index: usize, hint: NodeTypeHint ) -> Result<(), EngineError>
Report a match of the query. The index
is guaranteed to be between the first character
of the matched value and the previous structural character.
When the engine finds a match, it will usually occur at some structural character.
It is guaranteed that index
points to either:
- the first character of the matched value; or
- the character right after the colon or comma structural character directly preceding the matched value; or
- a whitespace character before the matched value, such that the next non-whitespace character is the first character of the matched value.
The builder should use the index
and the provided hint
to find the start of the
actual value being reported. Note that it is always possible to do so without the hint
(or, equivalently, when NodeTypeHint::Any
is given), but the hint can improve performance.
For example, when the hint is NodeTypeHint::Complex
with the curly bracket type, the
result builder can do a quick direct search for the next ‘{’ character.
{
"match": 42
// ^^^^^^^^
// any of these characters can be reported for the query $.match
}
{
"match": [42, 30]
// ^^^^^^
// any of these characters can be reported for the query $.match[1]
}
Errors
This function may access the input, which can raise an EngineError::InputError
.
More errors can occur if the input JSON is malformed (for example the document abruptly ends),
but they are not guaranteed to be detected or reported.