Skip to content

Commit 0032508

Browse files
committed
Format code
1 parent 4b18830 commit 0032508

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

pyo3-derive-backend/src/method.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2017-present PyO3 Project and Contributors
22

33
use crate::pyfunction::Argument;
4-
use crate::pyfunction::{PyFunctionAttr, parse_name_attribute};
4+
use crate::pyfunction::{parse_name_attribute, PyFunctionAttr};
55
use crate::utils;
66
use proc_macro2::TokenStream;
77
use quote::quote;
@@ -61,8 +61,11 @@ impl<'a> FnSpec<'a> {
6161
allow_custom_name: bool,
6262
) -> syn::Result<FnSpec<'a>> {
6363
let name = &sig.ident;
64-
let MethodAttributes { ty: mut fn_type, args: fn_attrs, mut python_name } =
65-
parse_method_attributes(meth_attrs, allow_custom_name)?;
64+
let MethodAttributes {
65+
ty: mut fn_type,
66+
args: fn_attrs,
67+
mut python_name,
68+
} = parse_method_attributes(meth_attrs, allow_custom_name)?;
6669

6770
let mut has_self = false;
6871
let mut arguments = Vec::new();
@@ -346,7 +349,7 @@ pub fn check_arg_ty_and_optional<'a>(
346349
struct MethodAttributes {
347350
ty: FnType,
348351
args: Vec<Argument>,
349-
python_name: Option<syn::Ident>
352+
python_name: Option<syn::Ident>,
350353
}
351354

352355
fn parse_method_attributes(
@@ -477,36 +480,47 @@ fn parse_method_attributes(
477480
property_name
478481
};
479482

480-
Ok(MethodAttributes { ty, args, python_name })
483+
Ok(MethodAttributes {
484+
ty,
485+
args,
486+
python_name,
487+
})
481488
}
482489

483490
fn parse_method_name_attribute(
484491
ty: &FnType,
485492
attrs: &mut Vec<syn::Attribute>,
486-
property_name: Option<syn::Ident>
493+
property_name: Option<syn::Ident>,
487494
) -> syn::Result<Option<syn::Ident>> {
488-
489495
let name = parse_name_attribute(attrs)?;
490496

491497
// Reject some invalid combinations
492498
if let Some(name) = &name {
493499
match ty {
494-
FnType::FnNew => return Err(syn::Error::new_spanned(
495-
name,
496-
"name can not be specified with #[new]",
497-
)),
498-
FnType::FnCall => return Err(syn::Error::new_spanned(
499-
name,
500-
"name can not be specified with #[call]",
501-
)),
502-
FnType::Getter => return Err(syn::Error::new_spanned(
503-
name,
504-
"name can not be specified for getter",
505-
)),
506-
FnType::Setter => return Err(syn::Error::new_spanned(
507-
name,
508-
"name can not be specified for setter",
509-
)),
500+
FnType::FnNew => {
501+
return Err(syn::Error::new_spanned(
502+
name,
503+
"name can not be specified with #[new]",
504+
))
505+
}
506+
FnType::FnCall => {
507+
return Err(syn::Error::new_spanned(
508+
name,
509+
"name can not be specified with #[call]",
510+
))
511+
}
512+
FnType::Getter => {
513+
return Err(syn::Error::new_spanned(
514+
name,
515+
"name can not be specified for getter",
516+
))
517+
}
518+
FnType::Setter => {
519+
return Err(syn::Error::new_spanned(
520+
name,
521+
"name can not be specified for setter",
522+
))
523+
}
510524
_ => {}
511525
}
512526
}

pyo3-derive-backend/src/pyfunction.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright (c) 2017-present PyO3 Project and Contributors
22

3+
use crate::module::add_fn_to_module;
4+
use proc_macro2::TokenStream;
35
use syn::ext::IdentExt;
46
use syn::parse::ParseBuffer;
57
use syn::punctuated::Punctuated;
68
use syn::spanned::Spanned;
79
use syn::{NestedMeta, Path};
8-
use proc_macro2::TokenStream;
9-
use crate::module::add_fn_to_module;
1010

1111
#[derive(Debug, Clone, PartialEq)]
1212
pub enum Argument {
@@ -201,21 +201,22 @@ pub fn parse_name_attribute(attrs: &mut Vec<syn::Attribute>) -> syn::Result<Opti
201201
let mut name_attrs = Vec::new();
202202

203203
// Using retain will extract all name attributes from the attribute list
204-
attrs.retain(|attr| {
205-
match attr.parse_meta() {
206-
Ok(syn::Meta::NameValue(ref nv)) if nv.path.is_ident("name") => {
207-
name_attrs.push((nv.lit.clone(), attr.span()));
208-
false
209-
}
210-
_ => true
204+
attrs.retain(|attr| match attr.parse_meta() {
205+
Ok(syn::Meta::NameValue(ref nv)) if nv.path.is_ident("name") => {
206+
name_attrs.push((nv.lit.clone(), attr.span()));
207+
false
211208
}
209+
_ => true,
212210
});
213211

214212
let mut name = None;
215213

216214
for (lit, span) in name_attrs {
217215
if name.is_some() {
218-
return Err(syn::Error::new(span, "#[name] can not be specified multiple times"))
216+
return Err(syn::Error::new(
217+
span,
218+
"#[name] can not be specified multiple times",
219+
));
219220
}
220221

221222
name = match lit {
@@ -224,8 +225,13 @@ pub fn parse_name_attribute(attrs: &mut Vec<syn::Attribute>) -> syn::Result<Opti
224225
// This span is the whole attribute span, which is nicer for reporting errors.
225226
ident.set_span(span);
226227
Some(ident)
227-
},
228-
_ => return Err(syn::Error::new(span, "Expected string literal for #[name] argument"))
228+
}
229+
_ => {
230+
return Err(syn::Error::new(
231+
span,
232+
"Expected string literal for #[name] argument",
233+
))
234+
}
229235
};
230236
}
231237

tests/test_class_basics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct RawIdents {}
7373

7474
#[pymethods]
7575
impl RawIdents {
76-
fn r#fn(&self) { }
76+
fn r#fn(&self) {}
7777
}
7878

7979
#[test]

0 commit comments

Comments
 (0)