Skip to main content

Doc

Struct Doc 

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

A finished document, held in the composer’s arena.

Doc is a Copy handle: passing it around costs nothing and every navigator taken from it shares the composer’s lifetime, so a node of one document can be spliced into another without any lifetime work.

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::document::SerializeOptions;
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("<!--first--><a><b/></a>")?;
assert_eq!(doc.children().len(), 2);
assert_eq!(doc.document_element().unwrap().local_name(), "a");
assert_eq!(doc.to_xml(&SerializeOptions::default())?, "<!--first--><a><b/></a>");

Implementations§

Source§

impl<'a> Doc<'a>

Source

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

A navigator on the document node.

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/></a>")?;

use xsd_schema::navigator::{DomNavigator, DomNodeType};
assert_eq!(doc.root().node_type(), DomNodeType::Root);
Source

pub fn document_element(&self) -> Option<Nav<'a>>

The first top-level element, if the document has one.

A document built with build_sequence may have none, or several; use children for those.

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("<!--lead--><a/>")?;

use xsd_schema::navigator::DomNavigator;
assert_eq!(doc.document_element().unwrap().local_name(), "a");
Source

pub fn children(&self) -> Vec<Nav<'a>>

Every top-level node, in document order.

This is how a sequence of elements becomes queryable: build it with build_sequence, then bind children() to a variable.

use bumpalo::Bump;
use xsd_schema::compose::{Composer, Content, Form, IntoXPathValue, Name};
use xsd_schema::namespace::NameTable;

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

let mut count = Form::new(Name::local("nbids"));
count.push(Content::Text("2".to_string()));
let mut first = Form::new(Name::local("bid_count"));
first.push(Content::Element(count));

let mut count = Form::new(Name::local("nbids"));
count.push(Content::Text("5".to_string()));
let mut second = Form::new(Name::local("bid_count"));
second.push(Content::Element(count));

let seq = c.build_sequence(vec![first, second])?;
let total = c.eval(
    "sum($s/nbids)",
    &["s"],
    None,
    vec![("s", seq.children().into_xpath_value())],
)?;
assert_eq!(total.string()?, "7");
Source

pub fn to_xml(&self, opts: &SerializeOptions) -> Result<String, ComposeError>

Writes the document as XML.

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::document::SerializeOptions;
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></a>")?;

// Compact by default; `indent` asks for layout instead.
assert_eq!(doc.to_xml(&SerializeOptions::default())?, "<a><b>1</b></a>");
let formatted = SerializeOptions { indent: Some(2), ..SerializeOptions::default() };
assert_eq!(doc.to_xml(&formatted)?, "<a>\n  <b>1</b>\n</a>");
Source

pub fn write_xml<W: Write>( &self, writer: W, opts: &SerializeOptions, ) -> Result<(), ComposeError>

Writes the document as XML into writer, without an intermediate String.

use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::document::SerializeOptions;
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/>")?;

let mut out: Vec<u8> = Vec::new();
doc.write_xml(&mut out, &SerializeOptions::default())?;
assert_eq!(String::from_utf8(out)?, "<a/>");
Source

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

The underlying document.

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/>")?;

// No schema set was configured, so the document is bound to none.
assert!(doc.inner().schema_set().is_none());

Trait Implementations§

Source§

impl<'a> Clone for Doc<'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<'a> Copy for Doc<'a>

Source§

impl Debug for Doc<'_>

Source§

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

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

impl<'a> IntoContextNode<'a> for Doc<'a>

Source§

fn into_context_node(self) -> Nav<'a>

Converts self into the navigator to focus on.
Source§

impl<'a> IntoContextNode<'a> for &Doc<'a>

Source§

fn into_context_node(self) -> Nav<'a>

Converts self into the navigator to focus on.
Source§

impl<'a> IntoXPathValue<'a> for Doc<'a>

Source§

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

Converts self into a bindable sequence.
Source§

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

Source§

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

Converts self into a bindable sequence.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<'a> Forget for Doc<'a>

§

impl<'a> Freeze for Doc<'a>
where &'a BufferDocument<'a>: Freeze,

§

impl<'a> Move for Doc<'a>

§

impl<'a> Unpin for Doc<'a>
where &'a BufferDocument<'a>: Unpin,

§

impl<'a> UnsafeUnpin for Doc<'a>
where &'a BufferDocument<'a>: UnsafeUnpin,

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.