peel-off 0.1.0

Peel off a specific variant from a Rust enum, splitting it into the extracted variant and the residual
Documentation
  • Coverage
  • 40%
    2 out of 5 items documented1 out of 3 items with examples
  • Size
  • Source code size: 12.72 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 569.71 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • drmingdrmer/peel-off
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • drmingdrmer

peel-off

Peel off a specific variant from a Rust enum, splitting it into the extracted variant and the residual.

This is useful for error handling where you want to extract and propagate one specific error variant (e.g., a forwarding/retry error) while handling the rest locally.

Documentation

Usage

use peel_off::Peel;

// An error enum with a variant you want to peel off
enum ApiError {
    Timeout(u64),
    NotFound(String),
    Internal(String),
}

// The remaining variants after peeling
enum OtherError {
    NotFound(String),
    Internal(String),
}

impl Peel for ApiError {
    type Peeled = u64;       // the extracted variant's payload
    type Residual = OtherError; // everything else

    fn peel(self) -> Result<OtherError, u64> {
        match self {
            ApiError::Timeout(ms) => Err(ms),
            ApiError::NotFound(s) => Ok(OtherError::NotFound(s)),
            ApiError::Internal(s) => Ok(OtherError::Internal(s)),
        }
    }
}

Peeling a Result directly

The blanket impl on Result<T, E: Peel> lets you call .peel() without unwrapping the error first. Combined with ?, the peeled variant propagates automatically:

fn handle_request(res: Result<String, ApiError>) -> Result<String, u64> {
    // If res is Err(ApiError::Timeout(ms)), this returns Err(ms) via `?`.
    // Otherwise, inner is Result<String, OtherError>.
    let inner = res.peel()?;

    match inner {
        Ok(v) => Ok(v),
        Err(_other) => Ok("fallback".to_string()),
    }
}

License

MIT