1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//! # erra
//!
//! A lightweight, `no_std`-compatible library for adding call-site context to
//! `Result<T, E>` without erasing or boxing the underlying error type.
//!
//! ## Motivation
//!
//! Rust's `?` operator is great at propagating errors, but it does not preserve
//! much context about the path that led there. If a production service surfaces
//! something like `Os { code: 2, kind: NotFound }`, you know what failed, but
//! not which operation triggered it or what the code was trying to do at the
//! time.
//!
//! The usual ways of adding context all involve trade-offs:
//!
//! - `map_err` with `format!` is repetitive, allocates eagerly, and usually
//! collapses a structured error into a `String`.
//! - `anyhow` and `eyre` are a good fit for applications, but they erase the
//! concrete error type behind `dyn Error`, which makes them a weaker fit for
//! library APIs that want to preserve typed errors.
//! - `thiserror` works well for defining boundary error types, but it is too
//! heavy for routine call-site annotation inside a module or subsystem.
//!
//! `erra` sits in the middle: it lets you attach context where the error
//! happens while keeping the original error type available for matching and
//! propagation. On the `Ok` path, it adds no allocation overhead.
//!
//! ## Quick start
//!
//! Bring [`ResultExt`] into scope to annotate any `Result<T, E>`:
//!
//! ```rust
//! use erra::ResultExt;
//! use std::fs;
//!
//! fn load_config(path: &str) -> core::result::Result<String, erra::Error<std::io::Error>> {
//! fs::read_to_string(path)
//! .annotate("failed to read application config layout")
//! }
//! ```
//!
//! If you prefer shorter signatures, use the crate's [`Result`] alias:
//!
//! ```rust
//! use erra::{Result, ResultExt};
//! use std::fs;
//!
//! fn load_config(path: &str) -> Result<String, std::io::Error> {
//! fs::read_to_string(path)
//! .annotate("failed to read application config layout")
//! }
//! ```
//!
//! For dynamic messages, use [`ResultExt::annotate_with`]. The closure runs
//! only on the `Err` path:
//!
//! ```rust
//! # #[cfg(feature = "alloc")] {
//! use erra::{Result, ResultExt};
//!
//! fn fetch_data(id: u64) -> Result<Vec<u8>, std::io::Error> {
//! std::fs::read(format!("/data/{id}"))
//! .annotate_with(|| format!("failed to pull record for id={id}"))
//! }
//! # }
//! ```
//!
//! ## Inspecting errors
//!
//! `erra::Error<E>` preserves the concrete type `E`, so callers can inspect the
//! wrapped error directly without downcasting:
//!
//! ```rust
//! use erra::{Result, ResultExt};
//! use std::io;
//!
//! fn run() -> Result<(), io::Error> {
//! std::fs::read_to_string("missing.toml").annotate("reading setup file")?;
//! Ok(())
//! }
//!
//! if let Err(err) = run() {
//! match err.source.kind() {
//! io::ErrorKind::NotFound => println!("file was missing"),
//! _ => eprintln!("system error: {err}"),
//! }
//! }
//! ```
//!
//! ## Nested context
//!
//! Annotations compose naturally. Multiple layers of `.annotate()` produce
//! nested wrappers such as `Error<Error<E>>`.
//!
//! With `std` enabled, [`std::error::Error::source`] walks that chain in the
//! usual way, so generic reporters and logging infrastructure can traverse it
//! without any special integration.
//!
//! ## Design notes
//!
//! ### No `From<E>`
//!
//! `erra` deliberately does not implement `From<E> for Error<E>`. If it did,
//! the `?` operator could wrap errors implicitly without adding any message,
//! which would defeat the purpose of explicit call-site annotation.
//!
//! ### `Display` and `source()`
//!
//! `Display` is meant for people: it formats the error as a readable outer-to-
//! inner message chain, such as `"outer context: inner context: underlying error"`.
//!
//! `source()` is meant for tools: it exposes the wrapped error through the
//! standard error-chain interface.
//!
//! ## Feature flags
//!
//! - `std` (default): implements `std::error::Error` for `Error<E>`. Implies
//! `alloc`.
//! - `alloc`: enables owned context strings and [`ResultExt::annotate_with`].
//!
//! With `default-features = false`, `erra` still works in `no_std` builds using
//! static string context only.
extern crate alloc;
pub use Error;
pub use ResultExt;
/// Shorthand for `core::result::Result<T, erra::Error<E>>`.
///
/// This alias keeps function signatures shorter when returning annotated
/// errors.
///
/// ```rust
/// use erra::{Result, ResultExt};
///
/// fn process() -> Result<i32, std::io::Error> {
/// std::fs::read_to_string("id.txt")
/// .annotate("failed to read id source")?;
/// Ok(42)
/// }
/// ```
///
/// This alias is not part of the prelude. Import it explicitly when you want
/// it.
pub type Result<T, E> = Result;
/// The `erra` prelude.
///
/// This module re-exports [`ResultExt`] for convenient glob imports:
///
/// ```rust
/// use erra::prelude::*;
/// ```