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
#![feature(try_from)]

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

extern crate kg_utils;
extern crate kg_display;

use proc_macro2::{Ident, Span};
use std::collections::HashMap;

use kg_display::fmt::*;
use kg_utils::collections::SparseSet;


decl_derive!([Display, attributes(display)] => display_derive);


fn display_derive(mut s: synstructure::Structure) -> proc_macro2::TokenStream {
    fn err_msg<S: Into<String>>(msg: S, v: &synstructure::VariantInfo) -> String {
        use std::fmt::Write;
        let mut msg = msg.into();
        if let Some(p) = v.prefix {
            write!(msg, " for {}::{}", p, v.ast().ident).expect("write failed")
        } else {
            write!(msg, " for {}", v.ast().ident).expect("write failed")
        }
        msg
    }

    let mut fmts = Vec::with_capacity(s.variants().len());

    for ref mut v in s.variants_mut() {
        v.binding_name(|field, i| {
            field.ident.clone().unwrap_or(Ident::new(&format!("a{}", i), Span::call_site()))
        });

        let disp = match find_display_attr(v.ast().attrs) {
            Some(m) => m,
            None => {
                panic!(err_msg("missing display(...) attribute", v));
            }
        };

        if disp.is_empty() {
            panic!(err_msg("empty display(...) attribute", v));
        }

        let params: Vec<(String, String)> = {
            let mut params = HashMap::new();
            for p in disp.iter() {
                if let &syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { ref ident, lit: syn::Lit::Str(ref s), .. })) = p {
                    if ident != "fmt" && ident != "alt" {
                        params.insert(ident.to_string(), s.value());
                    }
                }
            }
            params.into_iter().collect()
        };

        let fmt = match disp.get(0).unwrap() {
            &syn::NestedMeta::Literal(syn::Lit::Str(ref s)) => s.value(),
            &syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { ref ident, lit: syn::Lit::Str(ref s), .. })) if ident == "fmt" => s.value(),
            _ => panic!(err_msg("invalid display(...) attribute format", v)),
        };

        let mut bindings_set: SparseSet<usize> = SparseSet::new(v.bindings().len());
        let mut params_set: SparseSet<usize> = SparseSet::new(params.len());

        let fmt_str = FormatString::parse(&fmt).expect(&err_msg("invalid format string", v));
        fmt_str.each_argument(|arg| {
            match *arg {
                Argument::Next => panic!(err_msg("default positional argument found, only named arguments are supported", v)),
                Argument::Index(_) => panic!(err_msg("positional argument found, only named arguments are supported", v)),
                Argument::Name(ref name) => {
                    if let Some((i, _)) = params.iter().enumerate().find(|(_, p)| &p.0 == name) {
                        params_set.insert(i);
                    } else if let Some((i, _)) = v.bindings().iter().enumerate().find(|(_, bi)| bi.binding == name) {
                        bindings_set.insert(i);
                    } else {
                        panic!(err_msg(format!("unknown argument '{}'", name), v));
                    }
                },
            }
            true
        });

        let alt = {
            let fmt = if let Some(fmt) = disp.get(1) {
                match fmt {
                    &syn::NestedMeta::Literal(syn::Lit::Str(ref s)) => Some(s.value()),
                    &syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { ref ident, lit: syn::Lit::Str(ref s), .. })) if ident == "alt" => Some(s.value()),
                    _ => None,
                }
            } else {
                None
            };

            if let Some(fmt) = fmt {
                let mut bindings_set: SparseSet<usize> = SparseSet::new(v.bindings().len());
                let mut params_set: SparseSet<usize> = SparseSet::new(params.len());

                let fmt_str = FormatString::parse(&fmt).expect(&err_msg("invalid format string", v));
                fmt_str.each_argument(|arg| {
                    match *arg {
                        Argument::Next => panic!(err_msg("default positional argument found, only named arguments are supported", v)),
                        Argument::Index(_) => panic!(err_msg("positional argument found, only named arguments are supported", v)),
                        Argument::Name(ref name) => {
                            if let Some((i, _)) = params.iter().enumerate().find(|(_, p)| &p.0 == name) {
                                params_set.insert(i);
                            } else if let Some((i, _)) = v.bindings().iter().enumerate().find(|(_, bi)| bi.binding == name) {
                                bindings_set.insert(i);
                            } else {
                                panic!(err_msg(format!("unknown argument '{}'", name), v));
                            }
                        },
                    }
                    true
                });
                Some((fmt, fmt_str, bindings_set, params_set))
            } else {
                None
            }
        };

        fmts.push((params, (fmt, fmt_str, bindings_set, params_set), alt));
    }

    let mut fmt_it = fmts.into_iter();
    let display_body = s.each_variant(|v| {
        let fmt_ = fmt_it.next().unwrap();
        let params = fmt_.0;
        let (fmt, _, bset, pset) = fmt_.1;
        if let Some((fmt_alt, _, bset_alt, pset_alt)) = fmt_.2 {
            let args = v.bindings().iter().enumerate().filter_map(|(index, bi)| {
                if bset.contains(&index) {
                    let ref id = bi.binding;
                    Some(quote! { #id = #id })
                } else {
                    None
                }
            });

            let p_args = params.iter().enumerate().filter_map(|(index, p)| {
                if pset.contains(&index) {
                    let id = Ident::new(&p.0, Span::call_site());
                    let value: syn::Expr = syn::parse_str(&p.1).expect(&format!("cannot parse expression: `{}` for parameter '{}'", &p.1, &p.0));
                    Some(quote! { #id = #value })
                } else {
                    None
                }
            });

            let args_alt = v.bindings().iter().enumerate().filter_map(|(index, bi)| {
                if bset_alt.contains(&index) {
                    let ref id = bi.binding;
                    Some(quote! { #id = #id })
                } else {
                    None
                }
            });

            let p_args_alt = params.iter().enumerate().filter_map(|(index, p)| {
                if pset_alt.contains(&index) {
                    let id = Ident::new(&p.0, Span::call_site());
                    let value: syn::Expr = syn::parse_str(&p.1).expect(&format!("cannot parse expression: `{}` for parameter '{}'", &p.1, &p.0));
                    Some(quote! { #id = #value })
                } else {
                    None
                }
            });

            quote! {
                if f__.alternate() {
                    write!(f__, #fmt_alt #(, #args_alt)* #(, #p_args_alt)*)
                } else {
                    write!(f__, #fmt #(, #args)* #(, #p_args)*)
                }
            }
        } else {
            let args = v.bindings().iter().enumerate().filter_map(|(index, bi)| {
                if bset.contains(&index) {
                    let ref id = bi.binding;
                    Some(quote! { #id = #id })
                } else {
                    None
                }
            });

            let p_args = params.iter().enumerate().filter_map(|(index, p)| {
                if pset.contains(&index) {
                    let id = Ident::new(&p.0, Span::call_site());
                    let value: syn::Expr = syn::parse_str(&p.1).expect(&format!("cannot parse expression: `{}` for parameter '{}'", &p.1, &p.0));
                    Some(quote! { #id = #value })
                } else {
                    None
                }
            });

            quote! {
                write!(f__, #fmt #(, #args)* #(, #p_args)*)
            }
        }
    });

    let p = s.gen_impl(quote! {
        extern crate std;

        gen impl std::fmt::Display for @Self {
            #[allow(unused_variables)]
            fn fmt(&self, f__: &mut std::fmt::Formatter) -> std::fmt::Result {
                match *self {
                    #display_body
                }
            }
        }
    });

    p
}


fn find_display_attr(attrs: &[syn::Attribute]) -> Option<Vec<syn::NestedMeta>> {
    let doc_path: syn::Path = syn::Ident::new("doc", Span::call_site()).into();

    let mut disp = None;
    for attr in attrs {
        if attr.path != doc_path && attr.style == syn::AttrStyle::Outer {
            let disp_meta = {
                let m = attr.interpret_meta();
                if let Some(syn::Meta::List(syn::MetaList { ident, nested, .. })) = m {
                    if ident == "display" {
                        Some(nested.into_iter().collect())
                    } else {
                        None
                    }
                } else {
                    None
                }
            };
            if disp_meta.is_some() & &disp.is_some() {
                panic!("multiple display(...) attributes found")
            } else {
                disp = disp_meta;
            }
        }
    }
    disp
}