#error #error-derive #macro #utility #macro-derive

macro error-utils-derive

Some rust proc-macros to simplify common error handling patterns

4 releases

0.1.3 Jul 25, 2022
0.1.2 Jul 24, 2022
0.1.1 Jul 24, 2022
0.1.0 Jul 24, 2022

#16 in #error-derive


Used in 2 crates

MIT license

15KB
208 lines

pipeline status Latest Release

rust-error-utils

A collection of some rust macros to simplify common error handling patterns.

  1. Usage
    1. handle_err Macro
    2. fail Macro
    3. Errors Derive Macro
  2. License

Usage

Add to Cargo.toml:

[dependencies.error-utils]
git = "https://gitlab.com/robert-oleynik/rust-error-utils.git"

handle_err Macro

use 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 Macro

Shorthand for std::process::exit

fail!();
// or
fail!(10);

See Documentation

Errors Derive Macro

This 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

License

This project is license under the MIT license. See LICENSE

Dependencies

~1.5MB
~33K SLoC