pyo3_stub_gen_derive/
lib.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
mod gen_stub;

use proc_macro::TokenStream;

/// Embed metadata for Python stub file generation for `#[pyclass]` macro
///
/// ```
/// #[pyo3_stub_gen_derive::gen_stub_pyclass]
/// #[pyo3::pyclass(mapping, module = "my_module", name = "Placeholder")]
/// #[derive(Debug, Clone)]
/// pub struct PyPlaceholder {
///     #[pyo3(get)]
///     pub name: String,
///     #[pyo3(get)]
///     pub ndim: usize,
///     #[pyo3(get)]
///     pub description: Option<String>,
///     pub custom_latex: Option<String>,
/// }
/// ```
#[proc_macro_attribute]
pub fn gen_stub_pyclass(_attr: TokenStream, item: TokenStream) -> TokenStream {
    gen_stub::pyclass(item.into())
        .unwrap_or_else(|err| err.to_compile_error())
        .into()
}

/// Embed metadata for Python stub file generation for `#[pyclass]` macro with enum
///
/// ```
/// #[pyo3_stub_gen_derive::gen_stub_pyclass_enum]
/// #[pyo3::pyclass(module = "my_module", name = "DataType")]
/// #[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// pub enum PyDataType {
///     #[pyo3(name = "FLOAT")]
///     Float,
///     #[pyo3(name = "INTEGER")]
///     Integer,
/// }
/// ```
#[proc_macro_attribute]
pub fn gen_stub_pyclass_enum(_attr: TokenStream, item: TokenStream) -> TokenStream {
    gen_stub::pyclass_enum(item.into())
        .unwrap_or_else(|err| err.to_compile_error())
        .into()
}

/// Embed metadata for Python stub file generation for `#[pymethods]` macro
///
/// ```
/// #[pyo3_stub_gen_derive::gen_stub_pyclass]
/// #[pyo3::pyclass]
/// struct A {}
///
/// #[pyo3_stub_gen_derive::gen_stub_pymethods]
/// #[pyo3::pymethods]
/// impl A {
///     #[getter]
///     fn f(&self) -> Vec<u32> {
///        todo!()
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn gen_stub_pymethods(_attr: TokenStream, item: TokenStream) -> TokenStream {
    gen_stub::pymethods(item.into())
        .unwrap_or_else(|err| err.to_compile_error())
        .into()
}

/// Embed metadata for Python stub file generation for `#[pyfunction]` macro
///
/// ```
/// #[pyo3_stub_gen_derive::gen_stub_pyfunction]
/// #[pyo3::pyfunction]
/// #[pyo3(name = "is_odd")]
/// pub fn is_odd(x: u32) -> bool {
///     todo!()
/// }
/// ```
///
/// The function attributed by `#[gen_stub_pyfunction]` will be appended to default stub file.
/// If you want to append this function to another module, add `module` attribute.
///
/// ```
/// #[pyo3_stub_gen_derive::gen_stub_pyfunction(module = "my_module.experimental")]
/// #[pyo3::pyfunction]
/// #[pyo3(name = "is_odd")]
/// pub fn is_odd(x: u32) -> bool {
///     todo!()
/// }
/// ```
#[proc_macro_attribute]
pub fn gen_stub_pyfunction(attr: TokenStream, item: TokenStream) -> TokenStream {
    gen_stub::pyfunction(attr.into(), item.into())
        .unwrap_or_else(|err| err.to_compile_error())
        .into()
}