Enum wood::Wood [−][src]
Variants
Branchv(Branch)
Leafv(Leaf)
Implementations
impl Wood
[src]
impl Wood
[src]pub fn leaf(v: String) -> Wood
[src]
pub fn branch(v: Vec<Wood>) -> Wood
[src]
pub fn is_leaf(&self) -> bool
[src]
pub fn is_branch(&self) -> bool
[src]
pub fn line(&self) -> isize
[src]
pub fn col(&self) -> isize
[src]
pub fn line_and_col(&self) -> (isize, isize)
[src]
pub fn initial_str(&self) -> &str
[src]
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))
pub fn to_string(&self) -> String
[src]
pub fn strip_comments_escape(
&mut self,
comment_str: &str,
comment_escape_str: &str
)
[src]
&mut self,
comment_str: &str,
comment_escape_str: &str
)
pub fn strip_comments(&mut self, comment_str: &str)
[src]
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.
pub fn contents(&self) -> Iter<'_, Self>
[src]
if Leaf, returns a slice iter containing just this, else Branch, iterates over branch contents
pub fn head(&self) -> Result<&Wood, Box<WoodError>>
[src]
returns the first wood, or if it’s a leaf, itself
pub fn second(&self) -> Result<&Wood, Box<WoodError>>
[src]
returns the second wood within this one, if it is a list wood, if there is a second wood
pub fn tail<'b>(&'b self) -> Iter<'b, Self>
[src]
if Leaf, returns an empty slice iter, if Branch, returns contents after the first element
pub fn seek<'a, 'b>(&'a self, key: &'b str) -> Option<&'a Wood>
[src]
self.contents().find(|el| el.initial_str() == key)
pub fn find<'a, 'b>(&'a self, key: &'b str) -> Result<&'a Wood, Box<WoodError>>
[src]
returns the first child term with initial_str == key, or if none is found, an error
Trait Implementations
impl StructuralEq for Wood
[src]
impl StructuralEq for Wood
[src]Auto Trait Implementations
impl RefUnwindSafe for Wood
impl RefUnwindSafe for Wood
impl UnwindSafe for Wood
impl UnwindSafe for Wood