one_err

OneErr to rule them all.

There are some great error helper crates out there. My favorites are thiserror and anyhow.

But sometimes you just need something different. The thiserror crate can over time lead to giant trees of nested error enums, while anyhow is difficult to match on.

Sometimes you need to interoperate with std::io::Error, but that type is awkward to construct, not Clone, and cannot be serialized.

OneErr is a newtype over std::io::Error, but makes it clonable, serializable, and hopefully more ergonomic.

std::io::ErrorKind Matching

```rust use one_err::*;

for res in [ Ok("not-error"), Err(OneErr::from(std::io::ErrorKind::InvalidInput)), Err(OneErr::from(std::io::ErrorKind::ConnectionRefused)), ] { match res.maperr(|e| e.kind()) { Ok(ok) => asserteq!("not-error", ok), Err(std::io::ErrorKind::InvalidInput) => (), Err(std::io::ErrorKind::ConnectionRefused) => (), oth => panic!("unexpected: {:?}", oth), } } ```

ErrNo Matching

```rust use one_err::*;

for res in [ Ok("not-error"), Err(OneErr::from(ErrNo::NoData)), Err(OneErr::from(ErrNo::Proto)), ] { match res.maperr(|e| e.errno()) { Ok(ok) => asserteq!("not-error", ok), Err(ErrNo::NoData) => (), Err(ErrNo::Proto) => (), oth => panic!("unexpected: {:?}", oth), } } ```

Custom Matching

```rust use one_err::*;

const ERRFOO: &str = "FOO"; const ERRBAR: &str = "BAR";

for res in [ Ok("not-error"), Err(OneErr::new(ERRFOO, "foo test")), Err(OneErr::new(ERRBAR, "bar test")), ] { match res.asref().maperr(|e| (e.strkind(), e)) { Ok(ok) => asserteq!("not-error", *ok), Err((ERRFOO, e)) => asserteq!("foo test", e.getmessage().unwrap()), Err((ERRBAR, e)) => asserteq!("bar test", e.getmessage().unwrap()), oth => panic!("unexpected: {:?}", oth), } } ```

std::io Interoperability

```rust use one_err::*; use std::io::Read;

const CUSTOM_ERR: &str = "CustomError";

/// My custom Read that always errors. pub struct ErrReader;

impl Read for ErrReader { fn read(&mut self, buf: &mut [u8]) -> std::io::Result { Err(OneErr::new(CUSTOMERR, "foo").into()) } }

asserteq!( r#"{"error":"CustomError","message":"foo"}"#, &ErrReader.read(&mut []).unwraperr().to_string(), ); ```

Serialization and Parsing

```rust use one_err::*;

const CUSTOM_ERR: &str = "CustomError";

let err = OneErr::new(CUSTOMERR, "bar"); let enc = err.tostring();

assert_eq!( r#"{"error":"CustomError","message":"bar"}"#, &enc, );

let dec: OneErr = enc.parse().unwrap(); assert_eq!(err, dec); ```

License: Apache-2.0