Crates.io | error-utils-derive |
lib.rs | error-utils-derive |
version | 0.1.3 |
source | src |
created_at | 2022-07-24 19:14:22.515134 |
updated_at | 2022-07-25 10:52:17.606047 |
description | Some rust proc-macros to simplify common error handling patterns |
homepage | |
repository | https://gitlab.com/robert-oleynik/rust-error-utils |
max_upload_size | |
id | 632152 |
size | 17,010 |
A collection of some rust macros to simplify common error handling patterns.
Add to Cargo.toml
:
[dependencies.error-utils]
git = "https://gitlab.com/robert-oleynik/rust-error-utils.git"
handle_err
Macrouse error_utils::handle_err;
use std::path::Path;
fn read_file<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
let content = handle_err!(std::fs::read_string(path), err => {
eprintln!("{}", err);
return Err(err);
})
// ...
}
See Documentation
fail
MacroShorthand for std::process::exit
fail!();
// or
fail!(10);
See Documentation
Errors
Derive MacroThis Macro requires the
derive
feature. (Enabled per default)
Note: This example uses the toml
crate.
use std::path::Path;
use error_utils::Errors;
#[derive(Debug, Errors)]
enum ConfigError {
#[error("Failed to read config file (Reason: {})", from)]
Io(std::io::Error),
#[error("Failed to parse config file (Reason: {})", from)]
Toml(toml::de::Error)
}
fn read_config<P: AsRef<Path>>(path: P) -> Result<toml::Value, ConfigError> {
let content = std::fs::read_to_string(path)?;
Ok(toml::from_str(&content)?)
}
See Documentation
This project is license under the MIT
license. See
LICENSE