pyo3_stub_gen_derive/gen_stub/
pymethods.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens, TokenStreamExt};
use syn::{Error, ImplItem, ItemImpl, Result, Type};

use super::{quote_option, MemberInfo, MethodInfo, NewInfo};

pub struct PyMethodsInfo {
    struct_id: Type,
    new: Option<NewInfo>,
    getters: Vec<MemberInfo>,
    methods: Vec<MethodInfo>,
}

impl TryFrom<ItemImpl> for PyMethodsInfo {
    type Error = Error;
    fn try_from(item: ItemImpl) -> Result<Self> {
        let struct_id = *item.self_ty.clone();
        let mut new = None;
        let mut getters = Vec::new();
        let mut methods = Vec::new();

        for inner in item.items {
            if let ImplItem::Fn(item_fn) = inner {
                if NewInfo::is_candidate(&item_fn)? {
                    new = Some(NewInfo::try_from(item_fn)?);
                } else if MemberInfo::is_candidate_item(&item_fn)? {
                    getters.push(MemberInfo::try_from(item_fn)?);
                } else {
                    let mut method = MethodInfo::try_from(item_fn)?;
                    method.replace_self(&item.self_ty);
                    methods.push(method);
                }
            }
        }
        Ok(Self {
            struct_id,
            new,
            getters,
            methods,
        })
    }
}

impl ToTokens for PyMethodsInfo {
    fn to_tokens(&self, tokens: &mut TokenStream2) {
        let Self {
            struct_id,
            new,
            getters,
            methods,
        } = self;
        let new_tt = quote_option(new);
        tokens.append_all(quote! {
            ::pyo3_stub_gen::type_info::PyMethodsInfo {
                struct_id: std::any::TypeId::of::<#struct_id>,
                new: #new_tt,
                getters: &[ #(#getters),* ],
                methods: &[ #(#methods),* ],
            }
        })
    }
}