@@ -13,28 +13,30 @@ pub fn derive_from_args(input: TokenStream) -> TokenStream {
13
13
gen. to_string ( ) . parse ( ) . unwrap ( )
14
14
}
15
15
16
- enum ArgKind {
17
- Positional ,
18
- PositionalKeyword ,
19
- Keyword ,
16
+ /// The kind of the python parameter, this corresponds to the value of Parameter.kind
17
+ /// (https://docs.python.org/3/library/inspect.html#inspect.Parameter.kind)
18
+ enum ParameterKind {
19
+ PositionalOnly ,
20
+ PositionalOrKeyword ,
21
+ KeywordOnly ,
20
22
}
21
23
22
- impl ArgKind {
23
- fn from_ident ( ident : & Ident ) -> ArgKind {
24
+ impl ParameterKind {
25
+ fn from_ident ( ident : & Ident ) -> ParameterKind {
24
26
if ident == "positional" {
25
- ArgKind :: Positional
27
+ ParameterKind :: PositionalOnly
26
28
} else if ident == "positional_keyword" {
27
- ArgKind :: PositionalKeyword
29
+ ParameterKind :: PositionalOrKeyword
28
30
} else if ident == "keyword" {
29
- ArgKind :: Keyword
31
+ ParameterKind :: KeywordOnly
30
32
} else {
31
33
panic ! ( "Unrecognised attribute" )
32
34
}
33
35
}
34
36
}
35
37
36
38
struct ArgAttribute {
37
- kind : ArgKind ,
39
+ kind : ParameterKind ,
38
40
default : Option < Expr > ,
39
41
optional : bool ,
40
42
}
@@ -50,7 +52,7 @@ impl ArgAttribute {
50
52
let mut iter = list. nested . iter ( ) ;
51
53
let first_arg = iter. next ( ) . expect ( "at least one argument in pyarg list" ) ;
52
54
let kind = match first_arg {
53
- NestedMeta :: Meta ( Meta :: Word ( ident) ) => ArgKind :: from_ident ( ident) ,
55
+ NestedMeta :: Meta ( Meta :: Word ( ident) ) => ParameterKind :: from_ident ( ident) ,
54
56
_ => panic ! ( "Bad syntax for first pyarg attribute argument" ) ,
55
57
} ;
56
58
@@ -125,7 +127,7 @@ fn generate_field(field: &Field) -> TokenStream2 {
125
127
. collect :: < Vec < _ > > ( ) ;
126
128
let attr = if pyarg_attrs. is_empty ( ) {
127
129
ArgAttribute {
128
- kind : ArgKind :: PositionalKeyword ,
130
+ kind : ParameterKind :: PositionalOrKeyword ,
129
131
default : None ,
130
132
optional : false ,
131
133
}
@@ -153,10 +155,10 @@ fn generate_field(field: &Field) -> TokenStream2 {
153
155
}
154
156
} else {
155
157
let err = match attr. kind {
156
- ArgKind :: Positional | ArgKind :: PositionalKeyword => quote ! {
158
+ ParameterKind :: PositionalOnly | ParameterKind :: PositionalOrKeyword => quote ! {
157
159
crate :: function:: ArgumentError :: TooFewArgs
158
160
} ,
159
- ArgKind :: Keyword => quote ! {
161
+ ParameterKind :: KeywordOnly => quote ! {
160
162
crate :: function:: ArgumentError :: RequiredKeywordArgument ( tringify!( #name) )
161
163
} ,
162
164
} ;
@@ -166,17 +168,17 @@ fn generate_field(field: &Field) -> TokenStream2 {
166
168
} ;
167
169
168
170
match attr. kind {
169
- ArgKind :: Positional => {
171
+ ParameterKind :: PositionalOnly => {
170
172
quote ! {
171
173
#name: args. take_positional( ) #middle#ending,
172
174
}
173
175
}
174
- ArgKind :: PositionalKeyword => {
176
+ ParameterKind :: PositionalOrKeyword => {
175
177
quote ! {
176
178
#name: args. take_positional_keyword( stringify!( #name) ) #middle#ending,
177
179
}
178
180
}
179
- ArgKind :: Keyword => {
181
+ ParameterKind :: KeywordOnly => {
180
182
quote ! {
181
183
#name: args. take_keyword( stringify!( #name) ) #middle#ending,
182
184
}
0 commit comments