1 | use super::{parse_args, parse_pyo3_attrs, ArgInfo, ArgsWithSignature, Attr, Signature}; |
2 | |
3 | use proc_macro2::TokenStream as TokenStream2; |
4 | use quote::{quote, ToTokens, TokenStreamExt}; |
5 | use syn::{Error, ImplItemFn, Result}; |
6 | |
7 | #[derive (Debug)] |
8 | pub struct NewInfo { |
9 | args: Vec<ArgInfo>, |
10 | sig: Option<Signature>, |
11 | } |
12 | |
13 | impl NewInfo { |
14 | pub fn is_candidate(item: &ImplItemFn) -> Result<bool> { |
15 | let attrs: Vec = parse_pyo3_attrs(&item.attrs)?; |
16 | Ok(attrs.iter().any(|attr: &Attr| matches!(attr, Attr::New))) |
17 | } |
18 | } |
19 | |
20 | impl TryFrom<ImplItemFn> for NewInfo { |
21 | type Error = Error; |
22 | fn try_from(item: ImplItemFn) -> Result<Self> { |
23 | assert!(Self::is_candidate(&item)?); |
24 | let ImplItemFn { attrs: Vec, sig: Signature, .. } = item; |
25 | let attrs: Vec = parse_pyo3_attrs(&attrs)?; |
26 | let mut new_sig: Option = None; |
27 | for attr: Attr in attrs { |
28 | if let Attr::Signature(text_sig: Signature) = attr { |
29 | new_sig = Some(text_sig); |
30 | } |
31 | } |
32 | Ok(NewInfo { |
33 | args: parse_args(iter:sig.inputs)?, |
34 | sig: new_sig, |
35 | }) |
36 | } |
37 | } |
38 | |
39 | impl ToTokens for NewInfo { |
40 | fn to_tokens(&self, tokens: &mut TokenStream2) { |
41 | let Self { args: &Vec, sig: &Option } = self; |
42 | let args_with_sig: ArgsWithSignature<'_> = ArgsWithSignature { args, sig }; |
43 | tokens.append_all(iter:quote! { |
44 | ::pyo3_stub_gen::type_info::NewInfo { |
45 | args: #args_with_sig, |
46 | } |
47 | }) |
48 | } |
49 | } |
50 | |