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
//! # Example
//!
//! `_binded_a`, `_binded_b` and `_binded_0` in below example are
//! `BindedField`.
//!
//! ```rust
//! struct S {
//!     a: u8,
//!     b: u16,
//! }
//! let s = S { a: 0, b: 0 };
//! match s {
//!     S {
//!         a: _binded_a,
//!         b: _binded_b,
//!     } => {}
//! }
//! enum E {
//!     V1 { a: u8 },
//!     V2(u16),
//!     V3,
//! }
//! let e = E::V1 { a: 0 };
//! match e {
//!     E::V1 { a: _binded_a } => {}
//!     E::V2(_binded_0) => {}
//!     E::V3 => {}
//! }
//! ```
//!
//!
//! -----
//!
//! Adopted from `synstructure`.
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::{
    punctuated::Pair,
    token::{Mut, Ref},
    *,
};

use crate::{def_site, is_attr_name, syn_ext::PairExt};

/// Used to bind whole struct or enum.
#[derive(Debug, Clone)]
pub struct Binder<'a> {
    ident: &'a Ident,
    body: &'a Data,
    attrs: &'a [Attribute],
}

impl<'a> Binder<'a> {
    ///
    /// - `attrs`: Attributes of the type.
    pub const fn new(ident: &'a Ident, body: &'a Data, attrs: &'a [Attribute]) -> Self {
        Binder { ident, body, attrs }
    }

    pub fn new_from(input: &'a DeriveInput) -> Self {
        Self::new(&input.ident, &input.data, &input.attrs)
    }

    pub fn variants(&self) -> Vec<VariantBinder<'a>> {
        match *self.body {
            Data::Enum(DataEnum { ref variants, .. }) => {
                let enum_name = &self.ident;
                variants
                    .iter()
                    .map(|v| VariantBinder::new(Some(enum_name), &v.ident, &v.fields, &v.attrs))
                    .collect()
            }
            Data::Struct(DataStruct { ref fields, .. }) => {
                vec![VariantBinder::new(None, self.ident, fields, self.attrs)]
            }
            Data::Union(_) => unimplemented!("Binder for union type"),
        }
    }
}

/// Variant.
#[derive(Debug, Clone)]
pub struct VariantBinder<'a> {
    /// None for struct.
    enum_name: Option<&'a Ident>,
    /// Name of variant.
    name: &'a Ident,
    data: &'a Fields,
    attrs: &'a [Attribute],
}

impl<'a> VariantBinder<'a> {
    pub const fn new(
        enum_name: Option<&'a Ident>,
        name: &'a Ident,
        data: &'a Fields,
        attrs: &'a [Attribute],
    ) -> Self {
        VariantBinder {
            enum_name,
            name,
            data,
            attrs,
        }
    }

    pub const fn variant_name(&self) -> &Ident {
        self.name
    }

    pub const fn data(&self) -> &Fields {
        self.data
    }

    pub const fn attrs(&self) -> &[Attribute] {
        self.attrs
    }

    /// `EnumName::VariantName` for enum, and `StructName` for struct.
    pub fn qual_path(&self) -> Path {
        match self.enum_name {
            Some(enum_name) => {
                let vn = &self.name;

                parse_quote!(#enum_name::#vn)
            }
            None => self.name.clone().into(),
        }
    }

    ///
    ///  - `prefix`: prefix of field binding.
    pub fn bind(
        &self,
        prefix: &str,
        by_ref: Option<Ref>,
        mutability: Option<Mut>,
    ) -> (Pat, Vec<BindedField<'a>>) {
        let path = self.qual_path();

        let (pat, bindings) = match *self.data {
            Fields::Unit => {
                // EnumName::VariantName
                let pat = Pat::Path(PatPath {
                    qself: None,
                    path,
                    attrs: Default::default(),
                });

                // Unit struct does not have any field to bind
                (pat, vec![])
            }
            Fields::Named(FieldsNamed {
                named: ref fields,
                brace_token,
            }) => {
                let mut bindings = vec![];

                let fields = fields
                    .pairs()
                    .map(|e| {
                        let (t, p) = e.into_tuple();
                        Pair::new(t, p.cloned())
                    })
                    .enumerate()
                    .map(|(idx, f)| {
                        f.map_item(|f| {
                            let ident = f
                                .ident
                                .clone()
                                .expect("field of struct-like variants should have name");

                            let binded_ident =
                                Ident::new(&format!("{}{}", prefix, ident), ident.span());
                            bindings.push(BindedField {
                                idx,
                                binded_ident: binded_ident.clone(),
                                field: f,
                            });
                            FieldPat {
                                attrs: f
                                    .attrs
                                    .iter()
                                    .filter(|attr| is_attr_name(attr, "cfg"))
                                    .cloned()
                                    .collect(),
                                colon_token: f.colon_token,
                                member: Member::Named(ident),
                                pat: Box::new(Pat::Ident(PatIdent {
                                    by_ref,
                                    mutability,
                                    ident: binded_ident,
                                    subpat: None,
                                    attrs: Default::default(),
                                })),
                            }
                        })
                    })
                    .collect();
                // EnumName::VariantName { fields }
                let pat = Pat::Struct(PatStruct {
                    attrs: Default::default(),
                    qself: None,
                    path,
                    brace_token,
                    fields,
                    rest: None,
                });
                (pat, bindings)
            }
            Fields::Unnamed(FieldsUnnamed {
                unnamed: ref fields,
                paren_token,
            }) => {
                // TODO
                let mut bindings = vec![];

                let pats = fields
                    .pairs()
                    .map(|e| {
                        let (t, p) = e.into_tuple();
                        Pair::new(t, p.cloned())
                    })
                    .enumerate()
                    .map(|(idx, f)| {
                        f.map_item(|f| {
                            let binded_ident =
                                Ident::new(&format!("{}{}", prefix, idx), def_site());

                            bindings.push(BindedField {
                                idx,
                                binded_ident: binded_ident.clone(),
                                field: f,
                            });

                            Pat::Ident(PatIdent {
                                by_ref,
                                mutability,
                                ident: binded_ident,
                                subpat: None,
                                attrs: Default::default(),
                            })
                        })
                    })
                    .collect();
                // EnumName::VariantName ( fields )
                let pat = Pat::TupleStruct(PatTupleStruct {
                    attrs: Default::default(),
                    qself: None,
                    path,
                    paren_token,
                    elems: pats,
                });
                (pat, bindings)
            }
        };

        // if we don't need to move fields, we should match on reference to make tuple
        // work.

        // let pat = match by_ref {
        //     Some(ref_token) => Pat::Ref(PatRef {
        //         pat: box pat,
        //         and_token: ref_token.0.as_token(),
        //         mutability,
        //     }),
        //     None => pat,
        // };

        (pat, bindings)
    }
}

/// Binded field. Note that this struct acts like a binded variable for
/// `quote!`.
#[derive(Debug, Clone)]
pub struct BindedField<'a> {
    binded_ident: Ident,
    idx: usize,
    field: &'a Field,
}

impl<'a> BindedField<'a> {
    pub const fn idx(&self) -> usize {
        self.idx
    }

    /// Name of field binding.
    pub const fn name(&self) -> &Ident {
        &self.binded_ident
    }

    pub const fn field(&self) -> &Field {
        self.field
    }
}

impl<'a> ToTokens for BindedField<'a> {
    fn to_tokens(&self, t: &mut TokenStream) {
        self.binded_ident.to_tokens(t)
    }
}