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
impl Query
sourcepub fn is_root_node_query(&self) -> bool
pub fn is_root_node_query(&self) -> bool
Determines if this is a root query (“.”).
source§impl Query
impl Query
sourcepub fn execute<'a>(&self, element: Element<'a>) -> Element<'a>
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());
sourcepub fn execute_all<'a>(&self, element: Element<'a>) -> Vec<Element<'a>>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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