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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! MAVSpec Rust Procedural Macros
//!
//! <span style="font-size:24px">[πΊπ¦](https://mavka.gitlab.io/home/a_note_on_the_war_in_ukraine/)</span>
//! [](https://gitlab.com/mavka/libs/mavspec)
//! [](https://crates.io/crates/mavspec)
//! [](https://docs.rs/mavspec/latest/mavspec/)
//! [](https://gitlab.com/mavka/libs/mavspec/-/issues/)
//!
//! This crate provides proc macros for [MAVLink](https://mavlink.io/en/) MAVLink entities generated by
//! [MAVSpec](https://gitlab.com/mavka/libs/mavspec).
//!
//! # Usage
//!
//! In most cases marcos defined here will be used by autogenerated code. However, it is possible to use them for
//! creating custom MAVLink entities.
//!
//! Custom MAVLink message:
//!
//! ```rust
//! use mavspec::rust::derive::Message;
//!
//! #[derive(Clone, Debug, Message)]
//! #[message_id(255)] // Specify message ID
//! struct CustomMessage {
//! scalar_u8: u8,
//! array_u8_4: [u8; 4],
//! #[extension] // This marks an extension fields
//! ext_array_u32_4: [u32; 4],
//! }
//! ```
//!
//! Custom MAVLink enum:
//!
//! ```rust
//! use mavspec::rust::derive::Enum;
//!
//! #[repr(u8)] // Define enum representation
//! #[derive(Clone, Copy, Debug, Default, Enum)]
//! enum Variants {
//! #[default]
//! OptionA = 0,
//! OptionB = 1,
//! OptionC = 2,
//! }
//! ```
//!
//! Custom MAVLink dialect:
//!
//! ```rust
//! use mavspec::rust::derive::{Dialect, Message};
//!
//! #[derive(Clone, Debug, Message)]
//! #[message_id(255)] // Specify message ID
//! struct CustomMessage {
//! scalar_u8: u8,
//! array_u8_4: [u8; 4],
//! #[extension] // This marks an extension fields
//! ext_array_u32_4: [u32; 4],
//! }
//!
//! #[derive(Clone, Debug, Dialect)]
//! #[dialect(1099)] // Specify unique dialect `ID`
//! #[version(99)] // Specify dialect version
//! enum CustomDialect {
//! Custom(CustomMessage),
//! }
//! ```
use DeriveInput;
pub
pub
pub
pub
pub
pub
/// Derive MAVLink message from struct.
///
/// # Usage
///
/// Basic usage:
///
/// ```rust
/// use mavspec::rust::derive::Message;
///
/// #[derive(Clone, Debug, Message)]
/// #[message_id(255)] // Specify message ID
/// struct CustomMessage {
/// scalar_u8: u8,
/// array_u8_4: [u8; 4],
/// #[extension] // This marks an extension fields
/// ext_array_u32_4: [u32; 4],
/// }
/// ```
///
/// It is possible to override `CRC_EXTRA` byte using `#[crc_extra(32)]` attribute:
///
/// ```rust
/// use mavspec::rust::derive::Message;
///
/// #[derive(Clone, Debug, Message)]
/// #[message_id(255)] // Specify message ID
/// #[crc_extra(32)] // Set `CRC_EXTRA` byte
/// struct CustomMessage {
/// scalar_u8: u8,
/// array_u8_4: [u8; 4],
/// }
///
/// assert_eq!(CustomMessage::crc_extra(), 32);
/// ```
///
/// Auto-calculated `CRC_EXTRA` is not supported for arrays with lengths specified by constants. The
/// following won't compile:
///
/// ```rust,compile_fail
/// use mavspec::rust::derive::Message;
///
/// const FOUR: usize = 4;
///
/// #[derive(Clone, Debug, Message)]
/// #[message_id(255)]
/// struct CustomMessage {
/// scalar_u8: u8,
/// array_u8_4: [u8; FOUR], // Can't calculate `CRC_EXTRA`
/// }
/// ```
///
/// ## Enums
///
/// It is possible to use custom enums as message fields types with `#[derive(Enum)]`. Each enum should have a numeric
/// representation and implement [`Default`] trait.
///
/// Fields with custom types should be attributed with `base_type` attribute (i.e. `#[base_type(u16)]`) that specifies
/// actual base type of a field. For arrays base type is a type of their elements.
///
/// It is possible to use larger base types with smaller enum representation types. In such case you have to specify the
/// representation type of an enum with `repr_type` attribute (i.e. `#[repr_type(u8)]`).
///
/// It is forbidden to use larger representation types with smaller base types.
///
/// ```rust
/// use mavspec::rust::derive::{Enum, Message};
///
/// #[repr(u8)] // Define enum representation
/// #[derive(Clone, Copy, Debug, Default, Enum)]
/// enum Variants {
/// #[default]
/// OptionA = 0,
/// OptionB = 1,
/// OptionC = 2,
/// }
///
/// #[derive(Clone, Debug, Message)]
/// #[message_id(255)]
/// struct CustomMessage {
/// #[base_type(u8)]
/// scalar_u8: Variants,
///
/// #[base_type(u8)]
/// array_u8_4: [Variants; 4],
///
/// #[base_type(u16)]
/// #[repr_type(u8)]
/// large_scalar_u16: Variants,
///
/// #[base_type(u16)]
/// #[repr_type(u8)]
/// large_array_u16_4: [Variants; 4],
/// }
/// ```
///
/// ## Bitmasks
///
/// For bitmasks you can use native [bitflags](https://crates.io/crates/bitflags) flags. In such case you have to
/// attribute corresponding fields with `#[bitmask]` attribute. In the same spirit as for [enums](#enums) you have to
/// set `base_type` type and `repr_type` attributes.
///
/// ```rust
/// use mavspec::rust::derive::Message;
/// use bitflags::bitflags;
///
/// bitflags! {
/// #[derive(Clone, Copy, Debug, Default)]
/// struct Flags: u8 {
/// const FLAG_8 = 1;
/// const FLAG_7 = 1 << 1;
/// const FLAG_6 = 1 << 2;
/// const FLAG_5 = 1 << 3;
/// const FLAG_4 = 1 << 4;
/// const FLAG_3 = 1 << 5;
/// const FLAG_2 = 1 << 6;
/// const FLAG_1 = 1 << 7;
/// }
/// }
///
/// #[derive(Clone, Debug, Message)]
/// #[message_id(255)]
/// struct CustomMessage {
/// #[bitmask]
/// #[base_type(u8)]
/// scalar_u8: Flags,
///
/// #[bitmask]
/// #[base_type(u8)]
/// array_u8_4: [Flags; 4],
///
/// #[bitmask]
/// #[base_type(u16)]
/// #[repr_type(u8)]
/// large_scalar_u16: Flags,
///
/// #[bitmask]
/// #[base_type(u16)]
/// #[repr_type(u8)]
/// large_array_u16_4: [Flags; 4],
/// }
/// ```
/// Derive MAVLink enum from enum.
///
/// # Usage
///
/// Basic usage:
///
/// ```rust
/// use mavspec::rust::derive::Enum;
///
/// #[repr(u8)]
/// #[derive(Clone, Copy, Debug, Default, Enum)]
/// enum CustomEnum {
/// #[default]
/// OptionA = 0,
/// OptionB = 1,
/// OptionC = 2,
/// }
/// ```
///
/// It is possible to use constants in variant discriminants:
///
/// ```rust
/// use mavspec::rust::derive::Enum;
///
/// const ONE: u8 = 1;
/// const TWO: u8 = 2;
///
/// #[derive(Enum)]
/// #[repr(u8)]
/// #[derive(Copy, Clone, Debug, Default)]
/// enum CustomEnum {
/// #[default]
/// OptionA = 0,
/// OptionB = ONE, // Constants are supported
/// OptionC = TWO, //
/// }
/// ```
///
/// Derive MAVLink dialect from enum.
///
/// # Usage
///
/// Create a simple ad-hoc dialect for meaningless smalltalk:
///
/// ```rust
/// use mavspec::rust::derive::{Dialect, Enum, Message};
///
/// #[repr(u8)]
/// #[derive(Copy, Clone, Debug, Default, Enum)]
/// enum Mood {
/// #[default]
/// Serious = 0,
/// Grumpy = 1,
/// Delighted = 2,
/// Confused = 3,
/// }
///
/// #[derive(Clone, Debug, Message)]
/// #[message_id(42)]
/// struct Howdy {
/// #[base_type(u8)]
/// mood: Mood,
/// }
///
/// #[derive(Clone, Debug, Message)]
/// #[message_id(43)]
/// struct FineAndYou {
/// #[base_type(u8)]
/// mood: Mood,
/// }
///
/// #[derive(Clone, Debug, Dialect)]
/// #[dialect(1099)]
/// #[version(99)]
/// enum SmallTalk {
/// Howdy(Howdy),
/// FineAndYou(FineAndYou),
/// }
/// ```