macro_rules! define_errors {
(@emit [[$(#[$meta:meta])*] [$vis:vis] $name:ident]
$({
[$($vattr:tt)*]
[$($disp:tt)*]
[$kind:ident $(, $tag:ident = $val:expr)*]
$variant:ident $({ $($field:ident : $ftype:ty),* })?
})*
) => { ... };
(@display $f:ident [$display:literal $(, $param:ident)*] $caption:expr; $variant:ident $($field:ident)*) => { ... };
(@display $f:ident [] $caption:expr; $variant:ident $($field:ident)*) => { ... };
(@display $($rest:tt)*) => { ... };
(@fmt_field $f:ident, source, $field:ident) => { ... };
(@fmt_field $f:ident, $name:ident, $field:ident) => { ... };
(@check_tag caption) => { ... };
(@check_tag retryable) => { ... };
(@check_tag fatal) => { ... };
(@check_tag status) => { ... };
(@check_tag exit) => { ... };
(@check_tag $other:ident) => { ... };
(@munch $hdr:tt [$($done:tt)*] [] [] []) => { ... };
(@munch $hdr:tt $done:tt [$($attrs:tt)*] [] $kind:tt
#[error(display = $display:literal $(, $param:ident)* $(,)?)] $($rest:tt)*
) => { ... };
(@munch $hdr:tt $done:tt $attrs:tt $disp:tt $kind:tt #[error $($bad:tt)*] $($rest:tt)*) => { ... };
(@munch $hdr:tt $done:tt [$($attrs:tt)*] $disp:tt []
#[kind($kind:ident $(, $tag:ident = $val:expr)* $(,)?)] $($rest:tt)*
) => { ... };
(@munch $hdr:tt $done:tt $attrs:tt $disp:tt $kind:tt #[kind $($bad:tt)*] $($rest:tt)*) => { ... };
(@munch $hdr:tt $done:tt [$($attrs:tt)*] $disp:tt $kind:tt #[$($attr:tt)*] $($rest:tt)*) => { ... };
(@munch $hdr:tt [$($done:tt)*] [$($attrs:tt)*] [$($disp:tt)*] [$($kind:tt)+]
$variant:ident { $($field:ident : $ftype:ty),* $(,)? } $(, $($rest:tt)*)?
) => { ... };
(@munch $hdr:tt [$($done:tt)*] [$($attrs:tt)*] [$($disp:tt)*] [$($kind:tt)+]
$variant:ident $(, $($rest:tt)*)?
) => { ... };
(@munch $hdr:tt $done:tt $attrs:tt $disp:tt [] $variant:ident $($rest:tt)*) => { ... };
(@munch $hdr:tt $done:tt $attrs:tt $disp:tt $kind:tt) => { ... };
(@munch $hdr:tt $done:tt $attrs:tt $disp:tt $kind:tt $($rest:tt)+) => { ... };
(@find_source) => { ... };
(@find_source $field:ident $(, $rest:ident)*) => { ... };
(@find_source_match source, $source_field:ident $(, $rest:ident)*) => { ... };
(@find_source_match $field_name:ident, $field:ident $(, $rest:ident)*) => { ... };
(@get_caption $kind:ident) => { ... };
(@get_caption $kind:ident, caption = $caption:expr $(, $($rest:tt)*)?) => { ... };
(@get_caption $kind:ident, $tag:ident = $val:expr $(, $($rest:tt)*)?) => { ... };
(@get_tag $target:ident, $default:expr) => { ... };
(@get_tag retryable, $default:expr, retryable = $val:expr $(, $($rest:tt)*)?) => { ... };
(@get_tag fatal, $default:expr, fatal = $val:expr $(, $($rest:tt)*)?) => { ... };
(@get_tag status, $default:expr, status = $val:expr $(, $($rest:tt)*)?) => { ... };
(@get_tag exit, $default:expr, exit = $val:expr $(, $($rest:tt)*)?) => { ... };
(@get_tag $target:ident, $default:expr, $tag:ident = $val:expr $(, $($rest:tt)*)?) => { ... };
(@format_display $display:literal) => { ... };
(@format_display $display:literal, $($param:ident),+) => { ... };
(@format_display_field $field:ident) => { ... };
(@format_display_field $field:ident . $($rest:ident).+) => { ... };
(
$(
$(#[$meta:meta])* $vis:vis enum $name:ident {
$(
$(#[doc = $vdoc:expr])*
$(#[error(display = $display_a:literal $(, $param_a:ident)* $(,)?)])?
#[kind($kind:ident $(, $tag:ident = $val:expr)* $(,)?)]
$(#[error(display = $display_b:literal $(, $param_b:ident)* $(,)?)])?
$variant:ident $( { $($field:ident : $ftype:ty),* $(,)? } )?
),* $(,)?
}
)*
) => { ... };
(
$(
$(#[$meta:meta])* $vis:vis enum $name:ident { $($body:tt)* }
)*
) => { ... };
}Expand description
Declare an error enum with constructors, metadata methods,
Display and std::error::Error.
Each variant needs a #[kind(Name, tag = value, ...)] attribute and
may carry one #[error(display = "...", field, ...)] attribute. The
two may appear in either order, and doc comments or other attributes
(#[allow(...)], #[serde(...)], …) are passed through to the
variant. The trailing comma after the last variant is optional.
Recognised #[kind] tags are caption, retryable, fatal,
status and exit; any other tag is a compile error.
The macro generates, for each enum:
- the enum itself with
#[derive(Debug)]; - a constructor per variant named after the lowercased variant
(
Configbecomesconfig(...)), which fires the registered error hook; - inherent
kind,caption,is_retryable,is_fatal,status_codeandexit_codemethods (it does not implementForgeError; see thegroup!docs for the delegating impl); Display, using thedisplaystring when one is given, or"<caption>: <Variant> | field = value ..."otherwise. Only the default format needs the fields to implementDebug(andDisplayfor a field namedsource); a custom display string only needs what it formats;std::error::Error, whosesource()returns a field namedsourcethrough [ErrorSource].
The display string is a format! string. Fields listed after it are
passed as named arguments, and fields it names inline ({path:?})
are captured directly, so {{ and }} produce literal braces.
§Example
use error_forge::define_errors;
use std::path::PathBuf;
define_errors! {
pub enum StoreError {
/// The store file could not be read.
#[error(display = "cannot read {path:?}")]
#[kind(Io, retryable = true, status = 503)]
Read { path: PathBuf },
#[kind(Corrupt, fatal = true, exit = 3)]
Corrupt
}
}
let err = StoreError::read(PathBuf::from("db.bin"));
assert_eq!(err.to_string(), "cannot read \"db.bin\"");
assert!(err.is_retryable());
assert_eq!(StoreError::corrupt().exit_code(), 3);
assert_eq!(StoreError::corrupt().to_string(), "Corrupt: Corrupt");