From e2ed1fcb9d5abc0f14014b9b155e032192a17413 Mon Sep 17 00:00:00 2001 From: Daniil Konovalenko Date: Tue, 9 Feb 2021 18:57:59 +0300 Subject: [PATCH] extract from_py_with parsing into its own module --- pyo3-macros-backend/src/attrs.rs | 27 ++++++++++++++++++++++++ pyo3-macros-backend/src/from_pyobject.rs | 20 +++++------------- pyo3-macros-backend/src/lib.rs | 1 + 3 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 pyo3-macros-backend/src/attrs.rs diff --git a/pyo3-macros-backend/src/attrs.rs b/pyo3-macros-backend/src/attrs.rs new file mode 100644 index 00000000000..d7cd1851fc1 --- /dev/null +++ b/pyo3-macros-backend/src/attrs.rs @@ -0,0 +1,27 @@ +use proc_macro2::TokenStream; +use quote::ToTokens; +use syn::spanned::Spanned; +use syn::{Meta, MetaNameValue, Result}; + +#[derive(Clone, Debug, PartialEq)] +pub struct FromPyWithAttribute(syn::ExprPath); + +impl FromPyWithAttribute { + pub fn from_meta(meta: Meta) -> Result { + if let Meta::NameValue(MetaNameValue { + lit: syn::Lit::Str(string_literal), + .. + }) = meta + { + let expr_path = string_literal.parse::()?; + return Ok(FromPyWithAttribute(expr_path)); + } + bail_spanned!(meta.span() => "expected a name-value: `pyo3(from_py_with = \"func\")` ") + } +} + +impl ToTokens for FromPyWithAttribute { + fn to_tokens(&self, tokens: &mut TokenStream) { + self.0.to_tokens(tokens) + } +} diff --git a/pyo3-macros-backend/src/from_pyobject.rs b/pyo3-macros-backend/src/from_pyobject.rs index 7781f8a14b5..97cef94b168 100644 --- a/pyo3-macros-backend/src/from_pyobject.rs +++ b/pyo3-macros-backend/src/from_pyobject.rs @@ -1,11 +1,9 @@ +use crate::attrs::FromPyWithAttribute; use proc_macro2::TokenStream; use quote::quote; use syn::punctuated::Punctuated; use syn::spanned::Spanned; -use syn::{ - parse_quote, Attribute, DataEnum, DeriveInput, Fields, Ident, Meta, MetaList, MetaNameValue, - Result, -}; +use syn::{parse_quote, Attribute, DataEnum, DeriveInput, Fields, Ident, Meta, MetaList, Result}; /// Describes derivation input of an enum. #[derive(Debug)] @@ -320,7 +318,7 @@ impl ContainerAttribute { #[derive(Clone, Debug)] struct FieldAttributes { getter: FieldGetter, - from_py_with: Option, + from_py_with: Option, } #[derive(Clone, Debug)] @@ -418,16 +416,8 @@ impl FieldAttributes { bail_spanned!(arg_list.span() => "expected a single literal argument"); } - fn from_py_with_arg(meta: Meta) -> syn::Result { - if let Meta::NameValue(MetaNameValue { - lit: syn::Lit::Str(string_literal), - .. - }) = meta - { - let expr_path = string_literal.parse::()?; - return Ok(expr_path); - } - bail_spanned!(meta.span() => "expected a name-value: `pyo3(from_py_with = \"func\")` ") + fn from_py_with_arg(meta: Meta) -> syn::Result { + FromPyWithAttribute::from_meta(meta) } } diff --git a/pyo3-macros-backend/src/lib.rs b/pyo3-macros-backend/src/lib.rs index 6b8af63d91a..9738e01f46a 100644 --- a/pyo3-macros-backend/src/lib.rs +++ b/pyo3-macros-backend/src/lib.rs @@ -7,6 +7,7 @@ #[macro_use] mod utils; +mod attrs; mod defs; mod from_pyobject; mod konst;