Skip to content

[WASM] Clean up a bit #795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions derive/src/from_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl ArgAttribute {
}
}

fn generate_field(field: &Field) -> TokenStream2 {
fn generate_field(field: &Field, rp_path: &syn::Path) -> TokenStream2 {
let mut pyarg_attrs = field
.attrs
.iter()
Expand All @@ -132,24 +132,24 @@ fn generate_field(field: &Field) -> TokenStream2 {

let name = &field.ident;
let middle = quote! {
.map(|x| crate::pyobject::TryFromObject::try_from_object(vm, x)).transpose()?
.map(|x| #rp_path::pyobject::TryFromObject::try_from_object(vm, x)).transpose()?
};
let ending = if let Some(default) = attr.default {
quote! {
.unwrap_or_else(|| #default)
}
} else if attr.optional {
quote! {
.map(crate::function::OptionalArg::Present)
.unwrap_or(crate::function::OptionalArg::Missing)
.map(#rp_path::function::OptionalArg::Present)
.unwrap_or(#rp_path::function::OptionalArg::Missing)
}
} else {
let err = match attr.kind {
ParameterKind::PositionalOnly | ParameterKind::PositionalOrKeyword => quote! {
crate::function::ArgumentError::TooFewArgs
#rp_path::function::ArgumentError::TooFewArgs
},
ParameterKind::KeywordOnly => quote! {
crate::function::ArgumentError::RequiredKeywordArgument(tringify!(#name))
#rp_path::function::ArgumentError::RequiredKeywordArgument(tringify!(#name))
},
};
quote! {
Expand Down Expand Up @@ -181,7 +181,10 @@ pub fn impl_from_args(input: DeriveInput) -> TokenStream2 {
let fields = match input.data {
Data::Struct(ref data) => {
match data.fields {
Fields::Named(ref fields) => fields.named.iter().map(generate_field),
Fields::Named(ref fields) => fields
.named
.iter()
.map(|field| generate_field(field, &rp_path)),
Fields::Unnamed(_) | Fields::Unit => unimplemented!(), // TODO: better error message
}
}
Expand All @@ -192,9 +195,9 @@ pub fn impl_from_args(input: DeriveInput) -> TokenStream2 {
quote! {
impl #rp_path::function::FromArgs for #name {
fn from_args(
vm: &crate::vm::VirtualMachine,
args: &mut crate::function::PyFuncArgs
) -> Result<Self, crate::function::ArgumentError> {
vm: &#rp_path::VirtualMachine,
args: &mut #rp_path::function::PyFuncArgs
) -> Result<Self, #rp_path::function::ArgumentError> {
Ok(#name { #(#fields)* })
}
}
Expand Down
Loading