explicit_error_derive/
lib.rs1extern crate proc_macro;
2extern crate proc_macro2;
3extern crate quote;
4extern crate syn;
5
6#[cfg(feature = "actix-web")]
7mod actix;
8#[cfg(any(feature = "exit", feature = "actix-web"))]
9mod domain;
10
11#[cfg(any(feature = "http", feature = "exit", feature = "actix-web"))]
12use proc_macro::TokenStream;
13#[cfg(feature = "http")]
14use quote::quote;
15#[cfg(any(feature = "http", feature = "exit", feature = "actix-web"))]
16use syn::{DeriveInput, parse_macro_input};
17
18#[cfg(feature = "http")]
19#[proc_macro_derive(HttpError)]
20pub fn derive_http_error(input: TokenStream) -> TokenStream {
21 let input = parse_macro_input!(input as DeriveInput);
22
23 domain::derive(input, "explicit_error_http")
24 .unwrap_or_else(syn::Error::into_compile_error)
25 .into()
26}
27
28#[cfg(feature = "exit")]
29#[proc_macro_derive(ExitError)]
30pub fn derive_bin_error(input: TokenStream) -> TokenStream {
31 let input = parse_macro_input!(input as DeriveInput);
32
33 domain::derive(input, "explicit_error_exit")
34 .unwrap_or_else(syn::Error::into_compile_error)
35 .into()
36}
37
38#[cfg(feature = "actix-web")]
39#[proc_macro_derive(HandlerError)]
40pub fn derive_actix_handler_error(input: TokenStream) -> TokenStream {
41 let input = parse_macro_input!(input as DeriveInput);
42
43 actix::derive(input)
44 .unwrap_or_else(syn::Error::into_compile_error)
45 .into()
46}
47
48#[cfg(feature = "http")]
49#[proc_macro_derive(JSONDisplay)]
50pub fn json_display(input: TokenStream) -> TokenStream {
51 let input = parse_macro_input!(input as DeriveInput);
52 let ident = input.ident;
53 let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
54
55 let expanded = quote! {
56 #[automatically_derived]
57 impl #impl_generics std::fmt::Display for #ident #ty_generics #where_clause {
58 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59 write!(f, "{}", serde_json::json!(self))
60 }
61 }
62 };
63
64 TokenStream::from(expanded)
65}