pub struct Node { /* private fields */ }Expand description
A node of a bracket file: a Value plus the position it was parsed from.
Quoted strings and atoms are kept distinct, so text written by the platform is written back byte for byte. Whitespace between items is not kept: writing uses the platform layout. Equality and hashing ignore the position.
Like parsing and writing, cloning, comparison, hashing, Debug and dropping
are not recursive: a tree of any depth does not overflow the stack.
Implementations§
Source§impl Node
impl Node
Sourcepub fn list(items: impl IntoIterator<Item = Node>) -> Self
pub fn list(items: impl IntoIterator<Item = Node>) -> Self
A list of nodes.
Sourcepub fn string(value: impl Into<String>) -> Self
pub fn string(value: impl Into<String>) -> Self
A quoted string.
There is no From<&str> for building: Node::try_from(&str) parses text.
Sourcepub fn counted(items: impl IntoIterator<Item = Node>) -> Self
pub fn counted(items: impl IntoIterator<Item = Node>) -> Self
A counted list {count,item1,item2,...}, the common 1C shape.
pub fn value(&self) -> &Value
pub fn into_value(self) -> Value
Sourcepub fn value_mut(&mut self) -> &mut Value
pub fn value_mut(&mut self) -> &mut Value
Mutable access to the content; the position stays where the node was parsed.
Sourcepub fn position(&self) -> Option<Position>
pub fn position(&self) -> Option<Position>
Where the node starts in the input; None for nodes built in code.
Edits do not change it: it always refers to the original text.
pub fn is_list(&self) -> bool
pub fn is_str(&self) -> bool
pub fn is_atom(&self) -> bool
Sourcepub fn is_empty_atom(&self) -> bool
pub fn is_empty_atom(&self) -> bool
An empty atom: no value between commas ({1,,2}).
pub fn as_list(&self) -> Option<&[Node]>
Sourcepub fn as_parsed<T: FromStr>(&self) -> Option<T>
pub fn as_parsed<T: FromStr>(&self) -> Option<T>
Atom parsed into any FromStr type: node.as_parsed::<u32>().
pub fn as_i64(&self) -> Option<i64>
pub fn as_u64(&self) -> Option<u64>
pub fn as_f64(&self) -> Option<f64>
Sourcepub fn as_guid(&self) -> Option<&str>
pub fn as_guid(&self) -> Option<&str>
An atom shaped like a GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
Sourcepub fn as_base64(&self) -> Option<String>
pub fn as_base64(&self) -> Option<String>
Payload of a #base64:… atom without the prefix and line breaks (not decoded).
Sourcepub fn get_path(&self, path: &[usize]) -> Option<&Node>
pub fn get_path(&self, path: &[usize]) -> Option<&Node>
Item by a path of indices from this node: node.get_path(&[1, 0, 2]).
pub fn into_list(self) -> Option<Vec<Node>>
Sourcepub fn into_string(self) -> Option<String>
pub fn into_string(self) -> Option<String>
Value of a string or an atom.
pub fn try_list(&self) -> Result<&[Node], AccessError>
pub fn try_str(&self) -> Result<&str, AccessError>
pub fn try_quoted(&self) -> Result<&str, AccessError>
pub fn try_atom(&self) -> Result<&str, AccessError>
Sourcepub fn try_parse<T>(&self) -> Result<T, AccessError>
pub fn try_parse<T>(&self) -> Result<T, AccessError>
Atom parsed into any FromStr type: node.try_parse::<u32>()?.
The error names the target type and keeps the reason from FromStr::Err.
pub fn try_i64(&self) -> Result<i64, AccessError>
pub fn try_u64(&self) -> Result<u64, AccessError>
pub fn try_f64(&self) -> Result<f64, AccessError>
pub fn try_bool(&self) -> Result<bool, AccessError>
pub fn try_guid(&self) -> Result<&str, AccessError>
pub fn try_get(&self, idx: usize) -> Result<&Node, AccessError>
Sourcepub fn try_get_path(&self, path: &[usize]) -> Result<&Node, AccessError>
pub fn try_get_path(&self, path: &[usize]) -> Result<&Node, AccessError>
Item by a path of indices: node.try_get_path(&[0, 2, 0])?.
On failure the error carries the path to the node that could not be indexed.
Sourcepub fn as_list_mut(&mut self) -> Option<&mut Vec<Node>>
pub fn as_list_mut(&mut self) -> Option<&mut Vec<Node>>
List items for editing: replace, push, insert or remove nodes.
pub fn get_mut(&mut self, idx: usize) -> Option<&mut Node>
pub fn get_path_mut(&mut self, path: &[usize]) -> Option<&mut Node>
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, Node> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, Node> ⓘ
List items; an empty iterator for strings and atoms.
pub fn try_list_mut(&mut self) -> Result<&mut Vec<Node>, AccessError>
pub fn try_get_mut(&mut self, idx: usize) -> Result<&mut Node, AccessError>
Sourcepub fn try_get_path_mut(
&mut self,
path: &[usize],
) -> Result<&mut Node, AccessError>
pub fn try_get_path_mut( &mut self, path: &[usize], ) -> Result<&mut Node, AccessError>
Item by a path of indices for editing: *node.try_get_path_mut(&[1, 0])? = Node::from(2).
Sourcepub fn try_counted(&self) -> Result<&[Node], AccessError>
pub fn try_counted(&self) -> Result<&[Node], AccessError>
Items of a counted list {count,item1,item2,...}: {2,a,b} gives [a, b].
Fails if the count is not a number or does not match the number of items.
Source§impl Node
impl Node
Sourcepub fn from_reader<R: Read>(reader: R) -> Result<Node, ParseError>
pub fn from_reader<R: Read>(reader: R) -> Result<Node, ParseError>
Parses a single top-level value from any source.
A leading BOM and surrounding whitespace are allowed. Reads byte by byte through a buffer without loading the whole input into memory.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Node
Available on crate feature serde only.From this crate’s deserializer a node comes out as is, positions included.
impl<'de> Deserialize<'de> for Node
serde only.From this crate’s deserializer a node comes out as is, positions included.
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl Drop for Node
impl Drop for Node
impl Eq for Node
Source§impl<T: Into<Node>> FromIterator<T> for Node
impl<T: Into<Node>> FromIterator<T> for Node
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<'a> IntoIterator for &'a Node
impl<'a> IntoIterator for &'a Node
Source§impl<'a> IntoIterator for &'a mut Node
impl<'a> IntoIterator for &'a mut Node
Source§impl Serialize for Node
Available on crate feature serde only.Positions are not written: they belong to the parsed text.
impl Serialize for Node
serde only.Positions are not written: they belong to the parsed text.