aboutsummaryrefslogtreecommitdiff
path: root/macros/src/util/syn_ext.rs
blob: 620167038fd66ccb2cad20195821ee9293738795 (plain)
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
use proc_macro2::Ident;
use syn::punctuated::Punctuated;
use syn::token::Paren;
use syn::{
    Expr,
    ExprCall,
    ExprLit,
    ExprMethodCall,
    ExprPath,
    GenericMethodArgument,
    Lit,
    MethodTurbofish,
    Path,
    Token,
};

pub trait ExprMethodCallExt
{
    fn new(receiver: Expr, method: Ident, args: impl IntoIterator<Item = Expr>) -> Self;

    fn with_turbofish(self, turbofish: MethodTurbofish) -> Self;
}

impl ExprMethodCallExt for ExprMethodCall
{
    fn new(receiver: Expr, method: Ident, args: impl IntoIterator<Item = Expr>) -> Self
    {
        Self {
            attrs: Vec::new(),
            receiver: Box::new(receiver),
            dot_token: <Token![.]>::default(),
            method,
            turbofish: None,
            paren_token: Paren::default(),
            args: Punctuated::from_iter(args),
        }
    }

    fn with_turbofish(mut self, turbofish: MethodTurbofish) -> Self
    {
        self.turbofish = Some(turbofish);

        self
    }
}

pub trait ExprPathExt
{
    fn new(path: Path) -> Self;
}

impl ExprPathExt for ExprPath
{
    fn new(path: Path) -> Self
    {
        Self {
            attrs: Vec::new(),
            qself: None,
            path,
        }
    }
}

pub trait MethodTurbofishExt
{
    fn new(args: impl IntoIterator<Item = GenericMethodArgument>) -> Self;
}

impl MethodTurbofishExt for MethodTurbofish
{
    fn new(args: impl IntoIterator<Item = GenericMethodArgument>) -> Self
    {
        Self {
            colon2_token: <Token![::]>::default(),
            lt_token: <Token![<]>::default(),
            args: Punctuated::from_iter(args),
            gt_token: <Token![>]>::default(),
        }
    }
}

pub trait ExprCallExt
{
    fn new(function: Expr, args: impl IntoIterator<Item = Expr>) -> Self;
}

impl ExprCallExt for ExprCall
{
    fn new(function: Expr, args: impl IntoIterator<Item = Expr>) -> Self
    {
        Self {
            attrs: Vec::new(),
            func: Box::new(function),
            paren_token: Paren::default(),
            args: Punctuated::from_iter(args),
        }
    }
}

pub trait ExprLitExt
{
    fn new(lit: impl Into<Lit>) -> Self;
}

impl ExprLitExt for ExprLit
{
    fn new(lit: impl Into<Lit>) -> Self
    {
        Self {
            attrs: Vec::new(),
            lit: lit.into(),
        }
    }
}