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
use crate::expand::ExternCrates;
use proc_macro2::TokenStream;
use quote::quote;
use std::collections::BTreeMap;

/// Converts the given value into tokens that represent itself.
pub(crate) fn verbatim<T: Verbatim>(t: &T, crates: &ExternCrates) -> TokenStream {
    let mut s = TokenStream::new();
    t.to_verbatim_tokens(&mut s, crates);
    s
}

/// Conversion to tokens that represent the value itself.
pub(crate) trait Verbatim {
    /// Converts `self` into tokens that represent itself.
    fn to_verbatim_tokens(&self, s: &mut TokenStream, crates: &ExternCrates);
}

/// Provides a [`quote::ToTokens`] implementations for references of values that implement
/// [`Verbatim`].
pub(crate) struct ToTokensCompat<'a, T: ?Sized + Verbatim>(pub(crate) &'a T, &'a ExternCrates);

impl<T: Verbatim> quote::ToTokens for ToTokensCompat<'_, T> {
    #[inline]
    fn to_tokens(&self, tokens: &mut TokenStream) {
        self.0.to_verbatim_tokens(tokens, self.1)
    }
}

impl Verbatim for String {
    fn to_verbatim_tokens(&self, tokens: &mut TokenStream, crates: &ExternCrates) {
        let alloy_sol_types = &crates.sol_types;
        tokens.extend(if self.is_empty() {
            quote!(#alloy_sol_types::private::String::new())
        } else {
            quote!(#alloy_sol_types::private::ToOwned::to_owned(#self))
        })
    }
}

impl Verbatim for bool {
    #[inline]
    fn to_verbatim_tokens(&self, s: &mut TokenStream, _crates: &ExternCrates) {
        quote::ToTokens::to_tokens(self, s)
    }
}

impl Verbatim for usize {
    #[inline]
    fn to_verbatim_tokens(&self, s: &mut TokenStream, _crates: &ExternCrates) {
        quote::ToTokens::to_tokens(self, s)
    }
}

impl<T: Verbatim> Verbatim for Vec<T> {
    fn to_verbatim_tokens(&self, s: &mut TokenStream, crates: &ExternCrates) {
        let alloy_sol_types = &crates.sol_types;
        s.extend(if self.is_empty() {
            quote!(#alloy_sol_types::private::Vec::new())
        } else {
            let iter = self.iter().map(|t| ToTokensCompat(t, crates));
            quote!(#alloy_sol_types::private::vec![#(#iter),*])
        });
    }
}

impl<K: Verbatim, V: Verbatim> Verbatim for BTreeMap<K, V> {
    fn to_verbatim_tokens(&self, s: &mut TokenStream, crates: &ExternCrates) {
        let alloy_sol_types = &crates.sol_types;
        s.extend(if self.is_empty() {
            quote!(#alloy_sol_types::private::BTreeMap::new())
        } else {
            let k = self.keys().map(|t| ToTokensCompat(t, crates));
            let v = self.values().map(|t| ToTokensCompat(t, crates));
            quote!(#alloy_sol_types::private::BTreeMap::from([#( (#k, #v) ),*]))
        });
    }
}

impl<T: Verbatim> Verbatim for Option<T> {
    fn to_verbatim_tokens(&self, s: &mut TokenStream, crates: &ExternCrates) {
        let tts = match self {
            Some(t) => {
                let mut s = TokenStream::new();
                t.to_verbatim_tokens(&mut s, crates);
                quote!(::core::option::Option::Some(#s))
            }
            None => quote!(::core::option::Option::None),
        };
        s.extend(tts);
    }
}

macro_rules! derive_verbatim {
    () => {};

    (struct $name:ident { $($field:ident),* $(,)? } $($rest:tt)*) => {
        impl Verbatim for alloy_json_abi::$name {
            fn to_verbatim_tokens(&self, s: &mut TokenStream, crates: &ExternCrates) {
                let Self { $($field),* } = self;
                $(
                    let $field = ToTokensCompat($field, crates);
                )*
                let alloy_sol_types = &crates.sol_types;
                s.extend(quote! {
                    #alloy_sol_types::private::alloy_json_abi::$name {
                        $($field: #$field,)*
                    }
                });
            }
        }
        derive_verbatim!($($rest)*);
    };

    (enum $name:ident { $($variant:ident $( { $($field_idx:tt : $field:ident),* $(,)? } )?),* $(,)? } $($rest:tt)*) => {
        impl Verbatim for alloy_json_abi::$name {
            fn to_verbatim_tokens(&self, s: &mut TokenStream, crates: &ExternCrates) {
                match self {$(
                    Self::$variant $( { $($field_idx: $field),* } )? => {
                        $($(
                            let $field = ToTokensCompat($field, crates);
                        )*)?
                        let alloy_sol_types = &crates.sol_types;
                        s.extend(quote! {
                            #alloy_sol_types::private::alloy_json_abi::$name::$variant $( { $($field_idx: #$field),* } )?
                        });
                    }
                )*}
            }
        }
        derive_verbatim!($($rest)*);
    };
}

derive_verbatim! {
    struct Constructor { inputs, state_mutability }
    struct Fallback { state_mutability }
    struct Receive { state_mutability }
    struct Function { name, inputs, outputs, state_mutability }
    struct Error { name, inputs }
    struct Event { name, inputs, anonymous }
    struct Param { ty, name, components, internal_type }
    struct EventParam { ty, name, indexed, components, internal_type }

    enum InternalType {
        AddressPayable { 0: s },
        Contract { 0: s },
        Enum { contract: contract, ty: ty },
        Struct { contract: contract, ty: ty },
        Other { contract: contract, ty: ty },
    }

    enum StateMutability {
        Pure,
        View,
        NonPayable,
        Payable,
    }
}