Skip to main content

Expression

Struct Expression 

Source
pub struct Expression { /* private fields */ }
Expand description

A compiled JSONata expression.

Compile once with Expression::compile and evaluate many times. A compiled expression is immutable and can be shared between threads.

Implementations§

Source§

impl Expression

Source

pub fn compile(source: &str) -> Result<Self>

Parses and compiles a JSONata expression.

Source

pub fn source(&self) -> &str

Returns the source text this expression was compiled from.

Source

pub fn ast(&self) -> &Ast

The syntax tree of the expression.

Source

pub fn evaluate( &self, input: Option<&JsonValue>, bindings: &Bindings, ) -> Result<Option<JsonValue>>

Evaluates the expression against input with default Options.

None as input means undefined (no input document). A result of Ok(None) means the expression evaluated to undefined.

Source

pub fn evaluate_with( &self, input: Option<&JsonValue>, bindings: &Bindings, options: &Options, ) -> Result<Option<JsonValue>>

Evaluates the expression with explicit Options (limits and guardrails).

Source

pub fn evaluate_source<'a>( &self, input: &dyn Source<'a>, bindings: &'a Bindings, options: &Options, ) -> Result<Option<JsonValue>>

Evaluates the expression against an input read from a Source, one level at a time, as the evaluation reaches it. Use it for an input that is not a serde_json::Value, without converting it first.

input writes the input document as the only value of its level, or nothing for undefined. A level that the source fails to read stops the evaluation with U1004.

Source

pub fn evaluate_document( &self, document: &Document, bindings: &Bindings, options: &Options, ) -> Result<Option<JsonValue>>

Evaluates the expression against a Document converted beforehand. Use it to evaluate several expressions, or the same one several times, against one large document without converting it each time.

Source

pub fn evaluate_traced( &self, input: Option<&JsonValue>, bindings: &Bindings, options: &Options, tracer: &mut dyn Tracer, ) -> Result<Option<JsonValue>>

Evaluates the expression, reporting every evaluation event to tracer.

See the audit module for the events and the tracers jlens provides.

Source

pub fn evaluate_audited( &self, input: Option<&JsonValue>, bindings: &Bindings, options: &Options, audit: &AuditOptions, ) -> (Result<Option<JsonValue>>, AuditTree)

Evaluates the expression and records an audit::AuditTree of how the result was reached. The audit is returned even when the evaluation fails.

use jlens_core::{Bindings, Expression, Options};
use jlens_core::audit::{AuditOptions, OutcomeKind};

let expr = Expression::compile("$x > 10").unwrap();
let bindings = Bindings::new().var("x", 3);
let (result, audit) =
    expr.evaluate_audited(None, &bindings, &Options::default(), &AuditOptions::decisions());
assert_eq!(result.unwrap(), Some(serde_json::json!(false)));
let root = audit.root.unwrap();
assert_eq!(root.outcome, OutcomeKind::False);
assert_eq!(root.operands, Some([Some(3.into()), Some(10.into())]));

Trait Implementations§

Source§

impl Clone for Expression

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Expression

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.