proto-json 0.1.0

convert to and from protobuf -> json easily
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
extern crate proc_macro;
extern crate syn;
use quote::quote;
use syn::{
    fold::{fold_type, Fold},
    parse_macro_input, parse_quote,
    punctuated::Punctuated,
    visit::Visit,
    Attribute, Data, DataEnum, DataStruct, DeriveInput, Field, Fields, LitStr, Path, PathSegment,
    Type, TypePath,
};

/// Helps to glue together json and protobufs when placed on a valid prost::Message, prost::Enumeration, or prost::Oneof
/// For structs, it walks the fields checking whether they are enums then adds serialize_with and deserialize_with attributes to relevant fields.
/// Structs need to have the prost::Message attribute while Enums require the prost::Enumeration attribute
/// For enums, it checks that the provided string is a valid variant then deserializes it as i32 to match the protobuf definitions
/// # Example
/// ```
/// # #[macros::proto_json]
/// pub struct Address {
///     country : String,
///     city : String,
///     state : Option<String>,
///     street : String,
///     line1   : String,
///     line2   : Option<String>
/// # }
/// ```
/// Example with enums
/// ```
/// # #[macros::proto_json]
/// pub enum Currency {
///         USD = 0;
///         GPB = 1;
///         JPY = 2;
/// # }
/// ```
#[proc_macro_attribute]
pub fn proto_json(
    _attr: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> ::proc_macro::TokenStream {
    let mut ast = parse_macro_input!(input as DeriveInput);

    let ident = &ast.ident;

    let mut is_prost_message = false;
    let mut is_prost_enumeration = false;
    let mut is_prost_one_of = false;

    // If item does not implement one of the following attributes, then return error as it is not a valid protobuf object.
    for attrib in ast.attrs.iter() {
        if attrib.path().is_ident("derive") {
            attrib
                .parse_nested_meta(|meta| {
                    match meta.path.leading_colon {
                        Some(_) => match meta
                            .path
                            .segments
                            .last()
                            .unwrap()
                            .ident
                            .to_string()
                            .as_str()
                        {
                            "Message" => is_prost_message = true,
                            "Enumeration" => is_prost_enumeration = true,
                            "Oneof" => is_prost_one_of = true,
                            _ => (),
                        },
                        None => (),
                    }
                    Ok(())
                })
                .unwrap();
        }
    }

    let generated: proc_macro2::TokenStream = match ast.data {
        Data::Enum(ref mut de) => {
            match is_prost_enumeration {
                // Implement the str_to_i32 and i32_to_str methods
                true => {
                    // Iterate over enum variants
                    let variants = de.variants.iter().map(|v| &v.ident);

                    // Convert variant name to snake_case
                    let variant_str_as_i32 = variants.clone().map(|variant| {
                        let variant_str = &::convert_case::Casing::to_case(
                            &variant.to_string(),
                            ::convert_case::Case::Snake,
                        );
                        // usd => Ok(Currency::Usd as i32)
                        quote! (#variant_str => Ok(#ident::#variant as i32))
                    });

                    // Creates a list of the enum's fields. Used to give useful error messages when deserializing.
                    let expected_fields = variants
                        .clone()
                        .map(|variant| {
                            let variant_str = ::convert_case::Casing::to_case(
                                &variant.to_string(),
                                ::convert_case::Case::Snake,
                            );
                            variant_str
                        })
                        .into_iter()
                        .map(|x| x)
                        .collect::<Vec<String>>()
                        .join(",");

                    let serde_funcs = quote! {
                        /// Methods for converting from Protofbuf to and from Json enums
                        impl #ident {
                            /// Deserialize enum from string to protobuf i32
                            pub fn  str_to_i32<'de, D>(deserializer: D) -> core::result::Result<i32, D::Error>
                            where
                                D: serde::de::Deserializer<'de>,
                            {
                                let s: &str = serde::de::Deserialize::deserialize(deserializer)?;

                                match s.to_lowercase().as_str() {
                                    #(#variant_str_as_i32,)*
                                    _ => core::result::Result::Err(serde::de::Error::unknown_variant(s, &[#expected_fields])),
                                }
                            }
                            /// Deserialize optional enum from string to optional protobuf i32
                            pub fn str_to_i32_opt<'de, D>(deserializer: D) -> core::result::Result<Option<i32>, D::Error>
                            where
                                D: serde::de::Deserializer<'de>,
                            {
                                let s: Option<&str> = serde::de::Deserialize::deserialize(deserializer)?;

                                if let Some(s) = s {
                                    return Ok(Some(
                                        s.to_lowercase()
                                            .as_str()
                                            .parse::<Self>()
                                            .map_err(|_| serde::de::Error::unknown_variant(s, &[#expected_fields]))?
                                            as i32));
                                }

                                Ok(None)
                            }
                            /// Serialize enum from protobuf i32 to json string
                            pub fn i32_to_str<S>(data: &i32, serializer: S) -> core::result::Result<S::Ok, S::Error>
                            where
                                S: serde::Serializer,
                            {
                                serializer.serialize_str(&Self::try_from(data.to_owned()).unwrap().to_string())
                            }
                            /// Serialize enum from optional protobuf i32 to optional json string
                            pub fn i32_to_str_opt<S>(
                                data: &Option<i32>,
                                serializer: S,
                            ) -> core::result::Result<S::Ok, S::Error>
                            where
                                S: serde::Serializer,
                            {
                                if let Some(ref d) = *data {
                                    return serializer.serialize_str(&Self::try_from(d.to_owned()).unwrap().to_string());
                                }
                                serializer.serialize_none()
                            }
                            // Deserialize from a vec string to a vec i32
                            pub fn vec_str_to_vec_i32<'de, D>(deserializer: D) -> core::result::Result<Vec<i32>, D::Error>
                            where
                                D: serde::de::Deserializer<'de>,
                            {
                                let strings: Vec<&str> = serde::de::Deserialize::deserialize(deserializer)?;

                                let mut result = Vec::with_capacity(strings.len());

                                for s in strings {
                                    match s.parse::<Self>() {
                                        Ok(num) => result.push(num as i32),
                                        Err(_) => {
                                            return Err(serde::de::Error::invalid_value(
                                                serde::de::Unexpected::Str(s),
                                                &#expected_fields,
                                            ))
                                        }
                                    }
                                }

                                Ok(result)
                            }
                            // Serializes a vec of enum i32s to a vec of strings
                            pub fn vec_i32_to_vec_str<S>(
                                data: &Vec<i32>,
                                serializer: S,
                            ) -> core::result::Result<S::Ok, S::Error>
                            where
                                S: serde::Serializer,
                            {
                                let mut seq = serializer.serialize_seq(Some(data.len()))?;

                                for &i in data {
                                    serde::ser::SerializeSeq::serialize_element(
                                        &mut seq,
                                        &Self::try_from(i.to_owned()).unwrap().to_string(),
                                    )?;
                                }

                                serde::ser::SerializeSeq::end(seq)
                            }
                        }

                    };

                    quote! {
                        #ast
                        #serde_funcs
                    }
                    .into()
                }
                // Check if the given item has the prost::Oneof attribute which indicates an enum nested inside a module
                false => match is_prost_one_of {
                    true => {
                        let attribute: Attribute = parse_quote! {
                            #[derive(serde::Serialize, serde::Deserialize)]
                        };

                        let attribute2: Attribute = parse_quote!(
                            #[serde(rename_all = "snake_case")]
                        );

                        ast.attrs.push(attribute);
                        ast.attrs.push(attribute2);

                        // Generate a new TokenTree of data type DataEnum
                        let new = Data::Enum(DataEnum {
                            enum_token: de.enum_token,
                            brace_token: de.brace_token,
                            variants: de.variants.clone(),
                        });

                        // Copy over the attributes from the original enum for max compatibility
                        let new_enum = DeriveInput {
                            attrs: ast.attrs,
                            vis: ast.vis,
                            ident: ident.clone(),
                            generics: ast.generics,
                            data: new,
                        };

                        quote! {#new_enum}.into()
                    }
                    false => {
                        return ::syn::Error::new_spanned(
                            &ident,
                            "Could not parse the item as a valid protobuf Enum",
                        )
                        .to_compile_error()
                        .into();
                    }
                },
            }
        }
        // A struct that implements prost::Message
        ::syn::Data::Struct(ref mut ds) => match &ds.fields {
            Fields::Named(fields) => {
                match is_prost_message {
                    true => {
                        let mut new_fields = fields.to_owned();

                        new_fields.named.iter_mut().for_each(|f| match is_option(f) {
                            true => {
                                match check_struct_field_for_prost_enumeration_attribute(&f.attrs) {
                                    Some(a) => {
                                        match check_is_vec(&f.ty) {
                                            // check whether field is vec 
                                            true => {
                                                let serializer = format!("{a}::vec_i32_to_vec_str");
                                                let deserializer = format!("{a}::vec_str_to_vec_i32");

                                                // Create a new serialize_with, deserialize_with attribute
                                                let new_attr: Attribute = parse_quote! {
                                                    #[serde( default, deserialize_with = #deserializer, serialize_with = #serializer)]
                                                };

                                                f.attrs.push(new_attr);
                                            },
                                            false => {
                                                let serializer = format!("{a}::i32_to_str_opt");
                                                let deserializer = format!("{a}::str_to_i32_opt");

                                                // Create a new serialize_with, deserialize_with attribute
                                                let new_attr: Attribute = parse_quote! {
                                                    #[serde( default, deserialize_with = #deserializer, serialize_with = #serializer, skip_serializing_if = "Option::is_none" )]
                                                };

                                                f.attrs.push(new_attr);
                                            }
                                        }
                                    },
                                    None => ()
                                }
                            }
                            false => {

                                match check_struct_field_for_prost_enumeration_attribute(&f.attrs) {
                                    Some(a) => {
                                        match check_is_vec(&f.ty) {
                                            true => {
                                                let serializer = format!("{a}::vec_i32_to_vec_str");
                                                let deserializer = format!("{a}::vec_str_to_vec_i32");
                                                // Create a new serialize_with, deserialize_with attribute
                                                let new_attr: Attribute = parse_quote! {
                                                    #[serde(default, deserialize_with = #deserializer, serialize_with = #serializer)]
                                                };
                                                f.attrs.push(new_attr);
                                            },
                                            false => {
                                                let serializer = format!("{a}::i32_to_str");
                                                let deserializer = format!("{a}::str_to_i32");
                                                // Create a new serialize_with, deserialize_with attribute
                                                let new_attr: Attribute = parse_quote! {
                                                    #[serde(default, deserialize_with = #deserializer, serialize_with = #serializer)]
                                                };
                                                f.attrs.push(new_attr);
                                            }
                                        }
                                    },
                                    None => ()
                                }
                            }
                        });

                        let new = Data::Struct(DataStruct {
                            struct_token: ds.struct_token,
                            fields: Fields::Named(new_fields),
                            semi_token: ds.semi_token,
                        });

                        let new_st = DeriveInput {
                            attrs: ast.attrs,
                            vis: ast.vis,
                            ident: ident.clone(),
                            generics: ast.generics,
                            data: new,
                        };

                        quote! {#new_st}.into()
                    }
                    false => {
                        return ::syn::Error::new_spanned(
                            &ident,
                            "ProtoJson only works with Protobuf Structs",
                        )
                        .to_compile_error()
                        .into();
                    }
                }
            }
            _ => {
                return ::syn::Error::new_spanned(
                    &ds.fields,
                    "ProtoJson only supports named field structs",
                )
                .to_compile_error()
                .into();
            }
        },
        _ => {
            return ::syn::Error::new_spanned(
                &ident,
                "Only items with named fields can derive ProtoJson",
            )
            .to_compile_error()
            .into();
        }
    };

    ::proc_macro::TokenStream::from(generated)
}

/// Checks if a struct field is Optional
fn is_option(field: &Field) -> bool {
    let typ = &field.ty;

    let opt = match typ {
        Type::Path(typepath) if typepath.qself.is_none() => Some(typepath.path.clone()),
        _ => None,
    };

    if let Some(o) = opt {
        check_for_option(&o).is_some()
    } else {
        false
    }
}

/// Walks the path segments to check for Option
fn check_for_option(path: &Path) -> Option<&PathSegment> {
    let idents_of_path = path.segments.iter().fold(String::new(), |mut acc, v| {
        acc.push_str(&v.ident.to_string());
        acc.push(':');
        acc
    });
    vec!["Option:", "std:option:Option:", "core:option:Option:"]
        .into_iter()
        .find(|s| idents_of_path == *s)
        .and_then(|_| path.segments.last())
}


/// Checks whether the attribute has a prost-enumeration member and returns an optional ident of the name
fn check_struct_field_for_prost_enumeration_attribute(attrs: &[Attribute]) -> Option<String> {
    // If a #[prost(enumeration = "value")] exists, this is the optional name of the value
    let mut attrib: Option<String> = None;

    //
    for attr in attrs {
        // Looks for attributes in the form #[prost(enumeration = "Currency", tag = "2")]
        if attr.path().is_ident("prost") {
            // Check for the "enumeration" part in the enumeration = "Currency" and parse it as KV.
            attr.parse_nested_meta(|meta| {
                // #[prost(enumeration = "Ident")]
                if meta.path.is_ident("enumeration") {
                    // Get the Currency in enumeration = "Currency". Gives back a string literal (LitStr)
                    let value = meta.value().unwrap();
                    //
                    attrib = Some(value.parse::<LitStr>().unwrap().value());

                    return Ok(());
                // If there is no enumeration attribute, do nothing
                } else {
                    Ok(())
                }
            })
            .unwrap_or(());
        }
    }
    attrib
}

struct VecTypeVisitor {
    is_vec: bool,
}

impl<'ast> Visit<'ast> for VecTypeVisitor {
    fn visit_path_segment(&mut self, segment: &'ast syn::PathSegment) {
        if segment.ident == "Vec" {
            self.is_vec = true;
        }
    }
}

impl Fold for VecTypeVisitor {
    fn fold_type_path(&mut self, type_path: TypePath) -> TypePath {
        let mut new_segments = Punctuated::new();

        for segment in type_path.path.segments {
            new_segments.push(self.fold_path_segment(segment));
        }

        TypePath {
            qself: type_path.qself,
            path: syn::Path {
                leading_colon: type_path.path.leading_colon,
                segments: new_segments,
            },
        }
    }

    fn fold_path_segment(&mut self, segment: PathSegment) -> PathSegment {
        let new_segment = segment.clone();
        if segment.ident == "Vec" {
            self.is_vec = true;
        }
        new_segment
    }

    fn fold_type(&mut self, ty: Type) -> Type {
        match ty {
            Type::Path(type_path) => Type::Path(self.fold_type_path(type_path)),
            Type::Tuple(type_tuple) => {
                let new_elems = type_tuple
                    .elems
                    .into_iter()
                    .map(|ty| self.fold_type(ty))
                    .collect();
                Type::Tuple(syn::TypeTuple {
                    paren_token: type_tuple.paren_token,
                    elems: new_elems,
                })
            }
            // Add cases for other types as needed
            _ => ty,
        }
    }
}

fn check_is_vec(ty: &syn::Type) -> bool {
    let mut visitor = VecTypeVisitor { is_vec: false };
    fold_type(&mut visitor, ty.clone());
    visitor.is_vec
}