#[non_exhaustive]pub enum Error<E = Infallible> {
CloseError,
BuildError,
CloseAtError,
Overflow,
TokenOrder,
MissingNode(usize),
MissingCheckpoint(usize),
Flavor(E),
}Expand description
Errors raised while building a tree.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
CloseError
Error raised by Builder::close if there currently is no node being built.
§Examples
use syntree::{Builder, Error};
let mut tree = Builder::new();
tree.open("root")?;
tree.close()?;
// Syntax::Root and Syntax::Child is left open.
assert_eq!(tree.close(), Err(Error::CloseError));BuildError
Error raised by Builder::build if the tree isn’t correctly balanced.
§Examples
use syntree::{Builder, Error};
let mut tree = Builder::new();
tree.open("number")?;
tree.token("lit", 3)?;
tree.close()?;
tree.open("number")?;
// Syntax::Number is left open.
assert_eq!(tree.build(), Err(Error::BuildError));CloseAtError
Error raised by Builder::close_at if we’re not trying to close at a sibling node.
§Examples
use syntree::{Builder, Error};
let mut tree = Builder::new();
let c = tree.checkpoint()?;
tree.open("child")?;
tree.token("token", 3)?;
assert_eq!(tree.close_at(c, "operation"), Err(Error::CloseAtError));Overflow
Numerical overflow.
This happens when the tree has more nodes than the flavor’s
Width can address, or when a span would exceed
its Index. A narrow width such as u8 makes
this easy to reach.
§Examples
This is an example where we’re trying to build a really small tree using u8 pointers:
use syntree::{Builder, Error};
syntree::flavor! {
struct CustomFlavor {
type Index = u32;
type Width = u8;
}
}
let mut tree: Builder<_, CustomFlavor> = Builder::new_with();
for d in 0..u8::MAX - 1 {
tree.token(d, 1)?;
}
assert_eq!(tree.token(255, 1), Err(Error::Overflow));TokenOrder
A token’s span starts before the end of the token before it, or ends before it starts. Tokens are stored in source order, so they can’t overlap, but an explicit span may leave a gap after the previous token.
§Examples
use syntree::{Builder, Error, Span};
let mut tree = Builder::new();
tree.token("a", 3)?;
assert_eq!(tree.token_with("b", Span::new(2, 4)), Err(Error::TokenOrder));
assert_eq!(tree.token_with("b", Span::new(5, 4)), Err(Error::TokenOrder));
tree.token_with("b", Span::new(5, 6))?;MissingNode(usize)
The node of the given id is missing.
This happens when the flavor’s storage does not keep what is pushed into
it, such as EmptyVec.
MissingCheckpoint(usize)
Error raised by Builder::close_at if the checkpoint of the given index was not handed out by this builder.
§Examples
use syntree::{Builder, Error};
let mut a = Builder::new();
let mut b = Builder::new();
b.token("lit", 1)?;
let c = b.checkpoint()?;
assert_eq!(a.close_at(c, "operation"), Err(Error::MissingCheckpoint(0)));Flavor(E)
An error raised by the particular Flavor in use.
Trait Implementations§
impl<E: Copy> Copy for Error<E>
impl<E: Eq> Eq for Error<E>
Source§impl<E> Error for Error<E>where
E: 'static + Error,
impl<E> Error for Error<E>where
E: 'static + Error,
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()