rust-uio icon indicating copy to clipboard operation
rust-uio copied to clipboard

Adding Error trait to UioError

Open SebastianHambura opened this issue 8 months ago • 0 comments

Hey, thanks for this crate :)

Would it be possible to add the Error trait to UioError, so that it can play nicely with Anyhow (and I suppose other error handling crates) ?

I've never done it before, but I looked how Deku was doing it ( https://docs.rs/deku/0.17.0/src/deku/error.rs.html#74 ) and adding this should work :

impl Error for UioError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(self)
    }
}

impl Display for UioError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match *self {
            UioError::Address => write!(f, "Invalid address "),
            UioError::Size => write!(f, "Invalid Size "),
            UioError::Io(ref err) => write!(f, "IO error: {err} "),
            UioError::Map(ref err) => write!(f, "Map error: {err} "),
            UioError::Parse => write!(f, "Parse error"),
        }
    }
} 

SebastianHambura avatar Jun 13 '24 11:06 SebastianHambura