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
impl Document
Sourcepub fn new_asdf() -> Self
pub fn new_asdf() -> Self
An empty document carrying the directives ASDF conventionally writes.
Sourcepub fn node_count(&self) -> usize
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.
Sourcepub fn resolve(&self, id: NodeId) -> NodeId
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.
Sourcepub fn tag_of(&self, id: NodeId) -> Option<&Tag>
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.
Sourcepub fn add_scalar(&mut self, value: impl Into<String>) -> NodeId
pub fn add_scalar(&mut self, value: impl Into<String>) -> NodeId
Add a plain scalar node.
Sourcepub fn add_scalar_styled(
&mut self,
value: impl Into<String>,
style: ScalarStyle,
) -> NodeId
pub fn add_scalar_styled( &mut self, value: impl Into<String>, style: ScalarStyle, ) -> NodeId
Add a scalar node with an explicit style.
Sourcepub fn add_sequence(&mut self, items: Vec<NodeId>) -> NodeId
pub fn add_sequence(&mut self, items: Vec<NodeId>) -> NodeId
Add a sequence node built from existing nodes.
Sourcepub fn add_mapping(&mut self, pairs: Vec<(NodeId, NodeId)>) -> NodeId
pub fn add_mapping(&mut self, pairs: Vec<(NodeId, NodeId)>) -> NodeId
Add a mapping node built from existing key/value node pairs.
Sourcepub fn sequence_items(&self, id: NodeId) -> Option<&[NodeId]>
pub fn sequence_items(&self, id: NodeId) -> Option<&[NodeId]>
The items of a sequence node, following aliases.
Sourcepub fn mapping_entries(&self, id: NodeId) -> Option<&[Entry]>
pub fn mapping_entries(&self, id: NodeId) -> Option<&[Entry]>
The entries of a mapping node, following aliases.
Sourcepub fn mapping_get(&self, id: NodeId, key: &str) -> Option<NodeId>
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.
Sourcepub fn mapping_set(
&mut self,
id: NodeId,
key: &str,
value: NodeId,
) -> Option<NodeId>
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.
Sourcepub fn mapping_remove(&mut self, id: NodeId, key: &str) -> Option<NodeId>
pub fn mapping_remove(&mut self, id: NodeId, key: &str) -> Option<NodeId>
Remove a mapping entry by string key, returning the value node.
Sourcepub fn sequence_get(&self, id: NodeId, index: i64) -> Option<NodeId>
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.
Sourcepub fn container_len(&self, id: NodeId) -> Option<usize>
pub fn container_len(&self, id: NodeId) -> Option<usize>
The number of children of a container node, or None for scalars.
Source§impl Document
impl Document
Sourcepub fn lookup_from(&self, start: NodeId, path: &Path) -> Option<NodeId>
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.
Sourcepub fn lookup_str(&self, path: &str) -> Option<NodeId>
pub fn lookup_str(&self, path: &str) -> Option<NodeId>
Parse and resolve a path in one step.
Sourcepub fn parent_of(&self, id: NodeId) -> Option<NodeId>
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.
Sourcepub fn path_of(&self, id: NodeId) -> Option<String>
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
impl Document
Sourcepub fn insert_at(
&mut self,
path: &Path,
value: NodeId,
) -> Result<Option<NodeId>, PathError>
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.
Sourcepub fn insert_at_str(
&mut self,
path: &str,
value: NodeId,
) -> Result<Option<NodeId>, PathError>
pub fn insert_at_str( &mut self, path: &str, value: NodeId, ) -> Result<Option<NodeId>, PathError>
Parse and insert in one step.
Sourcepub fn sequence_set(
&mut self,
id: NodeId,
index: i64,
value: NodeId,
) -> Option<NodeId>
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.
Sourcepub fn remove_at_str(&mut self, path: &str) -> Option<NodeId>
pub fn remove_at_str(&mut self, path: &str) -> Option<NodeId>
Remove whatever is at path, returning it.