Skip to main content

Value

Struct Value 

Source
pub struct Value<'a>(/* private fields */);
Expand description

The result of one evaluation.

A Value is a thin wrapper around the engine’s own XPathValue: nothing is atomized, copied or flattened until a method asks for it. The accessors are the ones a host needs — the nodes, the atomic values, a boolean, a string, a number, an order-by key — and each says exactly when it refuses.

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a><b>1</b><b>2</b></a>")?;

let bs = c.eval("//b", &[], Some(doc.root()), Vec::new())?;
assert_eq!(bs.len(), 2);
assert_eq!(bs.nodes()?.len(), 2);

let total = c.eval("sum(//b)", &[], Some(doc.root()), Vec::new())?;
assert_eq!(total.number()?, 3.0);
assert_eq!(total.string()?, "3");

Implementations§

Source§

impl<'a> Value<'a>

Source

pub fn nodes(&self) -> Result<Vec<Nav<'a>>, ComposeError>

The node items, in order.

An atomic item is ComposeError::NotANode: dropping it silently would hide a mistake in the expression. pipe::nodes is the streaming counterpart.

use bumpalo::Bump;
use xsd_schema::compose::{ComposeError, Composer};
use xsd_schema::namespace::NameTable;
use xsd_schema::navigator::DomNavigator;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a><b/></a>")?;

let nodes = c.eval("//b", &[], Some(doc.root()), Vec::new())?.nodes()?;
assert_eq!(nodes[0].local_name(), "b");

// A string is not a node.
let atomic = c.eval("'x'", &[], None, Vec::new())?;
assert!(matches!(atomic.nodes(), Err(ComposeError::NotANode)));
Source

pub fn atomics(&self) -> Result<Vec<XmlValue>, ComposeError>

Every item atomized, in order.

A node is atomized with atomize_node, so an untyped element yields its xs:untypedAtomic string value and a typed one its typed value: one value per member when its type is a list type (XDM §3.3.1.2). A nilled element contributes nothing.

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a><b>1</b><b>2</b></a>")?;

let atomics = c.eval("//b", &[], Some(doc.root()), Vec::new())?.atomics()?;
let text: Vec<String> = atomics.iter().map(|v| v.to_string_value()).collect();
assert_eq!(text, ["1", "2"]);
Source

pub fn boolean(&self) -> Result<bool, ComposeError>

The effective boolean value (XPath 2.0 §2.4.3).

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a><b>x</b></a>")?;

assert!(c.eval("//b", &[], Some(doc.root()), Vec::new())?.boolean()?);
assert!(!c.eval("//zzz", &[], Some(doc.root()), Vec::new())?.boolean()?);
Source

pub fn string(&self) -> Result<String, ComposeError>

The string value of the single item, "" for the empty sequence.

More than one item is ComposeError::NotSingleton; use atomics to join a sequence yourself.

use bumpalo::Bump;
use xsd_schema::compose::{ComposeError, Composer};
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a><b>one</b><b>two</b></a>")?;

assert_eq!(c.eval("//b[1]", &[], Some(doc.root()), Vec::new())?.string()?, "one");
assert_eq!(c.eval("()", &[], None, Vec::new())?.string()?, "");
assert!(matches!(
    c.eval("//b", &[], Some(doc.root()), Vec::new())?.string(),
    Err(ComposeError::NotSingleton { len: 2 }),
));
Source

pub fn number(&self) -> Result<f64, ComposeError>

fn:number() of the single item: NaN when it does not convert, and NaN for the empty sequence.

More than one item is ComposeError::NotSingleton.

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);

assert_eq!(c.eval("2 + 3", &[], None, Vec::new())?.number()?, 5.0);
assert!(c.eval("'nope'", &[], None, Vec::new())?.number()?.is_nan());
assert!(c.eval("()", &[], None, Vec::new())?.number()?.is_nan());
Source

pub fn key(&self) -> Result<Option<XmlValue>, ComposeError>

The order by key: the single atomized item, or None for the empty sequence.

More than one item is XPTY0004, reported as ComposeError::XPath — an order-by key must be at most one value (XQuery 1.0 §3.8.3).

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a><b>7</b></a>")?;

let key = c.eval("b", &[], Some(doc.document_element().unwrap()), Vec::new())?.key()?;
assert_eq!(key.unwrap().to_string_value(), "7");
assert!(c.eval("()", &[], None, Vec::new())?.key()?.is_none());
Source

pub fn len(&self) -> usize

How many items the sequence holds.

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);

assert_eq!(c.eval("()", &[], None, Vec::new())?.len(), 0);
assert_eq!(c.eval("1", &[], None, Vec::new())?.len(), 1);
assert_eq!(c.eval("(1, 2, 3)", &[], None, Vec::new())?.len(), 3);
Source

pub fn is_empty(&self) -> bool

Whether the sequence is empty.

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);

assert!(c.eval("()", &[], None, Vec::new())?.is_empty());
assert!(!c.eval("1", &[], None, Vec::new())?.is_empty());
Source

pub fn iter(&self) -> impl Iterator<Item = &XmlItem<Nav<'a>>> + '_

Borrows the items, in order.

A one-item sequence is one item here: the engine stores a singleton without a backing vector, and this iterator yields it all the same.

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);

assert_eq!(c.eval("1", &[], None, Vec::new())?.iter().count(), 1);
assert_eq!(c.eval("(1, 2, 3)", &[], None, Vec::new())?.iter().count(), 3);
Source

pub fn into_inner(self) -> XPathValue<Nav<'a>>

The wrapped engine value.

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);

let sequence = c.eval("(1, 2)", &[], None, Vec::new())?.into_inner();
assert_eq!(sequence.into_vec().len(), 2);
Source

pub fn inner(&self) -> &XPathValue<Nav<'a>>

Borrows the wrapped engine value.

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);

let value = c.eval("(1, 2)", &[], None, Vec::new())?;
assert_eq!(value.inner().len(), 2);
// …and the `Value` is still usable afterwards.
assert_eq!(value.len(), 2);

Trait Implementations§

Source§

impl<'a> Clone for Value<'a>

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 Value<'_>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> From<XPathValue<BufferDocNavigator<'a>>> for Value<'a>

Source§

fn from(value: XPathValue<Nav<'a>>) -> Self

Converts to this type from the input type.
Source§

impl<'a> IntoContent<'a> for Value<'a>

Source§

fn into_content(self) -> Content<'a>

Converts self into one content item.
Source§

impl<'a> IntoContent<'a> for &Value<'a>

Source§

fn into_content(self) -> Content<'a>

Converts self into one content item.
Source§

impl<'a> IntoXPathValue<'a> for Value<'a>

Source§

fn into_xpath_value(self) -> XPathValue<Nav<'a>>

Converts self into a bindable sequence.
Source§

impl<'a> IntoXPathValue<'a> for &Value<'a>

Source§

fn into_xpath_value(self) -> XPathValue<Nav<'a>>

Converts self into a bindable sequence.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Value<'a>

§

impl<'a> !Send for Value<'a>

§

impl<'a> !Sync for Value<'a>

§

impl<'a> !UnwindSafe for Value<'a>

§

impl<'a> Forget for Value<'a>

§

impl<'a> Freeze for Value<'a>
where &'a BufferDocument<'a>: Freeze, *const u8 is !null: Freeze, PhantomData<NsRef>: Freeze, &'a SharedClock: Freeze, NonZero<u32>: Freeze, PhantomData<u8>: Freeze, PhantomData<u64>: Freeze, *const ArcInner<QNameParts> is !null: Freeze, PhantomData<ArcInner<QNameParts>>: Freeze, PhantomData<XmlValue>: Freeze, *const XmlValue is !null: Freeze, PhantomData<XmlItem<BufferDocNavigator<'a>>>: Freeze,

§

impl<'a> Move for Value<'a>

§

impl<'a> Unpin for Value<'a>
where &'a BufferDocument<'a>: Unpin, *const u8 is !null: Unpin, &'a SharedClock: Unpin, NonZero<u32>: Unpin, Arc<QNameParts>: Unpin, Box<XmlValue>: Unpin,

§

impl<'a> UnsafeUnpin for Value<'a>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> ErasedDestructor for T
where T: 'static,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.