reproto_core/
macros.rs

1/// Build a declaration body including common fields.
2macro_rules! decl_body {
3    (pub struct $name:ident<$f:ident> { $($rest:tt)* }) => {
4        #[derive(Debug, Clone, Serialize)]
5        #[serde(bound = "F: ::serde::Serialize, F::Field: ::serde::Serialize, F::Endpoint: ::serde::Serialize, F::Package: ::serde::Serialize, F::Name: ::serde::Serialize, F::EnumType: ::serde::Serialize")]
6        pub struct $name<$f: 'static> where $f: $crate::flavor::Flavor {
7            pub name: $f::Name,
8            pub ident: String,
9            pub comment: Vec<String>,
10            pub decls: Vec<$crate::rp_decl::RpDecl<$f>>,
11            $($rest)*
12        }
13    };
14}
15
16#[macro_export]
17macro_rules! decl_flavor {
18    ($flavor:ident, $source:ident) => {
19        pub type RpAccept = $source::RpAccept;
20        pub type RpCode = $source::RpCode;
21        pub type RpContext = $source::RpContext;
22        pub type RpDecl = $source::RpDecl<$flavor>;
23        pub type RpEndpoint = $source::RpEndpoint<$flavor>;
24        pub type RpEndpointArgument = $source::RpEndpointArgument<$flavor>;
25        pub type RpEndpointHttp = $source::RpEndpointHttp<$flavor>;
26        pub type RpEndpointHttp1 = $source::RpEndpointHttp1<$flavor>;
27        pub type RpEnumBody = $source::RpEnumBody<$flavor>;
28        pub type RpField = $source::RpField<$flavor>;
29        pub type RpFile = $source::RpFile<$flavor>;
30        pub type RpHttpMethod = $source::RpHttpMethod;
31        pub type RpInterfaceBody = $source::RpInterfaceBody<$flavor>;
32        pub type RpPathPart = $source::RpPathPart<$flavor>;
33        pub type RpPathSpec = $source::RpPathSpec<$flavor>;
34        pub type RpPathStep = $source::RpPathStep<$flavor>;
35        pub type RpReg = $source::RpReg;
36        pub type RpNamed<'a> = $source::RpNamed<'a, $flavor>;
37        pub type RpSubType = $source::RpSubType<$flavor>;
38        pub type RpTupleBody = $source::RpTupleBody<$flavor>;
39        pub type RpTypeBody = $source::RpTypeBody<$flavor>;
40        pub type RpChannel = $source::RpChannel<$flavor>;
41        pub type RpEnumType = $source::RpEnumType;
42        pub type RpName = $source::RpName<$flavor>;
43        pub type RpNumber = $source::RpNumber;
44        pub type RpPackage = $source::RpPackage;
45        pub type RpRequiredPackage = $source::RpRequiredPackage;
46        pub type RpServiceBody = $source::RpServiceBody<$flavor>;
47        pub type RpServiceBodyHttp = $source::RpServiceBodyHttp;
48        pub type RpSubTypeStrategy = $source::RpSubTypeStrategy;
49        pub type RpType = $source::RpType<$flavor>;
50        pub type RpValue = $source::RpValue;
51        pub type RpVariant<V> = $source::RpVariant<$flavor, V>;
52        pub type RpVariantRef<'a> = $source::RpVariantRef<'a, $flavor>;
53        pub type RpVersionedPackage = $source::RpVersionedPackage;
54    };
55}
56
57#[macro_export]
58macro_rules! translator_defaults {
59    ($slf:ident $($rest:tt)*) => {
60        translator_defaults!(@internal $slf $($rest)*);
61    };
62
63    (@internal $slf:ident, local_name $($rest:tt)*) => {
64        fn translate_local_name<T>(
65            &self,
66            translator: &T,
67            diag: &mut $crate::Diagnostics,
68            _reg: $crate::RpReg,
69            name: $crate::Loc<$crate::RpName<$slf::Source>>,
70        ) -> Result<$crate::Loc<$crate::RpName<$slf::Target>>>
71        where
72            T: Translator<Source = $slf::Source, Target = $slf::Target>,
73        {
74            name.translate(diag, translator)
75        }
76
77        translator_defaults!(@internal $slf $($rest)*);
78    };
79
80    (@internal $slf:ident, field $($rest:tt)*) => {
81        fn translate_field<T>(
82            &self,
83            translator: &T,
84            diag: &mut $crate::Diagnostics,
85            field: $crate::RpField<$slf::Source>,
86        ) -> Result<$crate::RpField<$slf::Target>>
87        where
88            T: Translator<Source = $slf::Source, Target = $slf::Target>,
89        {
90            field.translate(diag, translator)
91        }
92
93        translator_defaults!(@internal $slf $($rest)*);
94    };
95
96    (@internal $slf:ident, endpoint $($rest:tt)*) => {
97        fn translate_endpoint<T>(
98            &self,
99            translator: &T,
100            diag: &mut $crate::Diagnostics,
101            endpoint: $crate::RpEndpoint<$slf::Source>,
102        ) -> Result<$crate::RpEndpoint<$slf::Target>>
103        where
104            T: Translator<Source = $slf::Source, Target = $slf::Target>,
105        {
106            endpoint.translate(diag, translator)
107        }
108
109        translator_defaults!(@internal $slf $($rest)*);
110    };
111
112    (@internal $slf:ident, rp_type $($rest:tt)*) => {
113        fn translate_i32(&self) -> Result<RpType<$slf::Target>> {
114            Ok(RpType::Signed { size: 32 })
115        }
116
117        fn translate_i64(&self) -> Result<RpType<$slf::Target>> {
118            Ok(RpType::Signed { size: 64 })
119        }
120
121        fn translate_u32(&self) -> Result<RpType<$slf::Target>> {
122            Ok(RpType::Unsigned { size: 32 })
123        }
124
125        fn translate_u64(&self) -> Result<RpType<$slf::Target>> {
126            Ok(RpType::Unsigned { size: 64 })
127        }
128
129        fn translate_float(&self) -> Result<RpType<$slf::Target>> {
130            Ok(RpType::Float)
131        }
132
133        fn translate_double(&self) -> Result<RpType<$slf::Target>> {
134            Ok(RpType::Double)
135        }
136
137        fn translate_boolean(&self) -> Result<RpType<$slf::Target>> {
138            Ok(RpType::Boolean)
139        }
140
141        fn translate_string(&self) -> Result<RpType<$slf::Target>> {
142            Ok(RpType::String)
143        }
144
145        fn translate_datetime(&self) -> Result<RpType<$slf::Target>> {
146            Ok(RpType::DateTime)
147        }
148
149        fn translate_array(&self, inner: RpType<$slf::Target>) -> Result<RpType<$slf::Target>> {
150            Ok(RpType::Array {
151                inner: Box::new(inner),
152            })
153        }
154
155        fn translate_map(
156            &self,
157            key: RpType<$slf::Target>,
158            value: RpType<$slf::Target>,
159        ) -> Result<RpType<$slf::Target>> {
160            Ok(RpType::Map {
161                key: Box::new(key),
162                value: Box::new(value),
163            })
164        }
165
166        fn translate_any(&self) -> Result<RpType<$slf::Target>> {
167            Ok(RpType::Any)
168        }
169
170        fn translate_bytes(&self) -> Result<RpType<$slf::Target>> {
171            Ok(RpType::Bytes)
172        }
173
174        fn translate_name(
175            &self,
176            _reg: RpReg,
177            name: Loc<RpName<$slf::Target>>,
178        ) -> Result<<$slf::Target as Flavor>::Type> {
179            Ok(RpType::Name { name })
180        }
181
182        translator_defaults!(@internal $slf $($rest)*);
183    };
184
185    (@internal $slf:ident, enum_type $($rest:tt)*) => {
186        fn translate_enum_type<T>(
187            &self,
188            _: &T,
189            _: &mut $crate::Diagnostics,
190            enum_type: $crate::RpEnumType,
191        ) -> Result<<$slf::Target as Flavor>::EnumType>
192        where
193            T: Translator<Source = $slf::Source, Target = $slf::Target>,
194        {
195            Ok(enum_type)
196        }
197
198        translator_defaults!(@internal $slf $($rest)*);
199    };
200
201    (@internal $slf:ident) => {
202    };
203}