wrapping_error 0.1.0

An anti-boilerplate package for errors that wrap errors.
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 7.91 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 241.54 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 8s Average build duration of successful builds.
  • all releases: 8s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • valentinegb/wrapping_error
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • valentinegb

wrapping_error

An anti-boilerplate package for errors that wrap errors.

This package only exports one item: the wrapping_error macro. Using the wrapping_error macro, you can write this:

use std::{env::VarError, io};

use wrapping_error::wrapping_error;

wrapping_error!(pub(crate) AppDataError {
    Var(VarError) => "could not get $HOME environment variable",
    Io(io::Error) => "failed to read/write app data",
    Postcard(postcard::Error) => "failed to serialize/deserialize app data",
});

and get this:

use std::{fmt, error, env::VarError, io};

#[derive(Debug)]
pub(crate) enum AppDataError {
    Var(VarError),
    Io(io::Error),
    Postcard(postcard::Error),
}

impl fmt::Display for AppDataError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Self::Var(..) => write!(f, "could not get $HOME environment variable"),
            Self::Io(..) => write!(f, "failed to read/write app data"),
            Self::Postcard(..) => write!(f, "failed to serialize/deserialize app data"),
            ref err => err.fmt(f),
        }
    }
}

impl error::Error for AppDataError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            ref err => Some(err),
        }
    }
}

impl From<VarError> for AppDataError {
    fn from(err: VarError) -> Self {
        Self::Var(err)
    }
}

impl From<io::Error> for AppDataError {
    fn from(err: io::Error) -> Self {
        Self::Io(err)
    }
}

impl From<postcard::Error> for AppDataError {
    fn from(err: postcard::Error) -> Self {
        Self::Postcard(err)
    }
}