Enum wood::Wood

source ·
pub enum Wood {
    Branchv(Branch),
    Leafv(Leaf),
}

Variants§

§

Branchv(Branch)

§

Leafv(Leaf)

Implementations§

source§

impl Wood

source

pub fn leaf(v: String) -> Wood

source

pub fn branch(v: Vec<Wood>) -> Wood

source

pub fn empty() -> Wood

source

pub fn is_leaf(&self) -> bool

source

pub fn is_branch(&self) -> bool

source

pub fn what(&self) -> LB<'_>

source

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

source

pub fn get_branch(&self) -> Option<&[Wood]>

source

pub fn line_and_col(&self) -> (isize, isize)

source

pub fn line(&self) -> isize

source

pub fn col(&self) -> isize

source

pub fn initial_str(&self) -> &str

Seeks the earliest string in the tree by looking at the first element of each branch recursively until it hits a leaf. (If it runs into an empty list, returns the empty string. I recommend this whenever you want to use the first string as a discriminator, or whenever you want to get leaf str contents in general. This abstracts over similar structures in a way that I consider generally desirable. I would go as far as to say that the more obvious, less abstract way of getting initial string should be Considered Harmful. A few motivating examples: If you wanted to add a feature to a programming language that allows you to add a special tag to an invocation, you want to put the tag inside the invocation’s ast node but you don’t want it to be confused for a parameter, this pattern enables: ((f tag(special_invoke_inline)) a b) If the syntax of a sexp language had more structure to it than usual: ((if condition) then…) would still get easily picked up as an ‘if’ node. Annotations are a good example, more generally, if you’re refactoring and you decide you want to add an extra field to what was previously a leaf, this pattern enables you to make that change, confident that your code will still read its string content in the same way (list key:value “some prose”) -> (list key:value (“some prose” modifier:italicise))

source

pub fn to_string(&self) -> String

source

pub fn strip_comments_escape( &mut self, comment_str: &str, comment_escape_str: &str )

source

pub fn strip_comments(&mut self, comment_str: &str)

Strips any branches with first element of comment_str. If you need to produce a leaf that is equivalent to comment_str. If you need the wood to contain a leaf that is the comment_str, you can escape it with a backslash. This is actually a highly flawed way of providing commenting, because this will also strip out any serialization of a list of strings where the first element happens to equal the comment_str. That’s a really subtle error, that violates a lot of expectations. You could get around it by escaping your wood so that any strs that resemble comment tags wont read that way, but it’s a bit awkward and sometimes wont really work.

source

pub fn contents(&self) -> Iter<'_, Self>

if Leaf, returns a slice iter containing just this, else Branch, iterates over branch contents

source

pub fn head(&self) -> Result<&Wood, Box<WoodError>>

returns the first wood, or if it’s a leaf, itself

source

pub fn second(&self) -> Result<&Wood, Box<WoodError>>

returns the second wood within this one, if it is a list wood, if there is a second wood

source

pub fn tail<'b>(&'b self) -> Iter<'b, Self>

if Leaf, returns an empty slice iter, if Branch, returns contents after the first element

source

pub fn flatter_tail<'a>(&'a self) -> Chain<Iter<'a, Self>, Iter<'a, Self>>

returns the tail of the first element of the wood chained with the rest of the wood. For dealing with a common situation with the termpose syntax where you want to ignore the structure of initial line items

call a b car
  dog
  entropy
  foreign_adversary

in this case, flatter_tail would give you [a, b, car, dog, entropy, foreign_adversary]. (by contrast, tail would only give you [dog, entropy, foreign_adversary])

source

pub fn seek<'a, 'b>(&'a self, key: &'b str) -> Option<&'a Wood>

self.contents().find(|el| el.initial_str() == key)

source

pub fn seek_val<'a, 'b>(&'a self, key: &'b str) -> Option<&'a Wood>

source

pub fn find<'a, 'b>(&'a self, key: &'b str) -> Result<&'a Wood, Box<WoodError>>

returns the first child term with initial_str == key, or if none is found, an error

source

pub fn find_val<'a, 'b>( &'a self, key: &'b str ) -> Result<&'a Wood, Box<WoodError>>

find(self, key).and_then(|v| v.second())

Trait Implementations§

source§

impl Clone for Wood

source§

fn clone(&self) -> Wood

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Wood

source§

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

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

impl<'a> From<&'a str> for Wood

source§

fn from(v: &'a str) -> Wood

Converts to this type from the input type.
source§

impl From<String> for Wood

source§

fn from(v: String) -> Wood

Converts to this type from the input type.
source§

impl From<Vec<Wood>> for Wood

source§

fn from(v: Vec<Wood>) -> Wood

Converts to this type from the input type.
source§

impl PartialEq for Wood

Line numbers aren’t checked in equality comparisons

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Wood

Auto Trait Implementations§

§

impl Freeze for Wood

§

impl RefUnwindSafe for Wood

§

impl Send for Wood

§

impl Sync for Wood

§

impl Unpin for Wood

§

impl UnwindSafe for Wood

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

§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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.