| Crates.io | nok |
| lib.rs | nok |
| version | 0.0.1 |
| created_at | 2025-12-06 16:53:51.502504+00 |
| updated_at | 2025-12-06 16:53:51.502504+00 |
| description | Concrete Error type built around a pseudo stack trace |
| homepage | |
| repository | https://github.com/seavaux/nok |
| max_upload_size | |
| id | 1970419 |
| size | 46,574 |
Lightweight error source determination using pseudo stack traces.
The primary component is the Origin instance, which is determined at compile
time and always present.
pub struct Origin {
pub file: &'static str,
pub line: u32,
pub column: u32,
}
... optionally includes:
Payload)std::backtrace::Backtrace::force_capture().to_string())pub struct Error<Payload> {
pub origin: Origin,
pub message: Option<String>,
pub payload: Option<Payload>,
pub precursor: Option<Box<Error<Payload>>>,
pub backtrace: Option<String>,
}
Utilizing the optional precursor a single linked is formed, representing the pseudo stack trace.
pub struct ErrorStack<Payload> {
pub head: Box<Error<Payload>>,
}
The ErrorStack<Payload> type forms the E part in Result<T, E>.
type Res<T> = Result<T, ErrorStack<Payload>>;
std::backtrace::Backtrace is expensive and in certain environments like
WASM not helpfuluse nok::{ErrorStack, rethrow_err};
type Payload = u32;
// to add `#[macro_export]` to each macro use `generate_macros!(payload_type: Payload, export_globally: true)`
nok::generate_macros!(payload_type: Payload);
type Res<T> = Result<T, ErrorStack<Payload>>;
fn lookup_even_number(key: &str, lut: &std::collections::HashMap<i32, u32>) -> Res<u32> {
let num = rethrow_err!(lookup_bounded_number(key, lut))?;
if num % 2 == 0 {
Ok(num)
} else {
err!("number was not even")
}
}
fn lookup_bounded_number(key: &str, lut: &std::collections::HashMap<i32, u32>) -> Res<u32> {
let key: i32 = map_err!(key.parse())?;
verify!(0 <= key && key <= 100, "key has to be in the range [0; 100] but was {key}")?;
let val = ok_or!(lut.get(&key), "there is no entry for the key {key}")?;
Ok(*val)
}