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. Whitespace inside an atom, trailing whitespace included, is part of its text. 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<Text>) -> Self
pub fn string(value: impl Into<Text>) -> 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_text(&self) -> Option<&Text>
pub fn as_text(&self) -> Option<&Text>
Text of a string or an atom, unpaired surrogates included.
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Value of a string or an atom; None if it holds an unpaired surrogate.
Sourcepub fn to_string_lossy(&self) -> Option<Cow<'_, str>>
pub fn to_string_lossy(&self) -> Option<Cow<'_, str>>
Value of a string or an atom with unpaired surrogates replaced by U+FFFD.
Sourcepub fn as_atom_trimmed(&self) -> Option<&str>
pub fn as_atom_trimmed(&self) -> Option<&str>
Atom without leading and trailing whitespace: {12\r\n} gives 12.
The typed accessors (as_i64, try_parse and the like) read this text.
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; unpaired surrogates are replaced by U+FFFD.
pub fn try_list(&self) -> Result<&[Node], AccessError>
pub fn try_text(&self) -> Result<&Text, AccessError>
Sourcepub fn try_str(&self) -> Result<&str, AccessError>
pub fn try_str(&self) -> Result<&str, AccessError>
Fails for a list and for text with an unpaired surrogate.
pub fn try_quoted(&self) -> Result<&str, AccessError>
Sourcepub fn try_atom(&self) -> Result<&str, AccessError>
pub fn try_atom(&self) -> Result<&str, AccessError>
Atom as written: trailing whitespace included.
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.
Text is UTF-8; a leading BOM and surrounding whitespace are allowed. Reads 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.