Skip to main content

Graph

Struct Graph 

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

A collection of named JSON documents that can be queried together via the v2 VM.

§Thread safety

Graph is Send + Sync (nodes are plain serde_json::Value). The internal VM is not Sync, so wrap in Mutex for concurrent use.

Implementations§

Source§

impl Graph

Source

pub fn new() -> Self

Create an empty graph with a fresh VM.

Source

pub fn with_capacity(compile_cap: usize, resolution_cap: usize) -> Self

Create a graph backed by a VM with custom capacity hints.

compile_cap — compile-cache slots; resolution_cap — resolution-cache slots.

Source

pub fn add_node<S: Into<String>>(&mut self, name: S, value: Value) -> &mut Self

Add (or replace) a named node. Returns &mut self for chaining.

Source

pub fn get_node(&self, name: &str) -> Option<&Value>

Return a reference to a node value, if it exists.

Source

pub fn remove_node(&mut self, name: &str) -> Option<Value>

Remove a node, returning its value if it existed.

Source

pub fn len(&self) -> usize

Return the number of nodes in the graph.

Source

pub fn is_empty(&self) -> bool

Return true if the graph has no nodes.

Source

pub fn node_names(&self) -> impl Iterator<Item = &str>

Return an iterator over node names.

Source

pub fn query(&mut self, expr: &str) -> Result<Value, GraphError>

Evaluate a v2 expression against the virtual root — a JSON object { "node_name": value, ... } containing all nodes.

Navigate into a node via $.node_name....

Source

pub fn query_node( &mut self, node: &str, expr: &str, ) -> Result<Value, GraphError>

Evaluate a v2 expression against a single named node directly.

Source

pub fn register_method( &mut self, name: impl Into<String>, method: impl Method + 'static, )

Register a custom method callable from query expressions.

Source

pub fn vm(&self) -> &VM

Access the underlying VM (e.g. to inspect cache stats).

Source

pub fn vm_mut(&mut self) -> &mut VM

Mutable access to the underlying VM.

Source

pub fn virtual_root(&self) -> Value

Build the virtual root object containing all nodes as top-level fields.

Source

pub fn message(&mut self, schema: &str) -> Result<Value, GraphError>

Alias for query — reads better when constructing message schemas.

graph.message(r#"{ "total": $.orders.sum(price), "count": $.orders.len() }"#)

Trait Implementations§

Source§

impl Default for Graph

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Graph

§

impl !RefUnwindSafe for Graph

§

impl Send for Graph

§

impl Sync for Graph

§

impl Unpin for Graph

§

impl UnsafeUnpin for Graph

§

impl !UnwindSafe for Graph

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.