#error #hierarchy #macro #reuse #shorthand #proc-macro

macro error-tree

This crate let’s us use the error_tree! proc macro for ergonomic error hierarchy definition

4 releases

0.3.7 Oct 5, 2024
0.3.6 Jun 21, 2024
0.3.5 Jun 21, 2024
0.1.0 May 5, 2024

#16 in #shorthand

Download history 15/week @ 2024-06-28 38/week @ 2024-07-05 32/week @ 2024-07-26 3/week @ 2024-08-02 3/week @ 2024-08-09 2/week @ 2024-08-16 5/week @ 2024-08-23 8/week @ 2024-08-30 3/week @ 2024-09-06 25/week @ 2024-09-13 32/week @ 2024-09-20 27/week @ 2024-09-27 193/week @ 2024-10-04 19/week @ 2024-10-11

272 downloads per month
Used in 3 crates

MIT license

48KB
914 lines

Error Tree

Overview

error_tree is a Rust procedural macro crate designed to simplify error handling in Rust applications. It allows you to define complex error hierarchies in a clean and structured way, making your error handling logic both more robust and easier to maintain.

Features

  • Define nested error enums with ease.
  • Automatically generate From implementations for error conversions.
  • Simplify complex error handling in Rust.

Installation

Add error_tree to your Cargo.toml file under [dependencies]:

[dependencies]
error_tree = "0.1.0"

Usage

Here's a basic example of how to use error_tree:

use error_tree::error_tree;

error_tree!{

    // Enumerate possible errors for capturing audio.
    pub enum PassiveAudioCaptureError {
        FormatError,
        DeviceError(DeviceError),
        IOError(IOError),
        WavError(WavError),
        HostError(HostError),
        StreamOrChannelError(StreamOrChannelError),
    }

    pub enum MonitorAllInputsError { 
        DeviceError(DeviceError),
    }

    pub enum StreamOrChannelError { 
        StreamError(StreamError),
        ChannelError(ChannelError),
    }

    pub enum IOError { 
        Basic(std::io::Error),
    }

    pub enum ChannelError { 
        ChannelRecvError(mpsc::RecvError),
    }

    pub enum StreamError { 
        StreamError(cpal::StreamError),
        PauseStreamError(cpal::PauseStreamError),
        BuildStreamError(cpal::BuildStreamError),
        PlayStreamError(cpal::PlayStreamError),
        SupportedStreamConfigsError(cpal::SupportedStreamConfigsError),
        DefaultStreamConfigError(cpal::DefaultStreamConfigError),
    }

    pub enum DeviceError { 
        DeviceNotAvailable {
            device_name: String,
        },

        Basic(cpal::DevicesError),
        NameError(cpal::DeviceNameError),
    }

    pub enum WavError { 
        Hound(hound::Error),
    }

    pub enum HostError { 
        HostUnavailable(cpal::HostUnavailable),
    }
}

This will automatically generate the necessary From implementations for error conversions within your defined hierarchy.

Contributing

Contributions to error_tree are welcome! Please read our CONTRIBUTING.md for guidelines on how to contribute.

License

This crate is distributed under the terms of MIT License.

Dependencies

~4–6MB
~102K SLoC