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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/// Build a declaration body including common fields.
macro_rules! decl_body {
    (pub struct $name:ident<$f:ident> { $($rest:tt)* }) => {
        #[derive(Debug, Clone, Serialize)]
        #[serde(bound = "F: ::serde::Serialize, F::Field: ::serde::Serialize, F::Endpoint: ::serde::Serialize, F::Package: ::serde::Serialize, F::Name: ::serde::Serialize")]
        pub struct $name<$f: 'static> where $f: $crate::flavor::Flavor {
            pub name: $f::Name,
            pub ident: String,
            pub comment: Vec<String>,
            pub decls: Vec<$crate::rp_decl::RpDecl<$f>>,
            $($rest)*
        }
    };
}

#[macro_export]
macro_rules! decl_flavor {
    ($flavor:ident, $source:ident) => {
        pub type RpAccept = $source::RpAccept;
        pub type RpCode = $source::RpCode;
        pub type RpContext = $source::RpContext;
        pub type RpDecl = $source::RpDecl<$flavor>;
        pub type RpEndpoint = $source::RpEndpoint<$flavor>;
        pub type RpEndpointArgument = $source::RpEndpointArgument<$flavor>;
        pub type RpEndpointHttp = $source::RpEndpointHttp<$flavor>;
        pub type RpEndpointHttp1 = $source::RpEndpointHttp1<$flavor>;
        pub type RpEnumBody = $source::RpEnumBody<$flavor>;
        pub type RpField = $source::RpField<$flavor>;
        pub type RpFile = $source::RpFile<$flavor>;
        pub type RpHttpMethod = $source::RpHttpMethod;
        pub type RpInterfaceBody = $source::RpInterfaceBody<$flavor>;
        pub type RpPathPart = $source::RpPathPart<$flavor>;
        pub type RpPathSpec = $source::RpPathSpec<$flavor>;
        pub type RpPathStep = $source::RpPathStep<$flavor>;
        pub type RpReg = $source::RpReg;
        pub type RpNamed<'a> = $source::RpNamed<'a, $flavor>;
        pub type RpSubType = $source::RpSubType<$flavor>;
        pub type RpTupleBody = $source::RpTupleBody<$flavor>;
        pub type RpTypeBody = $source::RpTypeBody<$flavor>;
        pub type RpChannel = $source::RpChannel<$flavor>;
        pub type RpEnumOrdinal = $source::RpEnumOrdinal;
        pub type RpEnumType = $source::RpEnumType;
        pub type RpName = $source::RpName<$flavor>;
        pub type RpNumber = $source::RpNumber;
        pub type RpPackage = $source::RpPackage;
        pub type RpRequiredPackage = $source::RpRequiredPackage;
        pub type RpServiceBody = $source::RpServiceBody<$flavor>;
        pub type RpServiceBodyHttp = $source::RpServiceBodyHttp;
        pub type RpSubTypeStrategy = $source::RpSubTypeStrategy;
        pub type RpType = $source::RpType<$flavor>;
        pub type RpValue = $source::RpValue;
        pub type RpVariant = $source::RpVariant<$flavor>;
        pub type RpVersionedPackage = $source::RpVersionedPackage;
    };
}

#[macro_export]
macro_rules! translator_defaults {
    ($slf:ident $($rest:tt)*) => {
        translator_defaults!(@internal $slf $($rest)*);
    };

    (@internal $slf:ident, local_name $($rest:tt)*) => {
        fn translate_local_name<T>(
            &self,
            translator: &T,
            _reg: $crate::RpReg,
            name: $crate::RpName<$slf::Source>,
        ) -> Result<$crate::RpName<$slf::Target>>
        where
            T: Translator<Source = $slf::Source, Target = $slf::Target>,
        {
            name.translate(translator)
        }

        translator_defaults!(@internal $slf $($rest)*);
    };

    (@internal $slf:ident, field $($rest:tt)*) => {
        fn translate_field<T>(
            &self,
            translator: &T,
            field: $crate::RpField<$slf::Source>,
        ) -> Result<$crate::RpField<$slf::Target>>
        where
            T: Translator<Source = $slf::Source, Target = $slf::Target>,
        {
            field.translate(translator)
        }

        translator_defaults!(@internal $slf $($rest)*);
    };

    (@internal $slf:ident, endpoint $($rest:tt)*) => {
        fn translate_endpoint<T>(
            &self,
            translator: &T,
            endpoint: $crate::RpEndpoint<$slf::Source>,
        ) -> Result<$crate::RpEndpoint<$slf::Target>>
        where
            T: Translator<Source = $slf::Source, Target = $slf::Target>,
        {
            endpoint.translate(translator)
        }

        translator_defaults!(@internal $slf $($rest)*);
    };

    (@internal $slf:ident, rp_type $($rest:tt)*) => {
        fn translate_i32(&self) -> Result<RpType<$slf::Target>> {
            Ok(RpType::Signed { size: 32 })
        }

        fn translate_i64(&self) -> Result<RpType<$slf::Target>> {
            Ok(RpType::Signed { size: 64 })
        }

        fn translate_u32(&self) -> Result<RpType<$slf::Target>> {
            Ok(RpType::Unsigned { size: 32 })
        }

        fn translate_u64(&self) -> Result<RpType<$slf::Target>> {
            Ok(RpType::Unsigned { size: 64 })
        }

        fn translate_float(&self) -> Result<RpType<$slf::Target>> {
            Ok(RpType::Float)
        }

        fn translate_double(&self) -> Result<RpType<$slf::Target>> {
            Ok(RpType::Double)
        }

        fn translate_boolean(&self) -> Result<RpType<$slf::Target>> {
            Ok(RpType::Boolean)
        }

        fn translate_string(&self) -> Result<RpType<$slf::Target>> {
            Ok(RpType::String)
        }

        fn translate_datetime(&self) -> Result<RpType<$slf::Target>> {
            Ok(RpType::DateTime)
        }

        fn translate_array(&self, inner: RpType<$slf::Target>) -> Result<RpType<$slf::Target>> {
            Ok(RpType::Array {
                inner: Box::new(inner),
            })
        }

        fn translate_map(
            &self,
            key: RpType<$slf::Target>,
            value: RpType<$slf::Target>,
        ) -> Result<RpType<$slf::Target>> {
            Ok(RpType::Map {
                key: Box::new(key),
                value: Box::new(value),
            })
        }

        fn translate_any(&self) -> Result<RpType<$slf::Target>> {
            Ok(RpType::Any)
        }

        fn translate_bytes(&self) -> Result<RpType<$slf::Target>> {
            Ok(RpType::Bytes)
        }

        fn translate_name(
            &self,
            _reg: RpReg,
            name: RpName<$slf::Target>,
        ) -> Result<<$slf::Target as Flavor>::Type> {
            Ok(RpType::Name { name })
        }

        translator_defaults!(@internal $slf $($rest)*);
    };

    (@internal $slf:ident) => {
    };
}