shepmaster / snafu

Easily assign underlying errors into domain-specific errors while adding context
https://docs.rs/snafu/
Apache License 2.0
1.45k stars 61 forks source link

Add support for transforming from generic error types with lifetimes. #249

Closed XAMPPRocky closed 4 years ago

XAMPPRocky commented 4 years ago

Hello, I'm using snafu in my library which also uses nom which has a generic error type of nom::Err<E> which is commonly nom::<(&[u8], nom::error::ErrorKind)>. Currently, unless I'm missing something; it doesn't seem like you can convert from that error type to your own using the source(from()) attribute. Since it asks for you to define a lifetime which I can't do from the attribute, so right now I have to attach map_err to every nom call to map it into a owned type first. It would be nice if snafu could easily handle this kind of complex error type more easily.

Example


#[derive(Snafu)]
#[snafu(visibility = "pub(crate)")]
#[derive(Debug)]
pub enum Error {
    #[snafu(display("Error in Parser"))]
    Parser {
        #[snafu(source(from(nom::Err::<(&[u8], nom::error::ErrorKind)>, nom::Err::to_owned)))]
        source: nom::Err<(Vec<u8>, nom::error::ErrorKind)>,
        backtrace: snafu::Backtrace,
    },
}
shepmaster commented 4 years ago

Duplicate of #99?

XAMPPRocky commented 4 years ago

Seems so, closing

shepmaster commented 4 years ago

It is possible that this is unique, however. The duplicate is mostly about including the lifetime-parameterized error inside the new error, you mention that you want to transform it to an owned error.

XAMPPRocky commented 4 years ago

Well I just want to able to carry the nom error, and if not that transform for it into any error type I could carry instead. Right now, I'm basically converting it into a String so snafu never knows nom is even involved.