error-trees 0.2.0

Fail with multiple errors, istead of only the first.
Documentation
  • Coverage
  • 88.89%
    16 out of 18 items documented5 out of 12 items with examples
  • Size
  • Source code size: 15.81 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.18 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 15s Average build duration of successful builds.
  • all releases: 15s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • wuerges/error-trees
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • wuerges

Error Trees

Fail in a spectacular manner with multiple errors, instead only a single one!

// The error type
#[derive(Debug)]
struct Error(String);

impl<L> From<Error> for ErrorTree<L, Error> {
    fn from(e: Error) -> Self {
        Self::leaf(e)
    }
}

// A function that returns an error
fn faulty_function() -> Result<(), Error> {
    Err(Error("error".into()))
}

// A function that returns more than one error
fn parent_function() -> Result<Vec<()>, ErrorTree<&'static str, Error>> {
    let result1 = faulty_function().label_error("first faulty");
    let result2 = faulty_function().label_error("second faulty");

    let result: Result<_, ErrorTree<_, _>> = vec![result1, result2]
        .into_iter()
        .partition_result::<Vec<_>, Vec<_>, _, _>()
        .into_result();
    result.label_error("parent function")
}

// your main function
#[test]
fn main_function() {
    let result = parent_function();

    let flat_results = result.flatten_results();
    let flat_errors: Vec<FlatError<&str, Error>> = flat_results.unwrap_err();

    assert!(
        matches!(
            &flat_errors[..],
            [
                FlatError {
                    path: path1,
                    error: Error(_),
                },
                FlatError {
                    path: path2,
                    error: Error(_),
                },
            ]
            if path1 == &vec!["first faulty", "parent function"]
            && path2 == &vec!["second faulty", "parent function"]
        ),
        "unexpected: {:#?}",
        flat_errors
    );
}