Format code

This commit is contained in:
David Hewitt 2019-12-17 22:58:34 +00:00
parent 4b18830f1e
commit 0032508c3c
3 changed files with 56 additions and 36 deletions

View file

@ -1,7 +1,7 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use crate::pyfunction::Argument;
use crate::pyfunction::{PyFunctionAttr, parse_name_attribute};
use crate::pyfunction::{parse_name_attribute, PyFunctionAttr};
use crate::utils;
use proc_macro2::TokenStream;
use quote::quote;
@ -61,8 +61,11 @@ impl<'a> FnSpec<'a> {
allow_custom_name: bool,
) -> syn::Result<FnSpec<'a>> {
let name = &sig.ident;
let MethodAttributes { ty: mut fn_type, args: fn_attrs, mut python_name } =
parse_method_attributes(meth_attrs, allow_custom_name)?;
let MethodAttributes {
ty: mut fn_type,
args: fn_attrs,
mut python_name,
} = parse_method_attributes(meth_attrs, allow_custom_name)?;
let mut has_self = false;
let mut arguments = Vec::new();
@ -346,7 +349,7 @@ pub fn check_arg_ty_and_optional<'a>(
struct MethodAttributes {
ty: FnType,
args: Vec<Argument>,
python_name: Option<syn::Ident>
python_name: Option<syn::Ident>,
}
fn parse_method_attributes(
@ -477,36 +480,47 @@ fn parse_method_attributes(
property_name
};
Ok(MethodAttributes { ty, args, python_name })
Ok(MethodAttributes {
ty,
args,
python_name,
})
}
fn parse_method_name_attribute(
ty: &FnType,
attrs: &mut Vec<syn::Attribute>,
property_name: Option<syn::Ident>
property_name: Option<syn::Ident>,
) -> syn::Result<Option<syn::Ident>> {
let name = parse_name_attribute(attrs)?;
// Reject some invalid combinations
if let Some(name) = &name {
match ty {
FnType::FnNew => return Err(syn::Error::new_spanned(
FnType::FnNew => {
return Err(syn::Error::new_spanned(
name,
"name can not be specified with #[new]",
)),
FnType::FnCall => return Err(syn::Error::new_spanned(
))
}
FnType::FnCall => {
return Err(syn::Error::new_spanned(
name,
"name can not be specified with #[call]",
)),
FnType::Getter => return Err(syn::Error::new_spanned(
))
}
FnType::Getter => {
return Err(syn::Error::new_spanned(
name,
"name can not be specified for getter",
)),
FnType::Setter => return Err(syn::Error::new_spanned(
))
}
FnType::Setter => {
return Err(syn::Error::new_spanned(
name,
"name can not be specified for setter",
)),
))
}
_ => {}
}
}

View file

@ -1,12 +1,12 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use crate::module::add_fn_to_module;
use proc_macro2::TokenStream;
use syn::ext::IdentExt;
use syn::parse::ParseBuffer;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{NestedMeta, Path};
use proc_macro2::TokenStream;
use crate::module::add_fn_to_module;
#[derive(Debug, Clone, PartialEq)]
pub enum Argument {
@ -201,21 +201,22 @@ pub fn parse_name_attribute(attrs: &mut Vec<syn::Attribute>) -> syn::Result<Opti
let mut name_attrs = Vec::new();
// Using retain will extract all name attributes from the attribute list
attrs.retain(|attr| {
match attr.parse_meta() {
attrs.retain(|attr| match attr.parse_meta() {
Ok(syn::Meta::NameValue(ref nv)) if nv.path.is_ident("name") => {
name_attrs.push((nv.lit.clone(), attr.span()));
false
}
_ => true
}
_ => true,
});
let mut name = None;
for (lit, span) in name_attrs {
if name.is_some() {
return Err(syn::Error::new(span, "#[name] can not be specified multiple times"))
return Err(syn::Error::new(
span,
"#[name] can not be specified multiple times",
));
}
name = match lit {
@ -224,8 +225,13 @@ pub fn parse_name_attribute(attrs: &mut Vec<syn::Attribute>) -> syn::Result<Opti
// This span is the whole attribute span, which is nicer for reporting errors.
ident.set_span(span);
Some(ident)
},
_ => return Err(syn::Error::new(span, "Expected string literal for #[name] argument"))
}
_ => {
return Err(syn::Error::new(
span,
"Expected string literal for #[name] argument",
))
}
};
}