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
//! A custom-derive implementation for the `FormatArgs` trait.
#![recursion_limit="128"]

extern crate proc_macro;
extern crate syn;
#[macro_use] extern crate quote;

use proc_macro::TokenStream;

/// Derive a `FormatArgs` implementation for the provided input struct.
#[proc_macro_derive(FormatArgs)]
pub fn derive_format_args(input: TokenStream) -> TokenStream {
    let string = input.to_string();
    let ast = syn::parse_derive_input(&string).unwrap();
    implement(&ast).parse().unwrap()
}

fn implement(ast: &syn::DeriveInput) -> quote::Tokens {
    // The rough structure of this (dummy_ident, extern crate/use) is based on
    // how serde_derive does it.

    let ident = &ast.ident;
    let variant = match ast.body {
        syn::Body::Struct(ref variant) => variant,
        _ => panic!("#[derive(FormatArgs)] is not implemented for enums")
    };

    let dummy_ident = syn::Ident::new(format!("_IMPL_FORMAT_ARGS_FOR_{}", ident));

    let (validate_name, validate_index, get_child, as_usize);
    match *variant {
        syn::VariantData::Struct(ref fields) => {
            get_child = build_fields(fields);
            as_usize = build_usize(ast, fields);
            validate_index = quote! { false };

            let index = 0..fields.len();
            let ident: Vec<_> = fields.iter()
                .map(|field| field.ident.as_ref().unwrap())
                .map(ToString::to_string)
                .collect();
            validate_name = quote! {
                match name {
                    #(#ident => _Option::Some(#index),)*
                    _ => _Option::None,
                }
            };
        }
        syn::VariantData::Tuple(ref fields) => {
            get_child = build_fields(fields);
            as_usize = build_usize(ast, fields);
            validate_name = quote! { _Option::None };

            let len = fields.len();
            validate_index = quote! { index < #len };
        }
        syn::VariantData::Unit => {
            validate_name = quote! { _Option::None };
            validate_index = quote! { false };
            get_child = quote! { panic!("bad index {}", index) };
            as_usize = get_child.clone();
        }
    };

    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
    quote! {
        #[allow(non_upper_case_globals, unused_attributes)]
        #[allow(unused_variables, unused_qualifications)]
        const #dummy_ident: () = {
            extern crate runtime_fmt as _runtime_fmt;
            use std::fmt::{Formatter as _Formatter, Result as _Result};
            use std::option::Option as _Option;
            #[automatically_derived]
            impl #impl_generics _runtime_fmt::FormatArgs for #ident #ty_generics #where_clause {
                fn validate_name(name: &str) -> _Option<usize> {
                    #validate_name
                }
                fn validate_index(index: usize) -> bool {
                    #validate_index
                }
                fn get_child<__F>(index: usize) -> _Option<fn(&Self, &mut _Formatter) -> _Result>
                    where __F: _runtime_fmt::codegen::FormatTrait + ?Sized
                {
                    #get_child
                }
                fn as_usize(index: usize) -> Option<fn(&Self) -> &usize> {
                    #as_usize
                }
            }
        };
    }
}

fn build_fields(fields: &[syn::Field]) -> quote::Tokens {
    let index = 0..fields.len();
    let ty: Vec<_> = fields.iter().map(|field| &field.ty).collect();
    let ident: Vec<_> = fields.iter().enumerate().map(|(idx, field)| match field.ident {
        Some(ref ident) => ident.clone(),
        None => syn::Ident::from(idx),
    }).collect();
    quote! {
        match index {
            #(
                #index => _runtime_fmt::codegen::combine::<__F, Self, #ty, _>(
                    |this| &this.#ident
                ),
            )*
            _ => panic!("bad index {}", index)
        }
    }
}

fn build_usize(ast: &syn::DeriveInput, fields: &[syn::Field]) -> quote::Tokens {
    let self_ = &ast.ident;
    let (_, ty_generics, where_clause) = ast.generics.split_for_impl();

    // To avoid causing trouble with lifetime elision rules, an explicit
    // lifetime for the input and output is used.
    let lifetime = syn::Ident::new("'__as_usize_inner");
    let mut generics2 = ast.generics.clone();
    generics2.lifetimes.insert(0, syn::LifetimeDef {
        attrs: vec![],
        lifetime: syn::Lifetime { ident: lifetime.clone() },
        bounds: vec![],
    });
    let (impl_generics, _, _) = generics2.split_for_impl();

    let mut result = quote::Tokens::new();
    for (idx, field) in fields.iter().enumerate() {
        let ident = match field.ident {
            Some(ref ident) => ident.clone(),
            None => syn::Ident::from(idx),
        };
        let ty = &field.ty;
        result.append(quote! {
            #idx => {
                fn inner #impl_generics (this: &#lifetime #self_ #ty_generics)
                    -> &#lifetime #ty
                    #where_clause { &this.#ident }
                _runtime_fmt::codegen::as_usize(inner)
            },
        });
    }

    quote! {
        match index {
            #result
            _ => panic!("bad index {}", index)
        }
    }
}