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
use std::{collections::HashMap, mem};

use darling::{export::NestedMeta, FromMeta, ToTokens};
use proc_macro2::Ident;
use replace_types::{ReplaceTypes, VisitMut};
use syn::{parse_macro_input, Attribute, ItemImpl, Path, TypePath};

fn parse_substitutions(
    nested: impl AsRef<[NestedMeta]>,
) -> darling::Result<HashMap<TypePath, TypePath>> {
    let substitutions = HashMap::<Ident, TypePath>::from_list(nested.as_ref())?;

    let substitutions: HashMap<TypePath, TypePath> = substitutions
        .into_iter()
        .map(|(ident, type_path)| {
            (
                TypePath {
                    qself: None,
                    path: Path::from(ident),
                },
                type_path,
            )
        })
        .collect();

    Ok(substitutions)
}

/// Repeat an implementation with type substitutions
///
/// ## Example
///
/// ```
/// pub trait IntoBytes {
///     fn into_bytes(self) -> Vec<u8>;
/// }
///
/// #[impl_for(T = "i8")]
/// #[impl_for(T = "u8")]
/// #[impl_for(T = "i16")]
/// #[impl_for(T = "u16")]
/// #[impl_for(T = "i32")]
/// #[impl_for(T = "u32")]
/// #[impl_for(T = "i64")]
/// #[impl_for(T = "u64")]
/// #[impl_for(T = "isize")]
/// #[impl_for(T = "usize")]
/// impl IntoBytes for T {
///     fn into_bytes(self) -> Vec<u8> {
///         let mut buf = ::itoa::Buffer::new();
///         let s = buf.format(self);
///         s.as_bytes().to_vec()
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn impl_for(
    args: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let mut input = parse_macro_input!(input as ItemImpl);

    let mut errors: Vec<darling::Error> = Vec::new();
    let mut substitutions_list: Vec<HashMap<TypePath, TypePath>> = Vec::new();

    match NestedMeta::parse_meta_list(args.into())
        .map_err(darling::Error::from)
        .and_then(parse_substitutions)
    {
        Ok(substitutions) => {
            substitutions_list.push(substitutions);
        }
        Err(err) => {
            errors.push(err);
        }
    }

    let mut attrs: Vec<Attribute> = Vec::new();

    let input_attrs = mem::take(&mut input.attrs);

    for attr in input_attrs.into_iter() {
        if attr.path().is_ident("impl_for") {
            match attr.meta.require_list() {
                Ok(list) => {
                    match NestedMeta::parse_meta_list(list.tokens.to_owned())
                        .map_err(darling::Error::from)
                        .and_then(parse_substitutions)
                    {
                        Ok(substitutions) => {
                            substitutions_list.push(substitutions);
                        }
                        Err(err) => {
                            errors.push(err);
                        }
                    }
                }
                Err(err) => {
                    errors.push(err.into());
                }
            }
        } else {
            attrs.push(attr);
        }
    }

    if !errors.is_empty() {
        return darling::Error::multiple(errors).write_errors().into();
    }

    input.attrs = attrs;

    substitutions_list
        .into_iter()
        .map(|substitutions| {
            let mut item_impl = input.clone();
            ReplaceTypes::new(substitutions).visit_item_impl_mut(&mut item_impl);
            proc_macro::TokenStream::from(item_impl.into_token_stream())
        })
        .collect::<proc_macro::TokenStream>()
}