Struct jupiter::ig::docs::Query

source ·
pub struct Query { /* private fields */ }
Expand description

Represents a pre-compiled query.

A query can be obtained using Doc::compile. And then repeatedly being executed using Query::execute{Query::execute}.

Compiling a query essentially splits the given query string at “.” and then resolves each part of the query into a Symbol. Therefore a query compiled for one Doc cannot be used for another as its result would be entirely undefined. Also note that compiling queries is rather fast, therefore ad-hoc queries can be executed using Element::query.

Implementations§

source§

impl Query

source

pub fn is_root_node_query(&self) -> bool

Determines if this is a root query (“.”).

source§

impl Query

source

pub fn execute<'a>(&self, element: Element<'a>) -> Element<'a>

Executes the query against the given element.

Note that the element must be part of the same doc for which the query has been compiled otherwise the return value is undefined.

§Example
let builder = DocBuilder::new();
let mut object_builder = builder.obj();
let mut inner_builder = builder.obj();
inner_builder.put_int("Bar", 42).unwrap();
object_builder.put_object("Foo", inner_builder).unwrap();

let doc = builder.build_object(object_builder);

let query = doc.compile("Foo.Bar");
assert_eq!(query.execute(doc.root()).as_int().unwrap(), 42);

let query = doc.compile("XXX.UNKNOWN");
assert!(query.execute(doc.root()).is_empty());

let query = doc.compile(".");
assert!(query.execute(doc.root()).is_object());
source

pub fn execute_all<'a>(&self, element: Element<'a>) -> Vec<Element<'a>>

Executes this query against the given element and returns all matches.

§Example

There might be several matches if there is an inner list in the doc:


use yaml_rust::YamlLoader;
use jupiter::ig::yaml::list_to_doc;
let input = r#"
test:
    - foo:
        label: bar
    - foo:
        label: baz
        "#;

let rows = YamlLoader::load_from_str(input).unwrap();
let doc = list_to_doc(rows.as_slice(), |_| true).unwrap();


let query = doc.compile("test.foo.label");
let matches = query.execute_all(doc.root());
assert_eq!(matches.len(), 2);
assert_eq!(matches[0].as_str().unwrap(), "bar");
assert_eq!(matches[1].as_str().unwrap(), "baz");

Trait Implementations§

source§

impl Clone for Query

source§

fn clone(&self) -> Query

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Query

§

impl RefUnwindSafe for Query

§

impl Send for Query

§

impl Sync for Query

§

impl Unpin for Query

§

impl UnwindSafe for Query

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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>,

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more