thruput-macros 0.58.0

Macros for smart contract development
Documentation
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Parse attributes for the contract macro

use darling::FromMeta;
use darling::util::SpannedValue;
use proc_macro2::Span;
use quote::ToTokens;
use std::collections::BTreeMap;

/// One entry of a `throws(...)` list: the path naming an exception type.
///
/// A thrown type is just a type. It used to double as a variant name for
/// the generated client error enum, which is why differently-scoped types
/// sharing a last segment (`alpha::E` and `beta::E`) used to collide; with
/// no enum to name, they no longer do.
#[derive(Clone, Debug)]
pub struct ThrownType {
    ty: syn::Path,
}

impl ThrownType {
    fn from_path(path: &syn::Path) -> Self {
        Self { ty: path.clone() }
    }

    pub fn ty(&self) -> &syn::Path {
        &self.ty
    }

    fn ty_string(&self) -> String {
        self.ty.to_token_stream().to_string()
    }
}

#[derive(Clone, Debug)]
pub struct ThrowsList(Vec<ThrownType>);

impl ThrowsList {
    fn new(thrown_types: Vec<ThrownType>) -> darling::Result<Self> {
        if thrown_types.is_empty() {
            return Err(darling::Error::custom(
                "throws() requires at least one type",
            ));
        }

        // Same type listed twice: harmless to the client, but it would
        // duplicate the entry in the contract spec, and it is never what
        // the author meant.
        let mut seen = BTreeMap::new();
        for thrown in &thrown_types {
            if seen.insert(thrown.ty_string(), ()).is_some() {
                return Err(darling::Error::custom(format!(
                    "throws lists `{}` more than once",
                    thrown.ty_string(),
                ))
                .with_span(thrown.ty()));
            }
        }

        Ok(Self(thrown_types))
    }
}

impl FromMeta for ThrowsList {
    fn from_value(value: &syn::Lit) -> darling::Result<Self> {
        Err(darling::Error::unexpected_lit_type(value))
    }

    fn from_word() -> darling::Result<Self> {
        Err(darling::Error::unsupported_format("word"))
    }

    fn from_expr(expr: &syn::Expr) -> darling::Result<Self> {
        match expr {
            syn::Expr::Path(ep) if ep.qself.is_none() => {
                Self::new(vec![ThrownType::from_path(&ep.path)])
            }
            _ => Err(darling::Error::custom(
                "throws expects a type path or list of type paths",
            )),
        }
    }

    fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> {
        let mut thrown_types = Vec::new();
        for item in items {
            match item {
                darling::ast::NestedMeta::Meta(syn::Meta::Path(path)) => {
                    thrown_types.push(ThrownType::from_path(path));
                }
                _ => {
                    return Err(darling::Error::custom(
                        "throws expects type paths",
                    ));
                }
            }
        }
        Self::new(thrown_types)
    }
}

/// A list of event type paths declared via `emits(...)`.
#[derive(Clone, Debug)]
pub struct EmitsList(Vec<syn::Path>);

impl EmitsList {
    fn new(events: Vec<syn::Path>) -> darling::Result<Self> {
        if events.is_empty() {
            return Err(darling::Error::custom(
                "emits() requires at least one event type",
            ));
        }
        Ok(Self(events))
    }
}

impl FromMeta for EmitsList {
    fn from_value(value: &syn::Lit) -> darling::Result<Self> {
        Err(darling::Error::unexpected_lit_type(value))
    }

    fn from_word() -> darling::Result<Self> {
        Err(darling::Error::unsupported_format("word"))
    }

    fn from_expr(expr: &syn::Expr) -> darling::Result<Self> {
        match expr {
            syn::Expr::Path(ep) if ep.qself.is_none() => {
                Self::new(vec![ep.path.clone()])
            }
            _ => Err(darling::Error::custom(
                "emits expects a type path or list of type paths",
            )),
        }
    }

    fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> {
        let mut events = Vec::new();
        for item in items {
            match item {
                darling::ast::NestedMeta::Meta(syn::Meta::Path(path)) => {
                    events.push(path.clone());
                }
                _ => {
                    return Err(darling::Error::custom(
                        "emits expects type paths",
                    ));
                }
            }
        }
        Self::new(events)
    }
}

#[derive(Clone, Debug, FromMeta)]
pub struct Config {
    pub exports: Option<SpannedValue<String>>,
    pub interface: Option<SpannedValue<String>>,
    pub version: Option<SpannedValue<String>>,

    client: Option<syn::Ident>,
    throws: Option<ThrowsList>,
    emits: Option<EmitsList>,
}

impl Config {
    /// Validate user-supplied string attributes up front, so malformed
    /// input produces a compile error instead of a proc-macro panic
    /// deeper in code generation.
    pub fn validate(&self) -> syn::Result<()> {
        use super::util::{validate_ident_string, validate_version_string};

        if let Some(version) = &self.version {
            validate_version_string(version.span(), version)?;
        }
        if let Some(exports) = &self.exports {
            validate_ident_string(exports.span(), exports, "exports")?;
        }
        if let Some(interface) = &self.interface {
            validate_ident_string(interface.span(), interface, "interface")?;
        }
        Ok(())
    }

    pub fn client_ident(&self) -> Option<syn::Ident> {
        self.client.clone()
    }

    pub fn interface_name(&self) -> Option<String> {
        self.interface.as_ref().map(|s| s.as_str().to_string())
    }

    pub fn version_string(&self) -> Option<String> {
        self.version.as_ref().map(|v| v.as_str().to_string())
    }

    pub fn internal_exports_mod_ident(
        &self,
        effective_interface_name: &str,
    ) -> syn::Ident {
        if let Some(name) = &self.exports {
            return syn::Ident::new(name, name.span());
        }

        syn::Ident::new(
            &format!("__thruput_exports_{}", effective_interface_name),
            Span::call_site(),
        )
    }

    /// Returns the list of thrown error types (empty if none declared).
    pub fn throws_list(&self) -> Vec<ThrownType> {
        match &self.throws {
            Some(ThrowsList(thrown_types)) => thrown_types.clone(),
            None => vec![],
        }
    }

    /// Returns the first thrown error type (backwards compat).
    pub fn throws(&self) -> Option<ThrownType> {
        self.throws.as_ref().and_then(|t| t.0.first()).cloned()
    }

    /// Returns the list of declared event types (empty if none declared).
    pub fn emits_list(&self) -> Vec<syn::Path> {
        match &self.emits {
            Some(EmitsList(events)) => events.clone(),
            None => vec![],
        }
    }

    /// Returns true if any events were declared via `emits(...)`.
    pub fn has_emits(&self) -> bool {
        self.emits.is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use darling::ast::NestedMeta;

    /// Parse a `Config` from a `#[contract(...)]`-style attribute string.
    fn parse_config(attrs: &str) -> Config {
        let tokens: proc_macro2::TokenStream = attrs.parse().unwrap();
        let meta_list = NestedMeta::parse_meta_list(tokens).unwrap();
        Config::from_list(&meta_list).unwrap()
    }

    fn empty_config() -> Config {
        parse_config("")
    }

    #[test]
    fn test_setup_key_is_rejected_as_unknown() {
        // Dead config key: darling's unknown-field error handles it
        // uniformly across item kinds and features.
        let tokens: proc_macro2::TokenStream =
            r#"test_setup = "setup_fn""#.parse().unwrap();
        let meta_list = NestedMeta::parse_meta_list(tokens).unwrap();
        let err = Config::from_list(&meta_list).unwrap_err();
        assert!(err.to_string().contains("test_setup"), "{err}");
    }

    // -- internal_exports_mod_ident --

    #[test]
    fn hidden_exports_name_from_effective_interface() {
        let config = empty_config();
        assert_eq!(
            config.internal_exports_mod_ident("Adder").to_string(),
            "__thruput_exports_Adder"
        );
        assert_eq!(
            config.internal_exports_mod_ident("ERC20").to_string(),
            "__thruput_exports_ERC20"
        );
        assert_eq!(
            config.internal_exports_mod_ident("ISwapV2Pair").to_string(),
            "__thruput_exports_ISwapV2Pair"
        );
    }

    #[test]
    fn explicit_exports_overrides_hidden_exports_name() {
        let config = parse_config(r#"exports = "custom""#);
        assert_eq!(
            config.internal_exports_mod_ident("Adder").to_string(),
            "custom"
        );
    }

    // -- throws --

    #[test]
    fn throws_single_ident() {
        let config = parse_config(r#"throws = MyError"#);
        let throws = config.throws_list();
        assert_eq!(throws.len(), 1);
        assert_eq!(throws[0].ty().to_token_stream().to_string(), "MyError");
    }

    #[test]
    fn throws_list_syntax() {
        let config = parse_config(r#"throws(SwapError, ERC20Error)"#);
        let throws = config.throws_list();
        assert_eq!(throws.len(), 2);
        assert_eq!(throws[0].ty().to_token_stream().to_string(), "SwapError");
        assert_eq!(throws[1].ty().to_token_stream().to_string(), "ERC20Error");
    }

    #[test]
    fn throws_list_single() {
        let config = parse_config(r#"throws(SwapError)"#);
        let throws = config.throws_list();
        assert_eq!(throws.len(), 1);
        assert_eq!(throws[0].ty().to_token_stream().to_string(), "SwapError");
    }

    #[test]
    fn throws_path_single() {
        let config = parse_config(r#"throws = alpha::MyError"#);
        let throws = config.throws_list();
        assert_eq!(throws.len(), 1);
        assert_eq!(
            throws[0].ty().to_token_stream().to_string(),
            "alpha :: MyError"
        );
    }

    #[test]
    fn throws_path_list_syntax() {
        let config =
            parse_config(r#"throws(alpha::SwapError, beta::ERC20Error)"#);
        let throws = config.throws_list();
        assert_eq!(throws.len(), 2);
        assert_eq!(
            throws[0].ty().to_token_stream().to_string(),
            "alpha :: SwapError"
        );
        assert_eq!(
            throws[1].ty().to_token_stream().to_string(),
            "beta :: ERC20Error"
        );
    }

    /// Types that only share a last segment used to collide, because the
    /// last segment named a variant of the generated client error enum.
    #[test]
    fn throws_allows_same_named_types_from_different_modules() {
        let config = parse_config(r#"throws(alpha::E, beta::E)"#);
        assert_eq!(config.throws_list().len(), 2);
    }

    #[test]
    fn throws_none() {
        let config = parse_config(r#""#);
        let throws = config.throws_list();
        assert!(throws.is_empty());
    }
}

#[cfg(test)]
mod emits_tests {
    use super::*;
    use darling::ast::NestedMeta;

    fn parse_config(attrs: &str) -> darling::Result<Config> {
        let tokens: proc_macro2::TokenStream = attrs.parse().unwrap();
        let meta_list = NestedMeta::parse_meta_list(tokens).unwrap();
        Config::from_list(&meta_list)
    }

    #[test]
    fn emits_list_syntax() {
        let config =
            parse_config(r#"emits(TransferEvent, events::ApprovalEvent)"#)
                .unwrap();
        let emits = config.emits_list();
        assert_eq!(emits.len(), 2);
        assert_eq!(emits[0].to_token_stream().to_string(), "TransferEvent");
        assert_eq!(
            emits[1].to_token_stream().to_string(),
            "events :: ApprovalEvent"
        );
    }

    #[test]
    fn emits_single_path() {
        let config = parse_config(r#"emits = TransferEvent"#).unwrap();
        assert_eq!(config.emits_list().len(), 1);
        assert!(config.has_emits());
    }

    #[test]
    fn emits_none() {
        let config = parse_config(r#""#).unwrap();
        assert!(config.emits_list().is_empty());
        assert!(!config.has_emits());
    }

    #[test]
    fn emits_empty_list_rejected() {
        assert!(parse_config(r#"emits()"#).is_err());
    }
}