| Crates.io | fnerror |
| lib.rs | fnerror |
| version | 0.1.4 |
| created_at | 2025-09-04 22:07:26.569586+00 |
| updated_at | 2025-09-22 21:07:39.689335+00 |
| description | A proc-macro for generating error impls for functions. |
| homepage | |
| repository | https://github.com/ZXY595/fnerror |
| max_upload_size | |
| id | 1824854 |
| size | 24,328 |
A Rust library for error handling when you want your functions to automatically generate errors.
Add the following to your Cargo.toml:
cargo add fnerror
You also need to add thiserror to your crate: (Will be unneeded in the future)
cargo add thiserror
#[fnerror]
fn foo() -> Result<(), MyError> /* or Result<T> to use the default ident of error type ,
which is pascal case of the function name + "Error", like `FooError` */
{
bar().map_err(|e| {
#[fnerr]
Error2("{}", e as String)
})?;
baz().map_err(|e| {
#[fnerr]
Error3("{}, {}", e as &'static str, 123 as u8)
})?;
Ok(())
}
fn bar() -> Result<(), String> {
Err("test2 error".to_string())
}
fn baz() -> Result<(), &'static str> {
Err("test2 error")
}
Which expands to (with thiserror feature):
fn foo() -> ::std::result::Result<(), MyError> {
bar().map_err(|e| MyError::Error2(e))?;
baz().map_err(|e| MyError::Error3(e, 123))?;
Ok(())
}
#[derive(Debug, ::thiserror::Error)]
pub enum MyError {
#[error("{}", 0usize)]
Error2(String),
#[error("{}, {}",0usize, 1usize)]
Error3(&'static str, u8),
}