Skip to main content

Source

Trait Source 

Source
pub trait Source<'a> {
    // Required method
    fn read(&self, sink: &mut Sink<'_, 'a>) -> Result<(), String>;

    // Provided method
    fn read_field(
        &self,
        key: &str,
        sink: &mut Sink<'_, 'a>,
    ) -> Result<bool, String> { ... }
}
Expand description

Input read one level at a time, for evaluations whose input is not a serde_json::Value (see Expression::evaluate_source and ADR 0010).

A source stands for an array, an object, or the input itself. Each level is read once, on the evaluation’s first access to it, and only the levels the expression reaches are read. A source that borrows nothing implements Source<'a> for every 'a.

Required Methods§

Source

fn read(&self, sink: &mut Sink<'_, 'a>) -> Result<(), String>

Writes the values of this level to sink, in order: the items of an array, the fields of an object (each value after its key, given with Sink::key), or the input itself as the only value (nothing when it is undefined). Nested arrays and objects are written as new sources, read when the evaluation reaches them.

An error stops the evaluation with U1004, whose message is the error’s. The level is left empty, and the evaluation fails at its next step, before it calls a host function, or before it returns its result.

Provided Methods§

Source

fn read_field(&self, key: &str, sink: &mut Sink<'_, 'a>) -> Result<bool, String>

Writes the value of the field key of the object this source stands for, as the only value (nothing when the object has no such field), and returns true. A source that reads its object whole only returns false, writing nothing; that is the default.

A path step calls it on an object not read whole yet. The object keeps an array or an object written this way, so that it is read once; when the object is needed whole, its level is read with Source::read, and the arrays and objects read before keep their values. A scalar is not kept: the step reads it again. Implement it when one field costs less than the whole level, as a lookup in a hash map does. An error stops the evaluation as it does in Source::read.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§