micrortu_ie_representation_derive/lib.rs
1#![allow(clippy::redundant_closure_for_method_calls)]
2
3use proc_macro::TokenStream;
4
5mod bindings;
6mod config;
7mod finalize;
8mod register_block;
9mod state;
10
11/// Finalize the build process.
12/// That macro must be called at the end to embed metadata into the binary.
13/// It creates a link section "metadata" with json data of all registered blocks
14/// and exported symbol `COLLECTED_STRINGS` with all strings from the build.
15/// `BindingDefinition`'s `name_offset` and `name_len` are referencing `COLLECTED_STRINGS`.
16/// # Example
17/// ```rust
18/// finalize!();
19/// ```
20///
21#[proc_macro]
22pub fn finalize(_: TokenStream) -> TokenStream {
23 finalize::finalize()
24}
25
26/// Register block.
27/// That macro should be called for each block to register it.
28/// # Example
29/// ```rust
30/// register_block!(BlockType, BlockName, factory, init, step);
31/// ```
32#[proc_macro]
33pub fn register_block(input: TokenStream) -> TokenStream {
34 register_block::register_block(input)
35}
36
37/// Derive macro for `Config` trait.
38/// If block requires some configuration, it should be derived from `Config` trait.
39/// It requires type to be `AsBytes` and `FromBytes`. Firmware will pass slice
40/// of bytes and you should be able to call `from_bytes` method on it to get the
41/// configuration. For C code you should be able to cast a pointer to your struct.
42#[proc_macro_derive(Config, attributes(block_names))]
43pub fn derive_config(input: TokenStream) -> TokenStream {
44 config::derive_config(input)
45}
46
47/**
48# Macros for generating parser of arguments block requires.
49
50## Example
51
52```rust
53ports! {
54 #[block_names(block_name1, block_name2)]
55 pub struct Ports {
56 // parameter `a` has minimum size of 1 and unbouded maximum size, required
57 a: In 1,
58 // parameter `y` should have exactly 1 size, optional
59 y: InOut 1 1 ?,
60 // parameter `b` has minimum size of 2 and maximum size of 10, required
61 b: Out 2 10,
62 }
63}
64```
65
66Resulting struct would have fields with types from those:
67
68`GetSingleOptional`
69
70`GetSingle`
71
72`GetMultipleOptional`
73
74`GetMultiple`
75
76`SetSingleOptional`
77
78`SetSingle`
79
80`SetMultipleOptional`
81
82`SetMultiple`
83
84`GetSetSingleOptional`
85
86`GetSetSingle`
87
88`GetSetMultipleOptional`
89
90`GetSetMultiple`
91
92*/
93#[proc_macro]
94pub fn ports(input: TokenStream) -> TokenStream {
95 bindings::bindings(input, true)
96}
97
98/**
99# Macros for generating parser of arguments block requires.
100
101## Example
102
103```rust
104params! {
105 #[block_names(block_name1, block_name2)]
106 pub struct Params {
107 // parameter `a` has minimum size of 1 and unbouded maximum size, required
108 a: In 1,
109 // parameter `y` should have exactly 1 size, optional
110 y: InOut 1 1 ?,
111 // parameter `b` has minimum size of 2 and maximum size of 10, required
112 b: Out 2 10,
113 }
114}
115```
116
117Resulting struct would have fields with types from those:
118
119`GetSingleOptional`
120
121`GetSingle`
122
123`GetMultipleOptional`
124
125`GetMultiple`
126
127`SetSingleOptional`
128
129`SetSingle`
130
131`SetMultipleOptional`
132
133`SetMultiple`
134
135`GetSetSingleOptional`
136
137`GetSetSingle`
138
139`GetSetMultipleOptional`
140
141`GetSetMultiple`
142
143*/
144#[proc_macro]
145pub fn params(input: TokenStream) -> TokenStream {
146 bindings::bindings(input, false)
147}