Skip to main content

api_error_derive/
lib.rs

1// Copyright 2025-Present Centreon
2// SPDX-License-Identifier: Apache-2.0
3#![warn(clippy::pedantic)]
4#![allow(clippy::match_wildcard_for_single_variants)]
5#![allow(clippy::needless_pass_by_value)]
6
7use proc_macro2::TokenStream;
8use syn::LitStr;
9
10#[proc_macro_derive(ApiError, attributes(api_error))]
11pub fn derive_api_error(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
12    let input = syn::parse_macro_input!(input as syn::DeriveInput);
13    crate::expand::expand(input).into()
14}
15
16mod expand;
17mod parser;
18
19mod kw {
20    syn::custom_keyword!(inherit);
21    syn::custom_keyword!(transparent);
22    syn::custom_keyword!(status_code);
23    syn::custom_keyword!(message);
24}
25
26/// All possible parameters for the `api_error` attribute.
27enum VariantAttr {
28    /// Do we want to delegate to the inner field?
29    Transparent,
30
31    /// Do we want to inherit the message from the Display impl?
32    InheritMsg { status_code: Option<TokenStream> },
33
34    /// A custom message and status code.
35    Custom {
36        /// A format message, defaulting to the status code HTTP reason.
37        msg: Option<LitStr>,
38
39        /// The HTTP status code, defaulting to an `INTERNAL_SERVER_ERROR`.
40        status_code: Option<TokenStream>,
41    },
42}