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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// SPDX-License-Identifier: Apache-2.0

//! Procedural macro crate for `substrait-validator-core`.
//!
//! The derive macros defined here are essentially an ugly workaround for the
//! lack of any protobuf introspection functionality provided by prost.
//! Basically, they take (the AST of) the code generated by prost and try to
//! recover the needed protobuf message metadata from there. Things would have
//! been a *LOT* simpler and a *LOT* less brittle if prost would simply
//! provide this information via traits of its own, but alas, there doesn't
//! seem to be a way to do this without forking prost, and introspection
//! seems to be a non-goal of that project.
//!
//! Besides being ugly, this method is rather brittle and imprecise when it
//! comes to recovering field names, due to the various case conversions
//! automatically done by protoc and prost. Some known issues are:
//!
//!  - The recovered type name for messages defined within messages uses
//!    incorrect case conventions, as the procedural macros have no way
//!    of distinguishing packages from message definition scopes in the
//!    type path.
//!  - If the .proto source files use unexpected case conventions for
//!    various things, the resulting case conventions for types, field names,
//!    oneof variants, and enum variants will be wrong.
//!  - Whenever the .proto source files name a field using something that is
//!    a reserved word in Rust (notably `type`), prost will use a raw
//!    identifier to represent the name. This syntax is currently not filtered
//!    out from the recovered names, so a field named `type` becomes `r#type`.
//!    This is probably not a fundamental problem, though.
//!
//! Ultimately, however, these names are only used for diagnostic messages and
//! the likes. In the worst case, the above inconsistencies may confuse the
//! user, but they should not affect the valid/invalid/maybe-valid result of
//! the validator or cause compile- or runtime errors.

extern crate proc_macro;

use heck::{ToShoutySnakeCase, ToSnakeCase};
use proc_macro::TokenStream;
use quote::quote;

/// Converts a Rust identifier string generated via stringify!() to the
/// original identifier by "cooking" raw identifiers.
fn cook_ident(ident: &syn::Ident) -> String {
    let ident = ident.to_string();
    if let Some((_, keyword)) = ident.split_once('#') {
        keyword.to_string()
    } else {
        ident
    }
}

#[doc(hidden)]
#[proc_macro_derive(ProtoMeta, attributes(proto_meta))]
pub fn proto_meta(input: TokenStream) -> TokenStream {
    proto_meta_derive(syn::parse_macro_input!(input))
}

fn proto_meta_derive(ast: syn::DeriveInput) -> TokenStream {
    match ast.data {
        syn::Data::Struct(ref struct_data) => proto_meta_derive_message(&ast, struct_data),
        syn::Data::Enum(ref enum_data) => match enum_data.variants.iter().next().unwrap().fields {
            syn::Fields::Unit => {
                for variant in enum_data.variants.iter() {
                    if !matches!(variant.fields, syn::Fields::Unit) {
                        panic!("all variants of a protobuf oneof enum must have a single, unnamed field");
                    }
                }

                proto_meta_derive_enum(&ast, enum_data)
            }
            syn::Fields::Unnamed(..) => {
                for variant in enum_data.variants.iter() {
                    if let syn::Fields::Unnamed(fields) = &variant.fields {
                        if fields.unnamed.len() != 1 {
                            panic!("all variants of a protobuf oneof enum must have a single, unnamed field");
                        }
                    } else {
                        panic!("all variants of a protobuf oneof enum must have a single, unnamed field");
                    }
                }

                proto_meta_derive_oneof(&ast, enum_data)
            }
            _ => panic!("enum with named elements don't map to protobuf constructs"),
        },
        syn::Data::Union(_) => panic!("unions don't map to protobuf constructs"),
    }
}

enum FieldType {
    Optional,
    BoxedOptional,
    Repeated,
    Primitive,
}

fn is_repeated(typ: &syn::Type) -> FieldType {
    if let syn::Type::Path(path) = typ {
        if let Some(last) = path.path.segments.last() {
            if last.ident == "Option" {
                if let syn::PathArguments::AngleBracketed(ref args) = last.arguments {
                    if let syn::GenericArgument::Type(syn::Type::Path(path2)) =
                        args.args.first().unwrap()
                    {
                        if path2.path.segments.last().unwrap().ident == "Box" {
                            return FieldType::BoxedOptional;
                        } else {
                            return FieldType::Optional;
                        }
                    }
                }
                panic!("Option without type argument?");
            } else if last.ident == "Vec" {
                if let syn::PathArguments::AngleBracketed(ref args) = last.arguments {
                    if let syn::GenericArgument::Type(syn::Type::Path(path2)) =
                        args.args.first().unwrap()
                    {
                        if path2.path.segments.last().unwrap().ident == "u8" {
                            return FieldType::Primitive;
                        } else {
                            return FieldType::Repeated;
                        }
                    }
                }
                panic!("Vec without type argument?");
            }
        }
    }
    FieldType::Primitive
}

fn proto_meta_derive_message(ast: &syn::DeriveInput, data: &syn::DataStruct) -> TokenStream {
    let name = &ast.ident;
    let name_str = cook_ident(name);
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();

    let parse_unknown_matches: Vec<_> = data
        .fields
        .iter()
        .map(|field| {
            if let Some(ident) = &field.ident {
                let ident_str = cook_ident(ident);
                let action = match is_repeated(&field.ty) {
                    FieldType::Optional => quote! {
                        crate::parse::traversal::push_proto_field(
                            y,
                            &self.#ident.as_ref(),
                            #ident_str,
                            true,
                            |_, _| Ok(()),
                        );
                    },
                    FieldType::BoxedOptional => quote! {
                        crate::parse::traversal::push_proto_field(
                            y,
                            &self.#ident,
                            #ident_str,
                            true,
                            |_, _| Ok(()),
                        );
                    },
                    FieldType::Repeated => quote! {
                        crate::parse::traversal::push_proto_repeated_field(
                            y,
                            &self.#ident.as_ref(),
                            #ident_str,
                            true,
                            |_, _| Ok(()),
                            |_, _, _, _, _| (),
                        );
                    },
                    FieldType::Primitive => quote! {
                        use crate::input::traits::ProtoPrimitive;
                        if !y.config.ignore_unknown_fields || !self.#ident.proto_primitive_is_default() {
                            crate::parse::traversal::push_proto_field(
                                y,
                                &Some(&self.#ident),
                                #ident_str,
                                true,
                                |_, _| Ok(()),
                            );
                        }
                    },
                };
                quote! {
                    if !y.field_parsed(#ident_str) {
                        unknowns = true;
                        #action
                    }
                }
            } else {
                panic!("protobuf message fields must have names");
            }
        })
        .collect();

    quote!(
        impl #impl_generics crate::input::traits::ProtoMessage for #name #ty_generics #where_clause {
            fn proto_message_type() -> &'static str {
                use ::once_cell::sync::Lazy;
                static TYPE_NAME: Lazy<::std::string::String> = Lazy::new(|| {
                    crate::input::proto::cook_path(module_path!(), #name_str)
                });
                &TYPE_NAME
            }
        }

        impl #impl_generics crate::input::traits::InputNode for #name #ty_generics #where_clause {
            fn type_to_node() -> crate::output::tree::Node {
                use crate::input::traits::ProtoMessage;
                crate::output::tree::NodeType::ProtoMessage(Self::proto_message_type()).into()
            }

            fn data_to_node(&self) -> crate::output::tree::Node {
                use crate::input::traits::ProtoMessage;
                crate::output::tree::NodeType::ProtoMessage(Self::proto_message_type()).into()
            }

            fn oneof_variant(&self) -> Option<&'static str> {
                None
            }

            fn parse_unknown(
                &self,
                y: &mut crate::parse::context::Context<'_>,
            ) -> bool {
                let mut unknowns = false;
                #(#parse_unknown_matches)*
                unknowns
            }
        }
    )
    .into()
}

fn proto_meta_derive_oneof(ast: &syn::DeriveInput, data: &syn::DataEnum) -> TokenStream {
    let name = &ast.ident;
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();

    let variant_matches: Vec<_> = data
        .variants
        .iter()
        .map(|variant| {
            let ident = &variant.ident;
            let proto_name = cook_ident(ident).to_snake_case();
            quote! { #name::#ident (_) => #proto_name }
        })
        .collect();

    let node_matches: Vec<_> = data
        .variants
        .iter()
        .map(|variant| {
            let ident = &variant.ident;
            quote! { #name::#ident (x) => x.data_to_node() }
        })
        .collect();

    let parse_unknown_matches: Vec<_> = data
        .variants
        .iter()
        .map(|variant| {
            let ident = &variant.ident;
            quote! { #name::#ident (x) => x.parse_unknown(y) }
        })
        .collect();

    quote!(
        impl #impl_generics crate::input::traits::ProtoOneOf for #name #ty_generics #where_clause {
            fn proto_oneof_variant(&self) -> &'static str {
                match self {
                    #(#variant_matches),*
                }
            }
        }

        impl #impl_generics crate::input::traits::InputNode for #name #ty_generics #where_clause {
            fn type_to_node() -> crate::output::tree::Node {
                crate::output::tree::NodeType::ProtoMissingOneOf.into()
            }

            fn data_to_node(&self) -> crate::output::tree::Node {
                match self {
                    #(#node_matches),*
                }
            }

            fn oneof_variant(&self) -> Option<&'static str> {
                use crate::input::traits::ProtoOneOf;
                Some(self.proto_oneof_variant())
            }

            fn parse_unknown(
                &self,
                y: &mut crate::parse::context::Context<'_>,
            ) -> bool {
                match self {
                    #(#parse_unknown_matches),*
                }
            }
        }
    )
    .into()
}

fn proto_meta_derive_enum(ast: &syn::DeriveInput, data: &syn::DataEnum) -> TokenStream {
    let name = &ast.ident;
    let name_str = cook_ident(name);
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();

    let upper_name = name_str.to_shouty_snake_case();

    let variant_names: Vec<_> = data
        .variants
        .iter()
        .map(|variant| {
            let ident = &variant.ident;
            let proto_name = format!(
                "{}_{}",
                upper_name,
                cook_ident(ident).to_shouty_snake_case()
            );
            (ident, proto_name)
        })
        .collect();

    let variant_matches: Vec<_> = variant_names
        .iter()
        .map(|(ident, proto_name)| {
            quote! { #name::#ident => #proto_name }
        })
        .collect();

    let (_, first_variant_name) = &variant_names[0];

    quote!(
        impl #impl_generics crate::input::traits::ProtoEnum for #name #ty_generics #where_clause {
            fn proto_enum_type() -> &'static str {
                use ::once_cell::sync::Lazy;
                static TYPE_NAME: Lazy<::std::string::String> = Lazy::new(|| {
                    crate::input::proto::cook_path(module_path!(), #name_str)
                });
                &TYPE_NAME
            }

            fn proto_enum_default_variant() -> &'static str {
                #first_variant_name
            }

            fn proto_enum_variant(&self) -> &'static str {
                match self {
                    #(#variant_matches),*
                }
            }

            fn proto_enum_from_i32(x: i32) -> Option<Self> {
                Self::from_i32(x)
            }
        }
    )
    .into()
}