Skip to main content

Node

Struct Node 

Source
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

Source

pub fn list(items: impl IntoIterator<Item = Node>) -> Self

A list of nodes.

Source

pub fn atom(value: impl Into<String>) -> Self

An unquoted atom (GUID, literal, number).

Source

pub fn string(value: impl Into<String>) -> Self

A quoted string.

There is no From<&str> for building: Node::try_from(&str) parses text.

Source

pub fn counted(items: impl IntoIterator<Item = Node>) -> Self

A counted list {count,item1,item2,...}, the common 1C shape.

Source

pub fn value(&self) -> &Value

Source

pub fn into_value(self) -> Value

Source

pub fn value_mut(&mut self) -> &mut Value

Mutable access to the content; the position stays where the node was parsed.

Source

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.

Source

pub fn is_list(&self) -> bool

Source

pub fn is_str(&self) -> bool

Source

pub fn is_atom(&self) -> bool

Source

pub fn is_empty_atom(&self) -> bool

An empty atom: no value between commas ({1,,2}).

Source

pub fn as_list(&self) -> Option<&[Node]>

Source

pub fn as_str(&self) -> Option<&str>

Value of a string or an atom.

Source

pub fn as_quoted(&self) -> Option<&str>

Quoted string only.

Source

pub fn as_atom(&self) -> Option<&str>

Atom only.

Source

pub fn as_parsed<T: FromStr>(&self) -> Option<T>

Atom parsed into any FromStr type: node.as_parsed::<u32>().

Source

pub fn as_i64(&self) -> Option<i64>

Source

pub fn as_u64(&self) -> Option<u64>

Source

pub fn as_f64(&self) -> Option<f64>

Source

pub fn as_bool(&self) -> Option<bool>

Atom 1 is true, 0 is false.

Source

pub fn as_guid(&self) -> Option<&str>

An atom shaped like a GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.

Source

pub fn as_base64(&self) -> Option<String>

Payload of a #base64:… atom without the prefix and line breaks (not decoded).

Source

pub fn get(&self, idx: usize) -> Option<&Node>

List item by index.

Source

pub fn get_path(&self, path: &[usize]) -> Option<&Node>

Item by a path of indices from this node: node.get_path(&[1, 0, 2]).

Source

pub fn len(&self) -> usize

Number of list items; 0 for strings and atoms.

Source

pub fn is_empty(&self) -> bool

List without items; strings and atoms are considered empty too.

Source

pub fn iter(&self) -> Iter<'_, Node> ⓘ

List items; an empty iterator for strings and atoms.

Source

pub fn into_list(self) -> Option<Vec<Node>>

Source

pub fn into_string(self) -> Option<String>

Value of a string or an atom.

Source

pub fn try_list(&self) -> Result<&[Node], AccessError>

Source

pub fn try_str(&self) -> Result<&str, AccessError>

Source

pub fn try_quoted(&self) -> Result<&str, AccessError>

Source

pub fn try_atom(&self) -> Result<&str, AccessError>

Source

pub fn try_parse<T>(&self) -> Result<T, AccessError>
where T: FromStr, T::Err: Display,

Atom parsed into any FromStr type: node.try_parse::<u32>()?.

The error names the target type and keeps the reason from FromStr::Err.

Source

pub fn try_i64(&self) -> Result<i64, AccessError>

Source

pub fn try_u64(&self) -> Result<u64, AccessError>

Source

pub fn try_f64(&self) -> Result<f64, AccessError>

Source

pub fn try_bool(&self) -> Result<bool, AccessError>

Source

pub fn try_guid(&self) -> Result<&str, AccessError>

Source

pub fn try_get(&self, idx: usize) -> Result<&Node, AccessError>

Source

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.

Source

pub fn as_list_mut(&mut self) -> Option<&mut Vec<Node>>

List items for editing: replace, push, insert or remove nodes.

Source

pub fn get_mut(&mut self, idx: usize) -> Option<&mut Node>

Source

pub fn get_path_mut(&mut self, path: &[usize]) -> Option<&mut Node>

Source

pub fn iter_mut(&mut self) -> IterMut<'_, Node> ⓘ

List items; an empty iterator for strings and atoms.

Source

pub fn try_list_mut(&mut self) -> Result<&mut Vec<Node>, AccessError>

Source

pub fn try_get_mut(&mut self, idx: usize) -> Result<&mut Node, AccessError>

Source

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

Source

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

pub fn walk<'a>(&'a self, visit: impl FnMut(&[usize], &'a Node))

Pre-order depth-first traversal with the path from the root. Not recursive: forms can be deeply nested.

Source

pub fn describe(&self) -> String

Short description of the node for error messages.

Source§

impl Node

Source

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.

Source§

impl Node

Source

pub fn write_to<W: Write>(&self, writer: W) -> Result<()>

Writes the bracket file text to any io::Write.

Trait Implementations§

Source§

impl Clone for Node

Source§

fn clone(&self) -> Self

Not recursive: a deep tree does not overflow the stack.

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Node

Source§

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

Shows only the value: positions would drown the tree in noise.

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.

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Node

Source§

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

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

impl Drop for Node

Source§

fn drop(&mut self)

Not recursive: nested lists are moved onto a heap stack and dropped one by one.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for Node

Source§

impl From<&Node> for String

Source§

fn from(node: &Node) -> Self

Converts to this type from the input type.
Source§

impl From<&Node> for Vec<u8>

Source§

fn from(node: &Node) -> Self

Converts to this type from the input type.
Source§

impl From<Value> for Node

Source§

fn from(value: Value) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Node>> for Node

Source§

fn from(items: Vec<Node>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Node

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Node

Source§

fn from(n: i8) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Node

Source§

fn from(n: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Node

Source§

fn from(n: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Node

Source§

fn from(n: i64) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for Node

Source§

fn from(n: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Node

Source§

fn from(n: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Node

Source§

fn from(n: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Node

Source§

fn from(n: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Node

Source§

fn from(n: u64) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Node

Source§

fn from(n: usize) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<Node>> FromIterator<T> for Node

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl FromStr for Node

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

fn from_str(text: &str) -> Result<Node, ParseError>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Node

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Ignores positions. Not recursive: pre-order with list lengths.

1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<usize> for Node

Source§

type Output = Node

The returned type after indexing.
Source§

fn index(&self, idx: usize) -> &Node

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for Node

Source§

fn index_mut(&mut self, idx: usize) -> &mut Node

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a> IntoIterator for &'a Node

Source§

type Item = &'a Node

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, Node>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut Node

Source§

type Item = &'a mut Node

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, Node>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Node

Source§

fn eq(&self, other: &Self) -> bool

Ignores positions. Not recursive.

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Node

Available on crate feature serde only.

Positions are not written: they belong to the parsed text.

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<&Path> for Node

Source§

type Error = ParseError

The type returned in the event of a conversion error.
Source§

fn try_from(path: &Path) -> Result<Node, ParseError>

Performs the conversion.
Source§

impl TryFrom<&[u8]> for Node

Source§

type Error = ParseError

The type returned in the event of a conversion error.
Source§

fn try_from(bytes: &[u8]) -> Result<Node, ParseError>

Performs the conversion.
Source§

impl TryFrom<&str> for Node

Source§

type Error = ParseError

The type returned in the event of a conversion error.
Source§

fn try_from(text: &str) -> Result<Node, ParseError>

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for Node

§

impl RefUnwindSafe for Node

§

impl Send for Node

§

impl Sync for Node

§

impl Unpin for Node

§

impl UnsafeUnpin for Node

§

impl UnwindSafe for Node

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.