1 | mod gen_stub; |
2 | |
3 | use proc_macro::TokenStream; |
4 | |
5 | /// Embed metadata for Python stub file generation for `#[pyclass]` macro |
6 | /// |
7 | /// ``` |
8 | /// #[pyo3_stub_gen_derive::gen_stub_pyclass] |
9 | /// #[pyo3::pyclass(mapping, module = "my_module" , name = "Placeholder" )] |
10 | /// #[derive(Debug, Clone)] |
11 | /// pub struct PyPlaceholder { |
12 | /// #[pyo3(get)] |
13 | /// pub name: String, |
14 | /// #[pyo3(get)] |
15 | /// pub ndim: usize, |
16 | /// #[pyo3(get)] |
17 | /// pub description: Option<String>, |
18 | /// pub custom_latex: Option<String>, |
19 | /// } |
20 | /// ``` |
21 | #[proc_macro_attribute ] |
22 | pub fn gen_stub_pyclass (_attr: TokenStream, item: TokenStream) -> TokenStream { |
23 | gen_stubTokenStream::pyclass(item.into()) |
24 | .unwrap_or_else(|err: Error| err.to_compile_error()) |
25 | .into() |
26 | } |
27 | |
28 | /// Embed metadata for Python stub file generation for `#[pyclass]` macro with enum |
29 | /// |
30 | /// ``` |
31 | /// #[pyo3_stub_gen_derive::gen_stub_pyclass_enum] |
32 | /// #[pyo3::pyclass(module = "my_module" , name = "DataType" )] |
33 | /// #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
34 | /// pub enum PyDataType { |
35 | /// #[pyo3(name = "FLOAT" )] |
36 | /// Float, |
37 | /// #[pyo3(name = "INTEGER" )] |
38 | /// Integer, |
39 | /// } |
40 | /// ``` |
41 | #[proc_macro_attribute ] |
42 | pub fn gen_stub_pyclass_enum (_attr: TokenStream, item: TokenStream) -> TokenStream { |
43 | gen_stubTokenStream::pyclass_enum(item.into()) |
44 | .unwrap_or_else(|err: Error| err.to_compile_error()) |
45 | .into() |
46 | } |
47 | |
48 | /// Embed metadata for Python stub file generation for `#[pymethods]` macro |
49 | /// |
50 | /// ``` |
51 | /// #[pyo3_stub_gen_derive::gen_stub_pyclass] |
52 | /// #[pyo3::pyclass] |
53 | /// struct A {} |
54 | /// |
55 | /// #[pyo3_stub_gen_derive::gen_stub_pymethods] |
56 | /// #[pyo3::pymethods] |
57 | /// impl A { |
58 | /// #[getter] |
59 | /// fn f(&self) -> Vec<u32> { |
60 | /// todo!() |
61 | /// } |
62 | /// } |
63 | /// ``` |
64 | #[proc_macro_attribute ] |
65 | pub fn gen_stub_pymethods (_attr: TokenStream, item: TokenStream) -> TokenStream { |
66 | gen_stubTokenStream::pymethods(item.into()) |
67 | .unwrap_or_else(|err: Error| err.to_compile_error()) |
68 | .into() |
69 | } |
70 | |
71 | /// Embed metadata for Python stub file generation for `#[pyfunction]` macro |
72 | /// |
73 | /// ``` |
74 | /// #[pyo3_stub_gen_derive::gen_stub_pyfunction] |
75 | /// #[pyo3::pyfunction] |
76 | /// #[pyo3(name = "is_odd" )] |
77 | /// pub fn is_odd(x: u32) -> bool { |
78 | /// todo!() |
79 | /// } |
80 | /// ``` |
81 | /// |
82 | /// The function attributed by `#[gen_stub_pyfunction]` will be appended to default stub file. |
83 | /// If you want to append this function to another module, add `module` attribute. |
84 | /// |
85 | /// ``` |
86 | /// #[pyo3_stub_gen_derive::gen_stub_pyfunction(module = "my_module.experimental" )] |
87 | /// #[pyo3::pyfunction] |
88 | /// #[pyo3(name = "is_odd" )] |
89 | /// pub fn is_odd(x: u32) -> bool { |
90 | /// todo!() |
91 | /// } |
92 | /// ``` |
93 | #[proc_macro_attribute ] |
94 | pub fn gen_stub_pyfunction (attr: TokenStream, item: TokenStream) -> TokenStream { |
95 | gen_stubTokenStream::pyfunction(attr.into(), item.into()) |
96 | .unwrap_or_else(|err: Error| err.to_compile_error()) |
97 | .into() |
98 | } |
99 | |