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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#[macro_export]
macro_rules! export_cpy {
    //(0) - Entry point of the macro.
    //      Matches the module name and block of items.
    //      It exports Python related modules only if required,
    //      see the examples.
    //      Each type have a well defined scope, in sections:
    //      (1) - Processing
    //      (2) - Structure
    //      (3) - Python Module Binding
    (mod $module_name:ident { $($item:tt)* }) => {
        export_cpy!(@process_item $($item)*);

        #[cfg(feature = "python")]
        #[pymodule]
        fn $module_name(_py: Python, m: &PyModule) -> PyResult<()> {
            export_cpy!(@add_py_binding m, $($item)*);
            Ok(())
        }
    };

    //(1) - This section defines the processing items patterns
    (@process_item) => {};
    (@process_item enum $comment:literal $name:ident { $($variant:ident,)* } $($rest:tt)*) => {
        export_cpy!(@generate_enum $comment $name { $($variant,)* });
        export_cpy!(@process_item $($rest)*);
    };
    (@process_item enum $name:ident { $($variant:ident,)* } $($rest:tt)*) => {
        export_cpy!(@generate_enum "No documentation" $name { $($variant,)* });
        export_cpy!(@process_item $($rest)*);
    };
    (@process_item struct $comment:literal $name:ident { $($field:ident : $ftype:ty,)* } $($rest:tt)*) => {
        export_cpy!(@generate_struct $comment $name { $($field : $ftype,)* });
        export_cpy!(@process_item $($rest)*);
    };
    (@process_item struct $name:ident { $($field:ident : $ftype:ty,)* } $($rest:tt)*) => {
        export_cpy!(@generate_struct "No documentation" $name { $($field : $ftype,)* });
        export_cpy!(@process_item $($rest)*);
    };
    (@process_item fn $comment:literal $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        export_cpy!(@generate_function $comment $name($($param : $ptype),*) $(-> $ret)? $body);
        export_cpy!(@process_item $($rest)*);
    };
    (@process_item fn $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        export_cpy!(@generate_function "No documentation" $name($($param : $ptype),*) $(-> $ret)? $body);
        export_cpy!(@process_item $($rest)*);
    };
    (@process_item fn_c $comment:literal $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        export_cpy!(@generate_c_function $comment $name($($param : $ptype),*) $(-> $ret)? $body);
        export_cpy!(@process_item $($rest)*);
    };
    (@process_item fn_c $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        export_cpy!(@generate_c_function "No documentation" $name($($param : $ptype),*) $(-> $ret)? $body);
        export_cpy!(@process_item $($rest)*);
    };
    (@process_item fn_py $comment:literal $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        export_cpy!(@generate_py_function $comment $name($($param : $ptype),*) $(-> $ret)? $body);
        export_cpy!(@process_item $($rest)*);
    };
    (@process_item fn_py $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        export_cpy!(@generate_py_function "No documentation" $name($($param : $ptype),*) $(-> $ret)? $body);
        export_cpy!(@process_item $($rest)*);
    };

    //(2) - This section defines the structure of the itens
    (@generate_enum $comment:literal $name:ident { $($variant:ident,)* }) => {
        #[doc = $comment]
        #[derive(Clone, Debug)]
        #[repr(C)]
        #[cfg_attr(feature = "python", pyo3::prelude::pyclass)]
        pub enum $name {
            $(
                $variant,
            )*
        }
    };
    (@generate_struct $comment:literal $name:ident { $($field:ident : $ftype:ty,)* }) => {
        #[doc = $comment]
        #[derive(Clone, Debug)]
        #[repr(C)]
        #[cfg_attr(feature = "python", pyo3::prelude::pyclass(get_all, set_all))]
        pub struct $name {
            $(
                pub $field: $ftype,
            )*
        }
    };
    (@generate_function $comment:literal $name:ident($($arg:ident: $arg_type:ty),*) $(-> $ret:ty)? $body:block) => {
        #[doc = $comment]
        #[no_mangle]
        #[cfg_attr(feature = "python", pyfunction)]
        pub extern "C" fn $name($($arg: $arg_type),*) $(-> $ret)?
            $body
    };
    (@generate_c_function $comment:literal $name:ident($($arg:ident: $arg_type:ty),*) $(-> $ret:ty)? $body:block) => {
        #[doc = $comment]
        #[no_mangle]
        #[cfg(not(feature = "python"))]
        pub extern "C" fn $name($($arg: $arg_type),*) $(-> $ret)?
            $body
    };
    (@generate_py_function $comment:literal $name:ident($($arg:ident: $arg_type:ty),*) $(-> $ret:ty)? $body:block) => {
        #[doc = $comment]
        #[cfg(feature = "python")]
        #[pyfunction]
        pub fn $name($($arg: $arg_type),*) $(-> $ret)?
            $body
    };

    //(3) - This section defines the bindings to be exported to Python module
    (@add_py_binding $m:ident,) => {};
    (@add_py_binding $m:ident, enum $name:ident { $($variant:ident,)* } $($rest:tt)*) => {
        $m.add_class::<$name>()?;
        export_cpy!(@add_py_binding $m, $($rest)*);
    };
    (@add_py_binding $m:ident, enum $comment:literal $name:ident { $($variant:ident,)* } $($rest:tt)*) => {
        $m.add_class::<$name>()?;
        export_cpy!(@add_py_binding $m, $($rest)*);
    };
    (@add_py_binding $m:ident, struct $name:ident { $($field:ident : $ftype:ty,)* } $($rest:tt)*) => {
        $m.add_class::<$name>()?;
        export_cpy!(@add_py_binding $m, $($rest)*);
    };
    (@add_py_binding $m:ident, struct $comment:literal $name:ident { $($field:ident : $ftype:ty,)* } $($rest:tt)*) => {
        $m.add_class::<$name>()?;
        export_cpy!(@add_py_binding $m, $($rest)*);
    };
    (@add_py_binding $m:ident, fn $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        $m.add_wrapped(wrap_pyfunction!($name))?;
        export_cpy!(@add_py_binding $m, $($rest)*);
    };
    (@add_py_binding $m:ident, fn $comment:literal $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        $m.add_wrapped(wrap_pyfunction!($name))?;
        export_cpy!(@add_py_binding $m, $($rest)*);
    };
    (@add_py_binding $m:ident, fn_c $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        export_cpy!(@add_py_binding $m, $($rest)*);
    };
    (@add_py_binding $m:ident, fn_c $comment:literal $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        export_cpy!(@add_py_binding $m, $($rest)*);
    };
    (@add_py_binding $m:ident, fn_py $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        $m.add_wrapped(wrap_pyfunction!($name))?;
        export_cpy!(@add_py_binding $m, $($rest)*);
    };
    (@add_py_binding $m:ident, fn_py $comment:literal $name:ident($($param:ident : $ptype:ty),*) $(-> $ret:ty)? $body:block $($rest:tt)*) => {
        $m.add_wrapped(wrap_pyfunction!($name))?;
        export_cpy!(@add_py_binding $m, $($rest)*);
    };
}