error_macro/
struct.rs

1// Ref https://learning-rust.github.io/docs/b2.structs.html
2// Ref https://doc.rust-lang.org/rust-by-example/custom_types/structs.html
3
4//
5#[cfg(feature = "std")]
6#[macro_export]
7macro_rules! r#struct {
8    (
9        $( #[$meta:meta] )*
10        $pub:vis struct $name:ident {
11            $( $field_pub:vis $field_name:ident: $field_ty:ty ),* $(,)?
12        }
13    ) => {
14        $crate::internal_c_like_struct! {
15            $( #[$meta] )*
16            $pub struct $name {
17                $( $field_pub $field_name: $field_ty ),*
18            }
19        }
20
21        impl ::std::error::Error for $name {}
22    };
23
24    (
25        $( #[$meta:meta] )*
26        $pub:vis struct $name:ident($( $inner_pub:vis $inner_ty:ty ),* $(,)? );
27    ) => {
28        $crate::internal_tuple_struct! {
29            $( #[$meta] )*
30            $pub struct $name($( $inner_pub $inner_ty ),*);
31        }
32
33        impl ::std::error::Error for $name {}
34    };
35
36    (
37        $( #[$meta:meta] )*
38        $pub:vis struct $name:ident;
39    ) => {
40        $crate::internal_unit_struct! {
41            $( #[$meta] )*
42            $pub struct $name;
43        }
44
45        impl ::std::error::Error for $name {}
46    };
47}
48
49#[cfg(not(feature = "std"))]
50#[macro_export]
51macro_rules! r#struct {
52    (
53        $( #[$meta:meta] )*
54        $pub:vis struct $name:ident {
55            $( $field_pub:vis $field_name:ident: $field_ty:ty ),* $(,)?
56        }
57    ) => {
58        $crate::internal_c_like_struct! {
59            $( #[$meta] )*
60            $pub struct $name {
61                $( $field_pub $field_name: $field_ty ),*
62            }
63        }
64    };
65
66    (
67        $( #[$meta:meta] )*
68        $pub:vis struct $name:ident($( $inner_pub:vis $inner_ty:ty ),* $(,)? );
69    ) => {
70        $crate::internal_tuple_struct! {
71            $( #[$meta] )*
72            $pub struct $name($( $inner_pub $inner_ty ),*);
73        }
74    };
75
76    (
77        $( #[$meta:meta] )*
78        $pub:vis struct $name:ident;
79    ) => {
80        $crate::internal_unit_struct! {
81            $( #[$meta] )*
82            $pub struct $name;
83        }
84    };
85}
86
87//
88#[macro_export(local_inner_macros)]
89macro_rules! internal_c_like_struct {
90    (
91        $( #[$meta:meta] )*
92        $pub:vis struct $name:ident {
93            $( $field_pub:vis $field_name:ident: $field_ty:ty ),* $(,)?
94        }
95    ) => {
96        $( #[$meta] )*
97        #[derive(Debug)]
98        $pub struct $name {
99            $( $field_pub $field_name: $field_ty ),*
100        }
101
102        impl ::core::fmt::Display for $name {
103            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
104                ::core::write!(f, "{:?}", self)
105            }
106        }
107    }
108}
109
110#[macro_export(local_inner_macros)]
111macro_rules! internal_tuple_struct {
112    (
113        $( #[$meta:meta] )*
114        $pub:vis struct $name:ident($( $inner_pub:vis $inner_ty:ty ),* $(,)? );
115    ) => {
116        $( #[$meta] )*
117        #[derive(Debug)]
118        $pub struct $name($( $inner_pub $inner_ty ),*);
119
120        impl ::core::fmt::Display for $name {
121            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
122                ::core::write!(f, "{:?}", self)
123            }
124        }
125    }
126}
127
128#[macro_export(local_inner_macros)]
129macro_rules! internal_unit_struct {
130    (
131        $( #[$meta:meta] )*
132        $pub:vis struct $name:ident;
133    ) => {
134        $( #[$meta] )*
135        #[derive(Debug)]
136        $pub struct $name;
137
138        impl ::core::fmt::Display for $name {
139            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
140                ::core::write!(f, "{:?}", self)
141            }
142        }
143    }
144}