| 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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
 | #![recursion_limit = "128"]
#[macro_use] extern crate quote;
extern crate proc_macro;
extern crate syn;
extern crate validator;
use std::collections::HashMap;
use proc_macro::TokenStream;
use quote::ToTokens;
use validator::{Validator};
static RANGE_TYPES: [&'static str; 24] = [
    "usize", "u8", "u16", "u32", "u64",
    "isize", "i8", "i16", "i32", "i64",
    "f32", "f64",
    "Option<usize>", "Option<8>", "Option<16>", "Option<32>", "Option<64>",
    "Option<isize>", "Option<8>", "Option<16>", "Option<32>", "Option<64>",
    "Option<32>", "Option<64>",
];
#[derive(Debug)]
struct SchemaValidation {
    function: String,
    skip_on_field_errors: bool,
}
#[proc_macro_derive(Validate, attributes(validate))]
pub fn derive_validation(input: TokenStream) -> TokenStream {
    let source = input.to_string();
    // Parse the string representation to an AST
    let ast = syn::parse_macro_input(&source).unwrap();
    let expanded = expand_validation(&ast);
    expanded.parse().unwrap()
}
fn expand_validation(ast: &syn::MacroInput) -> quote::Tokens {
    let fields = match ast.body {
        syn::Body::Struct(syn::VariantData::Struct(ref fields)) => {
            if fields.iter().any(|field| field.ident.is_none()) {
                panic!("struct has unnamed fields");
            }
            fields
        },
        _ => panic!("#[derive(Validate)] can only be used with structs"),
    };
    let mut validations = vec![];
    let field_types = find_fields_type(&fields);
    for field in fields {
        let field_ident = match field.ident {
            Some(ref i) => i,
            None => unreachable!()
        };
        let (name, validators) = find_validators_for_field(field, &field_types);
        let field_name = field_types.get(&field_ident.to_string()).unwrap();
        // Don't put a & in front a pointer
        let validator_param = if field_name.starts_with("&") {
          quote!(self.#field_ident)
        } else {
          quote!(&self.#field_ident)
        };
        // same but for the ident used in a if let block
        let optional_validator_param = quote!(#field_ident);
        // same but for the ident used in a if let Some variable
        let optional_pattern_matched = if field_name.starts_with("Option<&") {
          quote!(#field_ident)
        } else {
          quote!(ref #field_ident)
        };
        for validator in &validators {
            validations.push(match validator {
                &Validator::Length {min, max, equal} =>  {
                    // Can't interpolate None
                    let min_tokens = option_u64_to_tokens(min);
                    let max_tokens = option_u64_to_tokens(max);
                    let equal_tokens = option_u64_to_tokens(equal);
                    // wrap in if-let if we have an option
                    if field_name.starts_with("Option<") {
                        quote!(
                            if let Some(#optional_pattern_matched) = self.#field_ident {
                                if !::validator::validate_length(
                                    ::validator::Validator::Length {
                                        min: #min_tokens,
                                        max: #max_tokens,
                                        equal: #equal_tokens
                                    },
                                    #optional_validator_param
                                ) {
                                    errors.add(#name, "length");
                                }
                            }
                        )
                    } else {
                        quote!(
                            if !::validator::validate_length(
                                ::validator::Validator::Length {
                                    min: #min_tokens,
                                    max: #max_tokens,
                                    equal: #equal_tokens
                                },
                                #validator_param
                            ) {
                                errors.add(#name, "length");
                            }
                        )
                    }
                },
                &Validator::Range {min, max} => {
                    // wrap in if-let if we have an option
                    if field_name.starts_with("Option<") {
                        quote!(
                            if let Some(#field_ident) = self.#field_ident {
                                if !::validator::validate_range(
                                    ::validator::Validator::Range {min: #min, max: #max},
                                    #field_ident as f64
                                ) {
                                    errors.add(#name, "range");
                                }
                            }
                        )
                    } else {
                        quote!(
                            if !::validator::validate_range(
                                ::validator::Validator::Range {min: #min, max: #max},
                                self.#field_ident as f64
                            ) {
                                errors.add(#name, "range");
                            }
                        )
                    }
                },
                &Validator::Email => {
                    // wrap in if-let if we have an option
                    if field_name.starts_with("Option<") {
                        quote!(
                            if let Some(#optional_pattern_matched) = self.#field_ident {
                                if !::validator::validate_email(#optional_validator_param) {
                                    errors.add(#name, "email");
                                }
                            }
                        )
                    } else {
                        quote!(
                            if !::validator::validate_email(#validator_param) {
                                errors.add(#name, "email");
                            }
                        )
                    }
                }
                &Validator::Url => {
                    // wrap in if-let if we have an option
                    if field_name.starts_with("Option<") {
                        quote!(
                            if let Some(#optional_pattern_matched) = self.#field_ident {
                                if !::validator::validate_url(#optional_validator_param) {
                                    errors.add(#name, "url");
                                }
                            }
                        )
                    } else {
                        quote!(
                            if !::validator::validate_url(#validator_param) {
                                errors.add(#name, "url");
                            }
                        )
                    }
                },
                &Validator::MustMatch(ref f) => {
                    let other_ident = syn::Ident::new(f.clone());
                    quote!(
                        if !::validator::validate_must_match(&self.#field_ident, &self.#other_ident) {
                            errors.add(#name, "no_match");
                        }
                    )
                },
                &Validator::Custom(ref f) => {
                    let fn_ident = syn::Ident::new(f.clone());
                    // wrap in if-let if we have an option
                    if field_name.starts_with("Option<") {
                        quote!(
                            if let Some(#optional_pattern_matched) = self.#field_ident {
                                match #fn_ident(#optional_validator_param) {
                                    ::std::option::Option::Some(s) => {
                                        errors.add(#name, &s);
                                    },
                                    ::std::option::Option::None => (),
                                };
                            }
                        )
                    } else {
                        quote!(
                            match #fn_ident(#validator_param) {
                                ::std::option::Option::Some(s) => {
                                    errors.add(#name, &s);
                                },
                                ::std::option::Option::None => (),
                            };
                        )
                    }
                },
                &Validator::Contains(ref n) => {
                    // wrap in if-let if we have an option
                    if field_name.starts_with("Option<") {
                        quote!(
                            if let Some(#optional_pattern_matched) = self.#field_ident {
                                if !::validator::validate_contains(#optional_validator_param, &#n) {
                                    errors.add(#name, "contains");
                                }
                            }
                        )
                    } else {
                        quote!(
                            if !::validator::validate_contains(#validator_param, &#n) {
                                errors.add(#name, "contains");
                            }
                        )
                    }
                },
                &Validator::Regex(ref re) => {
                    let re_ident = syn::Ident::new(re.clone());
                    // wrap in if-let if we have an option
                    if field_name.starts_with("Option<") {
                        quote!(
                            if let Some(#optional_pattern_matched) = self.#field_ident {
                                if !#re_ident.is_match(#optional_validator_param) {
                                    errors.add(#name, "regex");
                                }
                            }
                        )
                    } else {
                        quote!(
                            if !#re_ident.is_match(#validator_param) {
                                errors.add(#name, "regex");
                            }
                        )
                    }
                },
            });
        }
    }
    let struct_validation = find_struct_validation(&ast.attrs);
    let struct_validation_tokens = match struct_validation {
        Some(s) => {
            let fn_ident = syn::Ident::new(s.function);
            if s.skip_on_field_errors {
                quote!(
                    if errors.is_empty() {
                        match #fn_ident(self) {
                            ::std::option::Option::Some((key, val)) => {
                                errors.add(&key, &val);
                            },
                            ::std::option::Option::None => (),
                        }
                    }
                )
            } else {
                quote!(
                    match #fn_ident(self) {
                        ::std::option::Option::Some((key, val)) => {
                            errors.add(&key, &val);
                        },
                        ::std::option::Option::None => (),
                    }
                )
            }
        },
        None => quote!()
    };
    let ident = &ast.ident;
    // Helper is provided for handling complex generic types correctly and effortlessly
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
    let impl_ast = quote!(
        impl #impl_generics Validate for #ident #ty_generics #where_clause {
            fn validate(&self) -> ::std::result::Result<(), ::validator::Errors> {
                let mut errors = ::validator::Errors::new();
                #(#validations)*
                #struct_validation_tokens
                if errors.is_empty() {
                    ::std::result::Result::Ok(())
                } else {
                    ::std::result::Result::Err(errors)
                }
            }
        }
    );
    // println!("{}", impl_ast.to_string());
    impl_ast
}
/// Find if a struct has some schema validation and returns the info if so
fn find_struct_validation(struct_attrs: &Vec<syn::Attribute>) -> Option<SchemaValidation> {
    let error = |msg: &str| -> ! {
        panic!("Invalid schema level validation: {}", msg);
    };
    for attr in struct_attrs {
        match attr.value {
            syn::MetaItem::List(ref ident, ref meta_items) => {
                if ident != "validate" {
                    continue;
                }
                match meta_items[0] {
                    syn::NestedMetaItem::MetaItem(ref item) => match item {
                        &syn::MetaItem::List(ref ident2, ref args) => {
                            if ident2 != "schema" {
                                error("Only `schema` is allowed as validator on a struct")
                            }
                            let mut function = "".to_string();
                            let mut skip_on_field_errors = true;
                            for arg in args {
                                match *arg {
                                    syn::NestedMetaItem::MetaItem(ref item) => match *item {
                                        syn::MetaItem::NameValue(ref name, ref val) => {
                                            match name.to_string().as_ref() {
                                                "function" => {
                                                    function = match lit_to_string(val) {
                                                        Some(s) => s,
                                                        None => error("invalid argument type for `function` \
                                                        : only a string is allowed"),
                                                    };
                                                },
                                                "skip_on_field_errors" => {
                                                    skip_on_field_errors = match lit_to_bool(val) {
                                                        Some(s) => s,
                                                        None => error("invalid argument type for `skip_on_field_errors` \
                                                        : only a bool is allowed"),
                                                    };
                                                },
                                                _ => error("Unknown argument")
                                            }
                                        },
                                        _ => error("Unexpected args")
                                    },
                                    _ => error("Unexpected args")
                                }
                            }
                            if function == "" {
                                error("`function` is required");
                            }
                            return Some(SchemaValidation {
                                function: function,
                                skip_on_field_errors: skip_on_field_errors
                            });
                        },
                        _ => error("Unexpected struct validator")
                    },
                    _ => error("Unexpected struct validator")
                }
            },
            _ => error("Unexpected struct validator")
        }
    }
    None
}
// Find all the types (as string) for each field of the struct
// Needed for the `must_match` filter
fn find_fields_type(fields: &Vec<syn::Field>) -> HashMap<String, String> {
    let mut types = HashMap::new();
    for field in fields {
        let field_name = match field.ident {
            Some(ref s) => s.to_string(),
            None => unreachable!(),
        };
        let field_type = match field.ty {
            syn::Ty::Path(_, ref p) => {
                let mut tokens = quote::Tokens::new();
                p.to_tokens(&mut tokens);
                tokens.to_string().replace(' ', "")
            },
            syn::Ty::Rptr(ref l, ref p) => {
                let mut tokens = quote::Tokens::new();
                p.ty.to_tokens(&mut tokens);
                let mut name = tokens.to_string().replace(' ', "");
                if l.is_some() {
                    name.insert(0, '&')
                }
                name
            },
            _ => panic!("Type `{:?}` of field `{}` not supported", field.ty, field_name)
        };
        //println!("{:?}", field_type);
        types.insert(field_name, field_type);
    }
    types
}
/// Find everything we need to know about a Field.
fn find_validators_for_field(field: &syn::Field, field_types: &HashMap<String, String>) -> (String, Vec<Validator>) {
    let mut field_name = match field.ident {
        Some(ref s) => s.to_string(),
        None => unreachable!(),
    };
    let error = |msg: &str| -> ! {
        panic!("Invalid attribute #[validate] on field `{}`: {}", field.ident.clone().unwrap().to_string(), msg);
    };
    let field_type = field_types.get(&field_name).unwrap();
    let mut validators = vec![];
    let mut has_validate = false;
    let find_struct_validator = |name: String, meta_items: &Vec<syn::NestedMetaItem>| -> Validator {
        match name.as_ref() {
            "length" => {
                let mut min = None;
                let mut max = None;
                let mut equal = None;
                for meta_item in meta_items {
                    match *meta_item {
                        syn::NestedMetaItem::MetaItem(ref item) => match *item {
                            syn::MetaItem::NameValue(ref name, ref val) => {
                                match name.to_string().as_ref() {
                                    "min" => {
                                        min = match lit_to_int(val) {
                                            Some(s) => Some(s),
                                            None => error("invalid argument type for `min` of `length` validator: only integers are allowed"),
                                        };
                                    },
                                    "max" => {
                                        max = match lit_to_int(val) {
                                            Some(s) => Some(s),
                                            None => error("invalid argument type for `max` of `length` validator: only integers are allowed"),
                                        };
                                    },
                                    "equal" => {
                                        equal = match lit_to_int(val) {
                                            Some(s) => Some(s),
                                            None => error("invalid argument type for `equal` of `length` validator: only integers are allowed"),
                                        };
                                    },
                                    _ => error(&format!(
                                        "unknown argument `{}` for validator `length` (it only has `min`, `max`, `equal`)",
                                        name.to_string()
                                    ))
                                }
                            },
                            _ => panic!("unexpected item {:?} while parsing `length` validator", item)
                        },
                        _=> unreachable!()
                    }
                }
                if equal.is_some() && (min.is_some() || max.is_some()) {
                    error("both `equal` and `min` or `max` have been set in `length` validator: probably a mistake");
                }
                Validator::Length { min: min, max: max, equal: equal }
            },
            "range" => {
                let mut min = 0.0;
                let mut max = 0.0;
                for meta_item in meta_items {
                    match *meta_item {
                        syn::NestedMetaItem::MetaItem(ref item) => match *item {
                            syn::MetaItem::NameValue(ref name, ref val) => {
                                match name.to_string().as_ref() {
                                    "min" => {
                                        min = match lit_to_float(val) {
                                            Some(s) => s,
                                            None => error("invalid argument type for `min` of `range` validator: only integers are allowed")
                                        };
                                    },
                                    "max" => {
                                        max = match lit_to_float(val) {
                                            Some(s) => s,
                                            None => error("invalid argument type for `max` of `range` validator: only integers are allowed")
                                        };
                                    },
                                    _ => error(&format!(
                                        "unknown argument `{}` for validator `range` (it only has `min`, `max`)",
                                        name.to_string()
                                    ))
                                }
                            },
                            _ => panic!("unexpected item {:?} while parsing `range` validator", item)
                        },
                        _=> unreachable!()
                    }
                }
                Validator::Range { min: min, max: max}
            }
            _ => panic!("unexpected list validator: {:?}", name)
        }
    };
    for attr in &field.attrs {
        if attr.name() != "validate" && attr.name() != "serde" {
            continue;
        }
        if attr.name() == "validate" {
            has_validate = true;
        }
        match attr.value {
            syn::MetaItem::List(_, ref meta_items) => {
                if attr.name() == "serde" {
                    match find_original_field_name(meta_items) {
                        Some(s) => { field_name = s },
                        None => ()
                    };
                    continue;
                }
                // only validation from there on
                for meta_item in meta_items {
                    match *meta_item {
                        syn::NestedMetaItem::MetaItem(ref item) => match *item {
                            // email, url
                            syn::MetaItem::Word(ref name) => match name.to_string().as_ref() {
                                "email" => {
                                    if field_type != "String"
                                        && field_type != "&str"
                                        && field_type != "Option<String>"
                                        && !(field_type.starts_with("Option<") && field_type.ends_with("str>")) {
                                        panic!("`email` validator can only be used on String or &str");
                                    }
                                    validators.push(Validator::Email);
                                },
                                "url" => {
                                    if field_type != "String"
                                        && field_type != "&str"
                                        && field_type != "Option<String>"
                                        && !(field_type.starts_with("Option<") && field_type.ends_with("str>")) {
                                        panic!("`url` validator can only be used on String or &str");
                                    }
                                    validators.push(Validator::Url);
                                },
                                _ => panic!("Unexpected word validator: {}", name)
                            },
                            // custom, contains, must_match
                            syn::MetaItem::NameValue(ref name, ref val) => {
                                match name.to_string().as_ref() {
                                    "custom" => {
                                        match lit_to_string(val) {
                                            Some(s) => validators.push(Validator::Custom(s)),
                                            None => error("invalid argument for `custom` validator: only strings are allowed"),
                                        };
                                    },
                                    "contains" => {
                                        match lit_to_string(val) {
                                            Some(s) => validators.push(Validator::Contains(s)),
                                            None => error("invalid argument for `contains` validator: only strings are allowed"),
                                        };
                                    },
                                    "regex" => {
                                        match lit_to_string(val) {
                                            Some(s) => validators.push(Validator::Regex(s)),
                                            None => error("invalid argument for `regex` validator: only strings are allowed"),
                                        };
                                    }
                                    "must_match" => {
                                        match lit_to_string(val) {
                                            Some(s) => {
                                                if let Some(t2) = field_types.get(&s) {
                                                    if field_type == t2 {
                                                        validators.push(Validator::MustMatch(s));
                                                    } else {
                                                        error("invalid argument for `must_match` validator: types of field can't match");
                                                    }
                                                } else {
                                                    error("invalid argument for `must_match` validator: field doesn't exist in struct");
                                                }
                                            },
                                            None => error("invalid argument for `must_match` validator: only strings are allowed"),
                                        };
                                    },
                                    _ => panic!("unexpected name value validator: {:?}", name),
                                };
                            },
                            // validators with args: length for example
                            syn::MetaItem::List(ref name, ref meta_items) => {
                                // Some sanity checking first
                                if name == "length" {
                                    if field_type != "String"
                                        && !field_type.starts_with("Vec<")
                                        && !field_type.starts_with("Option<Vec<")
                                        && field_type != "Option<String>"
                                        // a bit ugly
                                        && !(field_type.starts_with("Option<") && field_type.ends_with("str>"))
                                        && field_type != "&str" {
                                        error(&format!(
                                            "Validator `length` can only be used on types `String`, `&str` or `Vec` but found `{}`",
                                            field_type
                                        ));
                                    }
                                    if meta_items.len() == 0 {
                                        error("Validator `length` requires at least 1 argument out of `min`, `max` and `equal`");
                                    }
                                }
                                if name == "range" {
                                    if !RANGE_TYPES.contains(&field_type.as_ref()) {
                                        error(&format!(
                                            "Validator `range` can only be used on number types but found `{}`",
                                            field_type
                                        ));
                                    }
                                    if meta_items.len() != 2 {
                                        error("Validator `range` requires 2 arguments: `min` and `max`");
                                    }
                                }
                                validators.push(find_struct_validator(name.to_string(), meta_items));
                            },
                        },
                        _ => unreachable!("Found a non MetaItem while looking for validators")
                    };
                }
            },
            _ => unreachable!("Got something other than a list of attributes while checking field `{}`", field_name),
        }
    }
    if has_validate && validators.is_empty() {
        error("it needs at least one validator");
    }
    (field_name, validators)
}
/// Serde can be used to rename fields on deserialization but most of the times
/// we want the error on the original field.
///
/// For example a JS frontend might send camelCase fields and Rust converts them to snake_case
/// but we want to send the errors back to the frontend with the original name
fn find_original_field_name(meta_items: &Vec<syn::NestedMetaItem>) -> Option<String> {
    let mut original_name = None;
    for meta_item in meta_items {
        match *meta_item {
            syn::NestedMetaItem::MetaItem(ref item) => match *item {
                syn::MetaItem::Word(_) => continue,
                syn::MetaItem::NameValue(ref name, ref val) => {
                    if name == "rename" {
                        original_name = Some(lit_to_string(val).unwrap());
                    }
                },
                // length
                syn::MetaItem::List(_, ref meta_items) => {
                    return find_original_field_name(meta_items);
                }
            },
            _ => unreachable!()
        };
        if original_name.is_some() {
            return original_name;
        }
    }
    original_name
}
fn lit_to_string(lit: &syn::Lit) -> Option<String> {
    match *lit {
        syn::Lit::Str(ref s, _) => Some(s.to_string()),
        _ => None,
    }
}
fn lit_to_int(lit: &syn::Lit) -> Option<u64> {
    match *lit {
        syn::Lit::Int(ref s, _) => Some(*s),
        // TODO: remove when attr_literals is stable
        syn::Lit::Str(ref s, _) => Some(s.parse::<u64>().unwrap()),
        _ => None,
    }
}
fn lit_to_float(lit: &syn::Lit) -> Option<f64> {
    match *lit {
        syn::Lit::Float(ref s, _) => Some(s.parse::<f64>().unwrap()),
        syn::Lit::Int(ref s, _) => Some(*s as f64),
        // TODO: remove when attr_literals is stable
        syn::Lit::Str(ref s, _) => Some(s.parse::<f64>().unwrap()),
        _ => None,
    }
}
fn lit_to_bool(lit: &syn::Lit) -> Option<bool> {
    match *lit {
        syn::Lit::Bool(ref s) => Some(*s),
        // TODO: remove when attr_literals is stable
        syn::Lit::Str(ref s, _) => if s == "true" { Some(true) } else { Some(false) },
        _ => None,
    }
}
fn option_u64_to_tokens(opt: Option<u64>) -> quote::Tokens {
    let mut tokens = quote::Tokens::new();
    tokens.append("::");
    tokens.append("std");
    tokens.append("::");
    tokens.append("option");
    tokens.append("::");
    tokens.append("Option");
    tokens.append("::");
    match opt {
        Some(ref t) => {
            tokens.append("Some");
            tokens.append("(");
            t.to_tokens(&mut tokens);
            tokens.append(")");
        }
        None => {
            tokens.append("None");
        }
    }
    tokens
}
 |