forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrom_args.rs
228 lines (212 loc) · 7.81 KB
/
from_args.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
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
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{
parse_quote, Attribute, Data, DeriveInput, Expr, Field, Ident, Lit, Meta, NestedMeta, Result,
};
/// The kind of the python parameter, this corresponds to the value of Parameter.kind
/// (https://docs.python.org/3/library/inspect.html#inspect.Parameter.kind)
enum ParameterKind {
PositionalOnly,
PositionalOrKeyword,
KeywordOnly,
Flatten,
}
impl ParameterKind {
fn from_ident(ident: &Ident) -> Option<ParameterKind> {
match ident.to_string().as_str() {
"positional" => Some(ParameterKind::PositionalOnly),
"any" => Some(ParameterKind::PositionalOrKeyword),
"named" => Some(ParameterKind::KeywordOnly),
"flatten" => Some(ParameterKind::Flatten),
_ => None,
}
}
}
struct ArgAttribute {
name: Option<String>,
kind: ParameterKind,
default: Option<DefaultValue>,
}
// None == quote!(Default::default())
type DefaultValue = Option<Expr>;
impl ArgAttribute {
fn from_attribute(attr: &Attribute) -> Option<Result<ArgAttribute>> {
if !attr.path.is_ident("pyarg") {
return None;
}
let inner = move || {
let Meta::List(list) = attr.parse_meta()? else {
bail_span!(attr, "pyarg must be a list, like #[pyarg(...)]")
};
let mut iter = list.nested.iter();
let first_arg = iter.next().ok_or_else(|| {
err_span!(list, "There must be at least one argument to #[pyarg()]")
})?;
let kind = match first_arg {
NestedMeta::Meta(Meta::Path(path)) => {
path.get_ident().and_then(ParameterKind::from_ident)
}
_ => None,
};
let kind = kind.ok_or_else(|| {
err_span!(
first_arg,
"The first argument to #[pyarg()] must be the parameter type, either \
'positional', 'any', 'named', or 'flatten'."
)
})?;
let mut attribute = ArgAttribute {
name: None,
kind,
default: None,
};
for arg in iter {
attribute.parse_argument(arg)?;
}
Ok(attribute)
};
Some(inner())
}
fn parse_argument(&mut self, arg: &NestedMeta) -> Result<()> {
if let ParameterKind::Flatten = self.kind {
bail_span!(arg, "can't put additional arguments on a flatten arg")
}
match arg {
NestedMeta::Meta(Meta::Path(path)) => {
if path.is_ident("default") || path.is_ident("optional") {
if self.default.is_none() {
self.default = Some(None);
}
} else {
bail_span!(path, "Unrecognized pyarg attribute");
}
}
NestedMeta::Meta(Meta::NameValue(name_value)) => {
if name_value.path.is_ident("default") {
if matches!(self.default, Some(Some(_))) {
bail_span!(name_value, "Default already set");
}
match name_value.lit {
Lit::Str(ref val) => self.default = Some(Some(val.parse()?)),
_ => bail_span!(name_value, "Expected string value for default argument"),
}
} else if name_value.path.is_ident("name") {
if self.name.is_some() {
bail_span!(name_value, "already have a name")
}
match &name_value.lit {
Lit::Str(val) => self.name = Some(val.value()),
_ => bail_span!(name_value, "Expected string value for name argument"),
}
} else {
bail_span!(name_value, "Unrecognized pyarg attribute");
}
}
_ => bail_span!(arg, "Unrecognized pyarg attribute"),
}
Ok(())
}
}
fn generate_field((i, field): (usize, &Field)) -> Result<TokenStream> {
let mut pyarg_attrs = field
.attrs
.iter()
.filter_map(ArgAttribute::from_attribute)
.collect::<std::result::Result<Vec<_>, _>>()?;
let attr = if pyarg_attrs.is_empty() {
ArgAttribute {
name: None,
kind: ParameterKind::PositionalOrKeyword,
default: None,
}
} else if pyarg_attrs.len() == 1 {
pyarg_attrs.remove(0)
} else {
bail_span!(field, "Multiple pyarg attributes on field");
};
let name = field.ident.as_ref();
let name_string = name.map(Ident::to_string);
if matches!(&name_string, Some(s) if s.starts_with("_phantom")) {
return Ok(quote! {
#name: ::std::marker::PhantomData,
});
}
let field_name = match name {
Some(id) => id.to_token_stream(),
None => syn::Index::from(i).into_token_stream(),
};
if let ParameterKind::Flatten = attr.kind {
return Ok(quote! {
#field_name: ::rustpython_vm::function::FromArgs::from_args(vm, args)?,
});
}
let pyname = attr
.name
.or(name_string)
.ok_or_else(|| err_span!(field, "field in tuple struct must have name attribute"))?;
let middle = quote! {
.map(|x| ::rustpython_vm::convert::TryFromObject::try_from_object(vm, x)).transpose()?
};
let ending = if let Some(default) = attr.default {
let default = default.unwrap_or_else(|| parse_quote!(::std::default::Default::default()));
quote! {
.map(::rustpython_vm::function::FromArgOptional::from_inner)
.unwrap_or_else(|| #default)
}
} else {
let err = match attr.kind {
ParameterKind::PositionalOnly | ParameterKind::PositionalOrKeyword => quote! {
::rustpython_vm::function::ArgumentError::TooFewArgs
},
ParameterKind::KeywordOnly => quote! {
::rustpython_vm::function::ArgumentError::RequiredKeywordArgument(#pyname.to_owned())
},
ParameterKind::Flatten => unreachable!(),
};
quote! {
.ok_or_else(|| #err)?
}
};
let file_output = match attr.kind {
ParameterKind::PositionalOnly => {
quote! {
#field_name: args.take_positional()#middle #ending,
}
}
ParameterKind::PositionalOrKeyword => {
quote! {
#field_name: args.take_positional_keyword(#pyname)#middle #ending,
}
}
ParameterKind::KeywordOnly => {
quote! {
#field_name: args.take_keyword(#pyname)#middle #ending,
}
}
ParameterKind::Flatten => unreachable!(),
};
Ok(file_output)
}
pub fn impl_from_args(input: DeriveInput) -> Result<TokenStream> {
let fields = match input.data {
Data::Struct(syn::DataStruct { fields, .. }) => fields
.iter()
.enumerate()
.map(generate_field)
.collect::<Result<TokenStream>>()?,
_ => bail_span!(input, "FromArgs input must be a struct"),
};
let name = input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let output = quote! {
impl #impl_generics ::rustpython_vm::function::FromArgs for #name #ty_generics #where_clause {
fn from_args(
vm: &::rustpython_vm::VirtualMachine,
args: &mut ::rustpython_vm::function::FuncArgs
) -> ::std::result::Result<Self, ::rustpython_vm::function::ArgumentError> {
Ok(#name { #fields })
}
}
};
Ok(output)
}