Skip to main content

Document

Struct Document 

Source
pub struct Document {
    pub version: Option<YamlVersion>,
    pub tag_handles: Vec<TagHandle>,
    /* private fields */
}
Expand description

A parsed YAML document: an arena of nodes reachable from a single root.

Fields§

§version: Option<YamlVersion>

The %YAML directive, if the document carried one.

§tag_handles: Vec<TagHandle>

The %TAG directives, in the order they appeared.

Implementations§

Source§

impl Document

Source

pub fn new() -> Self

An empty document with no root.

Source

pub fn new_asdf() -> Self

An empty document carrying the directives ASDF conventionally writes.

Source

pub fn node_count(&self) -> usize

The number of nodes in the arena.

This counts every node including those only reachable through an alias, so it is not the same as the number of distinct tree positions.

Source

pub fn root(&self) -> Option<NodeId>

The document’s root node, if it has one.

Source

pub fn set_root(&mut self, id: NodeId)

Set the document’s root node.

Source

pub fn add(&mut self, node: Node) -> NodeId

Add a node to the arena and return its id.

Source

pub fn node(&self, id: NodeId) -> &Node

Borrow a node.

§Panics

Panics if id did not come from this document.

Source

pub fn node_mut(&mut self, id: NodeId) -> &mut Node

Mutably borrow a node.

§Panics

Panics if id did not come from this document.

Source

pub fn get(&self, id: NodeId) -> Option<&Node>

Borrow a node if the id is in range.

Source

pub fn resolve(&self, id: NodeId) -> NodeId

Follow aliases until reaching a non-alias node.

Alias chains are bounded by the arena size, so a cycle – which a conforming YAML document cannot contain, but a hand-edited one might – terminates rather than looping forever.

Source

pub fn resolved(&self, id: NodeId) -> &Node

Borrow a node, following aliases first.

Source

pub fn tag_of(&self, id: NodeId) -> Option<&Tag>

The effective tag of a node, following aliases.

An alias node carries no tag of its own, so the tag of its target is what callers mean when they ask.

Source

pub fn add_scalar(&mut self, value: impl Into<String>) -> NodeId

Add a plain scalar node.

Source

pub fn add_scalar_styled( &mut self, value: impl Into<String>, style: ScalarStyle, ) -> NodeId

Add a scalar node with an explicit style.

Source

pub fn add_sequence(&mut self, items: Vec<NodeId>) -> NodeId

Add a sequence node built from existing nodes.

Source

pub fn add_mapping(&mut self, pairs: Vec<(NodeId, NodeId)>) -> NodeId

Add a mapping node built from existing key/value node pairs.

Source

pub fn sequence_items(&self, id: NodeId) -> Option<&[NodeId]>

The items of a sequence node, following aliases.

Source

pub fn mapping_entries(&self, id: NodeId) -> Option<&[Entry]>

The entries of a mapping node, following aliases.

Source

pub fn mapping_get(&self, id: NodeId, key: &str) -> Option<NodeId>

Look up a mapping value by string key, following aliases.

Where a key appears more than once – which YAML permits and ASDF files occasionally contain – the first occurrence wins, matching how a streaming reader would see it.

Source

pub fn mapping_set( &mut self, id: NodeId, key: &str, value: NodeId, ) -> Option<NodeId>

Insert or replace a mapping entry by string key.

Returns the previous value node when the key was already present.

Source

pub fn mapping_remove(&mut self, id: NodeId, key: &str) -> Option<NodeId>

Remove a mapping entry by string key, returning the value node.

Source

pub fn sequence_get(&self, id: NodeId, index: i64) -> Option<NodeId>

Index into a sequence, following aliases and accepting negative indices that count back from the end.

Source

pub fn container_len(&self, id: NodeId) -> Option<usize>

The number of children of a container node, or None for scalars.

Source§

impl Document

Source

pub fn lookup_from(&self, start: NodeId, path: &Path) -> Option<NodeId>

Resolve a path against a starting node.

Aliases are followed at each step, so a path may descend through a shared node.

Source

pub fn lookup(&self, path: &Path) -> Option<NodeId>

Resolve a path against the document root.

Source

pub fn lookup_str(&self, path: &str) -> Option<NodeId>

Parse and resolve a path in one step.

Source

pub fn parent_of(&self, id: NodeId) -> Option<NodeId>

The container that holds id, searching down from the root.

A node reachable by more than one route reports the first parent found in document order. Aliases are not followed: an alias node is its own child of whatever holds it, and the node it points at is reported under its defining position.

Source

pub fn path_of(&self, id: NodeId) -> Option<String>

The path from the root to id, in the syntax Path::parse accepts.

Absolute, so /history/extensions/0 rather than history/extensions/0, and a sequence index is written plainly rather than bracketed – that is the form libasdf reports and the one its own tests expect. A bare numeric component is unambiguous on the way back in, since it is read as an index only when the container it addresses is a sequence.

The root itself is /. Returns None when id is not reachable from the root, which is the case for a node built but not yet attached.

Source§

impl Document

Source

pub fn insert_at( &mut self, path: &Path, value: NodeId, ) -> Result<Option<NodeId>, PathError>

Insert value at path, creating intermediate mappings as needed.

This mirrors libasdf’s asdf_node_insert_at with materialisation on: setting powers/squares in an empty tree creates the powers mapping on the way. Returns the node that was replaced, if any.

Intermediate steps are only ever created as mappings, since a bare path component gives no way to say “make a sequence here”. A component that names an existing sequence still indexes into it.

Source

pub fn insert_at_str( &mut self, path: &str, value: NodeId, ) -> Result<Option<NodeId>, PathError>

Parse and insert in one step.

Source

pub fn sequence_set( &mut self, id: NodeId, index: i64, value: NodeId, ) -> Option<NodeId>

Replace a sequence element, or append when the index is one past the end. Returns the element that was replaced.

Source

pub fn remove_at_str(&mut self, path: &str) -> Option<NodeId>

Remove whatever is at path, returning it.

Source

pub fn sequence_remove(&mut self, id: NodeId, index: i64) -> Option<NodeId>

Remove a sequence element, returning it.

Trait Implementations§

Source§

impl Clone for Document

Source§

fn clone(&self) -> Document

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 Document

Source§

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

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

impl Default for Document

Source§

fn default() -> Self

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

Auto Trait Implementations§

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