Enum ErrorTree

Source
pub enum ErrorTree<L, E> {
    Leaf(E),
    Edge(L, Box<ErrorTree<L, E>>),
    Vec(Vec<ErrorTree<L, E>>),
}
Expand description

The error Tree structure.

  • L is the Label type.
  • E is the inner Error type. It can be an error enum (from the thiserror package).

Variants§

§

Leaf(E)

Stores your single error type.

§

Edge(L, Box<ErrorTree<L, E>>)

Adds a label to the subtree.

§

Vec(Vec<ErrorTree<L, E>>)

Groups multiple subtrees at the same level.

Implementations§

Source§

impl<L, E> ErrorTree<L, E>

Source

pub fn leaf(error: E) -> Self

Creates a Leaf tree from an error.

struct Error(String);
let error_tree = ErrorTree::<&'static str, _>::leaf(Error("error".into()));
Source§

impl<L, E> ErrorTree<L, E>
where L: Clone,

Source

pub fn flatten_tree(self) -> Vec<FlatError<L, E>>

Flattens the error tree in a Vec of FlatErrors.

#[derive(Debug)]
struct Error(String);

let error_1 = ErrorTree::leaf(Error("error1".into())).with_label("label1");
let error_2 = ErrorTree::leaf(Error("error2".into())).with_label("label2");

let errors = vec![error_1, error_2];

let tree: ErrorTree<&'static str, Error> = errors.into();
let tree = tree.with_label("parent_label");

let flat_errors = tree.flatten_tree();

assert!(
    matches!(
        &flat_errors[..],
        [
            FlatError {
                path: path1,
                error: Error(error1),
            },
            FlatError {
                path: path2,
                error: Error(error2),
            },
        ]
        if path1 == &vec!["label1", "parent_label"]
        && path2 == &vec!["label2", "parent_label"]
        && error1 == "error1"
        && error2 == "error2"
    ),
    "unexpected: {:#?}",
    flat_errors
);

Trait Implementations§

Source§

impl<L: Debug, E: Debug> Debug for ErrorTree<L, E>

Source§

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

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

impl<L, E> From<Vec<E>> for ErrorTree<L, E>
where E: IntoErrorTree<L, E>, ErrorTree<L, E>: From<E>,

Source§

fn from(errors: Vec<E>) -> Self

Converts to this type from the input type.
Source§

impl<L, E> From<Vec<ErrorTree<L, E>>> for ErrorTree<L, E>

Source§

fn from(subtrees: Vec<ErrorTree<L, E>>) -> Self

Converts to this type from the input type.
Source§

impl<L, E> IntoErrorTree<L, E> for ErrorTree<L, E>

Source§

fn with_label(self, label: L) -> ErrorTree<L, E>

Adds a label to an error tree. Read more

Auto Trait Implementations§

§

impl<L, E> Freeze for ErrorTree<L, E>
where E: Freeze, L: Freeze,

§

impl<L, E> RefUnwindSafe for ErrorTree<L, E>

§

impl<L, E> Send for ErrorTree<L, E>
where E: Send, L: Send,

§

impl<L, E> Sync for ErrorTree<L, E>
where E: Sync, L: Sync,

§

impl<L, E> Unpin for ErrorTree<L, E>
where E: Unpin, L: Unpin,

§

impl<L, E> UnwindSafe for ErrorTree<L, E>
where E: UnwindSafe, L: UnwindSafe,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

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

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.