anywrap/lib.rs
1//! # Anywrap
2//! Anywrap is an error handler designed for applications, similar to anyhow, but it supports matching on enum variants, making it more ergonomic.
3
4//! # Example
5//!
6//! ```rust
7//! use std::fmt;
8//! use std::fs::File;
9//! use anywrap::{anywrap, AnyWrap};
10//!
11//! pub struct ErrorCode(pub u32);
12//!
13//! impl fmt::Display for ErrorCode {
14//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15//! write!(f, "{}", self.0)
16//! }
17//! }
18//!
19//! #[derive(AnyWrap)]
20//! #[anywrap]
21//! pub enum Error {
22//! #[anywrap_attr(display = "Error Code: {code}", from = "code")]
23//! Code { code: ErrorCode },
24//! #[anywrap_attr(display = "{source}")]
25//! IO { source: std::io::Error },
26//! }
27//!
28//! pub type Result<T, E = Error> = std::result::Result<T, E>;
29//!
30//! pub fn define_error() -> Result<()> {
31//! let e = Error::from(ErrorCode(1));
32//! Err(e)
33//! }
34//!
35//! pub fn chain1() -> Result<()> {
36//! define_error().context("chain1")
37//! }
38//!
39//! pub fn with_chain() -> Result<()> {
40//! chain1().context("with_chain")
41//! }
42//!
43//! pub fn auto() -> Result<()> {
44//! let _ = File::open("test.txt")?;
45//!
46//! Ok(())
47//! }
48//!
49//! fn main() {
50//! if let Err(e) = auto() {
51//! println!("--12: {e:?}");
52//! }
53//! if let Err(e) = with_chain() {
54//! println!("--15 display: {e}");
55//! println!("--15 debug: {e:?}");
56//! }
57//! }
58//! ```
59
60pub mod location;
61pub use anywrap_macro::{anywrap, AnyWrap};