postcard_bindgen_core/code_gen/
function.rs

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
use genco::{lang::Lang, quote, tokens::FormatInto, Tokens};

#[macro_export]
macro_rules! function_args {
    ($(($n:expr, $t:expr)),+ $(,)?) => {

        [$(({
            use genco::tokens::FormatInto;
            let mut tokens = genco::Tokens::new();
            $n.format_into(&mut tokens);
            tokens
        }, {
            use genco::tokens::FormatInto;
            let mut tokens = genco::Tokens::new();
            $t.format_into(&mut tokens);
            tokens
        })),+]
    };
    ($($x:expr),+ $(,)?) => {

        [$({
            use genco::tokens::FormatInto;
            let mut tokens = genco::Tokens::new();
            $x.format_into(&mut tokens);
            tokens
        }),+]
    };

}

pub struct FunctionArg<L>
where
    L: Lang,
{
    pub(super) name: Tokens<L>,
    pub(super) r#type: Option<Tokens<L>>,
}

impl<L> FunctionArg<L>
where
    L: Lang,
{
    pub fn new(name: impl FormatInto<L>, r#type: impl FormatInto<L>) -> Self {
        Self {
            name: quote!($name),
            r#type: Some(quote!($r#type)),
        }
    }

    pub fn new_untyped(name: impl FormatInto<L>) -> Self {
        Self {
            name: quote!($name),
            r#type: None,
        }
    }
}

impl<N, T, L> From<(N, T)> for FunctionArg<L>
where
    L: Lang,
    N: FormatInto<L>,
    T: FormatInto<L>,
{
    fn from((name, r#type): (N, T)) -> Self {
        Self::new(name, r#type)
    }
}

impl<L> From<Tokens<L>> for FunctionArg<L>
where
    L: Lang,
{
    fn from(name: Tokens<L>) -> Self {
        Self::new_untyped(name)
    }
}

pub trait ToArgs<L>
where
    L: Lang,
{
    fn to_args(self) -> Vec<FunctionArg<L>>;
}

impl<L, A> ToArgs<L> for Vec<A>
where
    L: Lang,
    A: Into<FunctionArg<L>>,
{
    fn to_args(self) -> Vec<FunctionArg<L>> {
        self.into_iter().map(|a| a.into()).collect()
    }
}

impl<L> ToArgs<L> for FunctionArg<L>
where
    L: Lang,
{
    fn to_args(self) -> Vec<FunctionArg<L>> {
        vec![self]
    }
}

impl<L> ToArgs<L> for ()
where
    L: Lang,
{
    fn to_args(self) -> Vec<FunctionArg<L>> {
        Vec::new()
    }
}

impl<L, F, const N: usize> ToArgs<L> for [F; N]
where
    L: Lang,
    F: Into<FunctionArg<L>>,
{
    fn to_args(self) -> Vec<FunctionArg<L>> {
        self.into_iter().map(|f| f.into()).collect()
    }
}

impl<'a, L, F, const N: usize> ToArgs<L> for &'a [F; N]
where
    L: Lang,
    &'a F: Into<FunctionArg<L>>,
{
    fn to_args(self) -> Vec<FunctionArg<L>> {
        self.iter().map(|f| f.into()).collect()
    }
}

pub struct Function<L>
where
    L: Lang,
{
    pub(super) args: Vec<FunctionArg<L>>,
    pub(super) name: Tokens<L>,
    pub(super) body: Tokens<L>,
    pub(super) return_type: Option<Tokens<L>>,
}

impl<L> Function<L>
where
    L: Lang,
{
    pub fn new(
        name: impl FormatInto<L>,
        args: impl ToArgs<L>,
        body: impl FormatInto<L>,
        return_type: impl FormatInto<L>,
    ) -> Self {
        Self {
            name: quote!($name),
            args: args.to_args(),
            return_type: Some(quote!($return_type)),
            body: quote!($body),
        }
    }

    pub fn new_untyped(
        name: impl FormatInto<L>,
        args: impl ToArgs<L>,
        body: impl FormatInto<L>,
    ) -> Self {
        Self {
            name: quote!($name),
            args: args.to_args(),
            return_type: None,
            body: quote!($body),
        }
    }
}