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 // Copyright (c) 2017-present PyO3 Project and Contributors
use crate::pyfunction::Argument; use crate::pyfunction::Argument;
use crate::pyfunction::{PyFunctionAttr, parse_name_attribute}; use crate::pyfunction::{parse_name_attribute, PyFunctionAttr};
use crate::utils; use crate::utils;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::quote; use quote::quote;
@ -61,8 +61,11 @@ impl<'a> FnSpec<'a> {
allow_custom_name: bool, allow_custom_name: bool,
) -> syn::Result<FnSpec<'a>> { ) -> syn::Result<FnSpec<'a>> {
let name = &sig.ident; let name = &sig.ident;
let MethodAttributes { ty: mut fn_type, args: fn_attrs, mut python_name } = let MethodAttributes {
parse_method_attributes(meth_attrs, allow_custom_name)?; 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 has_self = false;
let mut arguments = Vec::new(); let mut arguments = Vec::new();
@ -346,7 +349,7 @@ pub fn check_arg_ty_and_optional<'a>(
struct MethodAttributes { struct MethodAttributes {
ty: FnType, ty: FnType,
args: Vec<Argument>, args: Vec<Argument>,
python_name: Option<syn::Ident> python_name: Option<syn::Ident>,
} }
fn parse_method_attributes( fn parse_method_attributes(
@ -477,36 +480,47 @@ fn parse_method_attributes(
property_name property_name
}; };
Ok(MethodAttributes { ty, args, python_name }) Ok(MethodAttributes {
ty,
args,
python_name,
})
} }
fn parse_method_name_attribute( fn parse_method_name_attribute(
ty: &FnType, ty: &FnType,
attrs: &mut Vec<syn::Attribute>, attrs: &mut Vec<syn::Attribute>,
property_name: Option<syn::Ident> property_name: Option<syn::Ident>,
) -> syn::Result<Option<syn::Ident>> { ) -> syn::Result<Option<syn::Ident>> {
let name = parse_name_attribute(attrs)?; let name = parse_name_attribute(attrs)?;
// Reject some invalid combinations // Reject some invalid combinations
if let Some(name) = &name { if let Some(name) = &name {
match ty { match ty {
FnType::FnNew => return Err(syn::Error::new_spanned( FnType::FnNew => {
name, return Err(syn::Error::new_spanned(
"name can not be specified with #[new]", name,
)), "name can not be specified with #[new]",
FnType::FnCall => return Err(syn::Error::new_spanned( ))
name, }
"name can not be specified with #[call]", FnType::FnCall => {
)), return Err(syn::Error::new_spanned(
FnType::Getter => return Err(syn::Error::new_spanned( name,
name, "name can not be specified with #[call]",
"name can not be specified for getter", ))
)), }
FnType::Setter => return Err(syn::Error::new_spanned( FnType::Getter => {
name, return Err(syn::Error::new_spanned(
"name can not be specified for setter", name,
)), "name can not be specified for getter",
))
}
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 // 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::ext::IdentExt;
use syn::parse::ParseBuffer; use syn::parse::ParseBuffer;
use syn::punctuated::Punctuated; use syn::punctuated::Punctuated;
use syn::spanned::Spanned; use syn::spanned::Spanned;
use syn::{NestedMeta, Path}; use syn::{NestedMeta, Path};
use proc_macro2::TokenStream;
use crate::module::add_fn_to_module;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Argument { 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(); let mut name_attrs = Vec::new();
// Using retain will extract all name attributes from the attribute list // Using retain will extract all name attributes from the attribute list
attrs.retain(|attr| { attrs.retain(|attr| match attr.parse_meta() {
match attr.parse_meta() { Ok(syn::Meta::NameValue(ref nv)) if nv.path.is_ident("name") => {
Ok(syn::Meta::NameValue(ref nv)) if nv.path.is_ident("name") => { name_attrs.push((nv.lit.clone(), attr.span()));
name_attrs.push((nv.lit.clone(), attr.span())); false
false
}
_ => true
} }
_ => true,
}); });
let mut name = None; let mut name = None;
for (lit, span) in name_attrs { for (lit, span) in name_attrs {
if name.is_some() { 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 { 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. // This span is the whole attribute span, which is nicer for reporting errors.
ident.set_span(span); ident.set_span(span);
Some(ident) 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",
))
}
}; };
} }

View file

@ -73,7 +73,7 @@ struct RawIdents {}
#[pymethods] #[pymethods]
impl RawIdents { impl RawIdents {
fn r#fn(&self) { } fn r#fn(&self) {}
} }
#[test] #[test]