error_info_macros/lib.rs
1mod error_info;
2mod input;
3
4use proc_macro::TokenStream;
5use proc_macro_error::proc_macro_error;
6
7/// Derives the `ErrorInfo` trait for an enum.
8/// It's **highly recommended** to include a prefix on each variant, as the name is used to generate the code, which is
9/// ofter required to be unique.
10///
11/// Each variant must provide an status and a message, which can use variant's fields.
12///
13/// If `summary` feature is enabled, it requires the `linkme` crate to be available.
14///
15/// ## Examples
16/// ``` ignore
17/// #[derive(Debug, ErrorInfo)]
18/// #[allow(clippy::enum_variant_names)]
19/// pub enum CustomErrorCode {
20/// #[error(status = StatusCode::BAD_REQUEST, message = "Bad request: missing '{field}' field")]
21/// BadRequest { field: String },
22/// #[error(status = StatusCode::INTERNAL_SERVER_ERROR, message = "Internal server error")]
23/// InternalServerError,
24/// }
25/// ```
26#[proc_macro_error]
27#[proc_macro_derive(ErrorInfo, attributes(error))]
28pub fn error_info(input: TokenStream) -> TokenStream {
29 let input = syn::parse_macro_input!(input as syn::DeriveInput);
30 error_info::r#impl(input).into()
31}