stackerror_impl/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_attribute]
pub fn derive_stack_error(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let input = parse_macro_input!(item as DeriveInput);
    let name = &input.ident;
    let first_field_type = if let syn::Data::Struct(data) = &input.data {
        if let Some(field) = data.fields.iter().next() {
            &field.ty
        } else {
            panic!("Expected at least one field");
        }
    } else {
        panic!("Expected a struct");
    };

    let expanded = quote! {
        #input

        impl #name {
            fn new(error: impl std::fmt::Display + Send + Sync + 'static) -> Self {
                Self(#first_field_type::new(error))
            }
        }

        impl ErrorStacks for #name {
            fn stack_err(self, error: impl std::fmt::Display + Send + Sync + 'static) -> Self {
                Self(self.0.stack_err(error))
            }
        }

        impl ErrorWithCode<ErrorCode> for #name {
            fn err_code(&self) -> Option<&ErrorCode> {
                self.0.err_code()
            }

            fn with_err_code(self, code: Option<ErrorCode>) -> Self {
                Self(self.0.with_err_code(code))
            }
        }

        impl ErrorWithUri for #name {
            fn err_uri(&self) -> Option<&str> {
                self.0.err_uri()
            }

            fn with_err_uri(self, uri: Option<String>) -> Self {
                Self(self.0.with_err_uri(uri))
            }
        }

        impl ErrorStacksWithCode for #name {
            fn stack_err_code(self, error: impl std::fmt::Display + Send + Sync + 'static) -> Self {
                Self(self.0.stack_err_code(error))
            }
        }

        impl ErrorStacksWithUri for #name {
            fn stack_err_uri(self, error: impl std::fmt::Display + Send + Sync + 'static) -> Self {
                Self(self.0.stack_err_uri(error))
            }
        }

        impl ErrorStacksWithCodeUri for #name {
            fn stack_err_code_uri(self, error: impl std::fmt::Display + Send + Sync + 'static) -> Self {
                Self(self.0.stack_err_code_uri(error))
            }
        }

        impl std::fmt::Display for #name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.0.fmt(f)
            }
        }

        impl std::fmt::Debug for #name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.0.fmt(f)
            }
        }

        impl std::error::Error for #name {
            fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
                self.0.source()
            }
        }
    };

    TokenStream::from(expanded)
}