Skip to content

Commit 6fd6cc6

Browse files
committed
Renamed ArgKind, and member to be consistent with python inspect module
1 parent c8f4474 commit 6fd6cc6

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

derive/src/lib.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,30 @@ pub fn derive_from_args(input: TokenStream) -> TokenStream {
1313
gen.to_string().parse().unwrap()
1414
}
1515

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,
2022
}
2123

22-
impl ArgKind {
23-
fn from_ident(ident: &Ident) -> ArgKind {
24+
impl ParameterKind {
25+
fn from_ident(ident: &Ident) -> ParameterKind {
2426
if ident == "positional" {
25-
ArgKind::Positional
27+
ParameterKind::PositionalOnly
2628
} else if ident == "positional_keyword" {
27-
ArgKind::PositionalKeyword
29+
ParameterKind::PositionalOrKeyword
2830
} else if ident == "keyword" {
29-
ArgKind::Keyword
31+
ParameterKind::KeywordOnly
3032
} else {
3133
panic!("Unrecognised attribute")
3234
}
3335
}
3436
}
3537

3638
struct ArgAttribute {
37-
kind: ArgKind,
39+
kind: ParameterKind,
3840
default: Option<Expr>,
3941
optional: bool,
4042
}
@@ -50,7 +52,7 @@ impl ArgAttribute {
5052
let mut iter = list.nested.iter();
5153
let first_arg = iter.next().expect("at least one argument in pyarg list");
5254
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),
5456
_ => panic!("Bad syntax for first pyarg attribute argument"),
5557
};
5658

@@ -125,7 +127,7 @@ fn generate_field(field: &Field) -> TokenStream2 {
125127
.collect::<Vec<_>>();
126128
let attr = if pyarg_attrs.is_empty() {
127129
ArgAttribute {
128-
kind: ArgKind::PositionalKeyword,
130+
kind: ParameterKind::PositionalOrKeyword,
129131
default: None,
130132
optional: false,
131133
}
@@ -153,10 +155,10 @@ fn generate_field(field: &Field) -> TokenStream2 {
153155
}
154156
} else {
155157
let err = match attr.kind {
156-
ArgKind::Positional | ArgKind::PositionalKeyword => quote! {
158+
ParameterKind::PositionalOnly | ParameterKind::PositionalOrKeyword => quote! {
157159
crate::function::ArgumentError::TooFewArgs
158160
},
159-
ArgKind::Keyword => quote! {
161+
ParameterKind::KeywordOnly => quote! {
160162
crate::function::ArgumentError::RequiredKeywordArgument(tringify!(#name))
161163
},
162164
};
@@ -166,17 +168,17 @@ fn generate_field(field: &Field) -> TokenStream2 {
166168
};
167169

168170
match attr.kind {
169-
ArgKind::Positional => {
171+
ParameterKind::PositionalOnly => {
170172
quote! {
171173
#name: args.take_positional()#middle#ending,
172174
}
173175
}
174-
ArgKind::PositionalKeyword => {
176+
ParameterKind::PositionalOrKeyword => {
175177
quote! {
176178
#name: args.take_positional_keyword(stringify!(#name))#middle#ending,
177179
}
178180
}
179-
ArgKind::Keyword => {
181+
ParameterKind::KeywordOnly => {
180182
quote! {
181183
#name: args.take_keyword(stringify!(#name))#middle#ending,
182184
}

0 commit comments

Comments
 (0)