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
346
347
348
349
350
351
352
353
354
355
//! Derive attributes for [`ethercrab-wire`].
//!
//! # Experimental
//!
//! This crate is in its early stages and may contain bugs or publish breaking changes at any time.
//! It is in use by [`ethercrab`] and is well exercised there, but please use with caution in your
//! own code.
//!
//! These derives support both structs with bit- and (multi)byte-sized fields for structs, as well
//! as enums with optional catch-all variant.
//!
//! # Supported attributes
//!
//! ## Structs
//!
//! - `#[wire(bits = N)]` OR `#[wire(bytes = N)]`
//!
//! The size of this struct when packed on the wire. These attributes may not be present at the
//! same time.
//!
//! ## Struct fields
//!
//! - `#[wire(bits = N)]` OR `#[wire(bytes = N)]`
//!
//! How many bytes this field consumes on the wire. These attributes may not be present at the
//! same time.
//!
//! All builtin `u*` and `i*` types do not need this attribute by default; their size is computed
//! automatically if it's omitted. If a different bit width is desired, the `#[wire]` attribute is
//! required.
//!
//! - `#[wire(pre_skip = N)]` OR `#[wire(pre_skip_bytes = N)]`
//!
//! Skip one or more whole bytes before or after this field in the packed representation.
//!
//! - `#[wire(post_skip = N)]` OR `#[wire(post_skip_bytes = N)]`
//!
//! How many bits or bytes to skip in the raw data **after** this field.
//!
//! These attributes are only applicable to fields that are less than 8 bits wide.
//!
//! ## Enums
//!
//! Enums must have a `#[repr()]` attribute, as well as implement the `Copy` trait.
//!
//! ## Enum discriminants
//!
//! Enum discriminants may not contain fields.
//!
//! - `#[wire(alternatives = [])]`
//!
//! A discriminant with this attribute will be parsed successfully if either its direct value or
//! any of the listed alternatives are found in the input data.
//!
//! The discriminant value is used when packing _to_ the wire.
//!
//! - `#[wire(catch_all)]`
//!
//! Apply this once to a discriminant with a single unnamed field the same type as the enum's
//! `#[repr()]` to catch any unrecognised values.
//!
//! # Examples
//!
//! ## A struct with both bit fields and multi-byte fields.
//!
//! ```rust
//! #[derive(ethercrab_wire::EtherCrabWireReadWrite)]
//! #[wire(bytes = 4)]
//! struct Mixed {
//! #[wire(bits = 1)]
//! one_bit: u8,
//! #[wire(bits = 2)]
//! two_bits: u8,
//!
//! // Fields that are 8 bits or larger must be byte aligned, so we skip the two remaining bits
//! // of the previous byte with `post_skip`.
//! #[wire(bits = 3, post_skip = 2)]
//! three_bits: u8,
//!
//! /// Whole `u8`
//! // #[wire(bytes = 1)] Attribute not required as it is the default width of a `u8`
//! one_byte: u8,
//!
//! /// Whole `u16`
//! // #[wire(bytes = 2)] Attribute not required as it is the default width of a `u16`
//! one_word: u16,
//! }
//! ```
//!
//! ## Generic structs
//!
//! Structs can hold generic fields as long as the generic parameters implement one or more
//! `EtherCrabWire*` traits as appropriate.
//!
//! <section class="warning">
//!
//! It is the end user's responsibility to ensure the generic is of the correct size compared to the
//! `#[wire(bits|bytes)]` attribute on the containing struct.
//!
//! </section>
//!
//! ```rust
//! // Example value abstraction. NOTE: This byte size MUST be the same as the `T` definition in
//! // `AnalogInput`
//! #[derive(ethercrab_wire::EtherCrabWireRead)]
//! #[wire(bytes = 2)]
//! struct Value {
//! #[wire(bytes = 2)]
//! raw: u16
//! };
//!
//! /// Example: Status word for Beckhoff EL31xx devices and others.
//! #[derive(ethercrab_wire::EtherCrabWireRead)]
//! #[wire(bytes = 4)]
//! pub struct AnalogInput<T: ethercrab_wire::EtherCrabWireRead> {
//! #[wire(bits = 1)]
//! underrange: bool,
//! #[wire(bits = 1)]
//! overrange: bool,
//! #[wire(bits = 2)]
//! limit1: u8,
//! #[wire(bits = 2)]
//! limit2: u8,
//! #[wire(bits = 1)]
//! error: bool,
//! #[wire(pre_skip = 6, bits = 1)]
//! sync_error: bool,
//! #[wire(bits = 1)]
//! tx_pdo_bad: bool,
//! #[wire(bits = 1)]
//! tx_pdo_toggle: bool,
//! #[wire(bits = 16)]
//! value: T, // <-- generic field
//! }
//!
//! type IntegerAnalog = AnalogInput<Value>;
//! ```
//!
//! ## Enum with catch all discriminant and alternatives
//!
//! ```rust
//! # use ethercrab_wire::EtherCrabWireRead;
//! # #[derive(PartialEq, Debug)]
//! #[derive(Copy, Clone, ethercrab_wire::EtherCrabWireReadWrite)]
//! #[repr(u8)]
//! enum OneByte {
//! Foo = 0x01,
//! #[wire(alternatives = [ 3, 4, 5, 6 ])]
//! Bar = 0x02,
//! Baz = 0x07,
//! Quux = 0xab,
//! #[wire(catch_all)]
//! Unknown(u8),
//! }
//!
//! // Normal discriminant
//! assert_eq!(OneByte::unpack_from_slice(&[0x07]), Ok(OneByte::Baz));
//!
//! // Alternative value for `Bar`
//! assert_eq!(OneByte::unpack_from_slice(&[0x05]), Ok(OneByte::Bar));
//!
//! // Catch all
//! assert_eq!(OneByte::unpack_from_slice(&[0xaa]), Ok(OneByte::Unknown(0xaa)));
//! ```
//!
//! # Struct field alignment
//!
//! Struct fields of 1 byte or more MUST be byte-aligned. For example, the following struct will be
//! rejected due to `bar` being 5 bits "early":
//!
//! ```rust,compile_fail
//! #[derive(ethercrab_wire::EtherCrabWireReadWrite)]
//! #[wire(bytes = 2)]
//! struct Broken {
//! #[wire(bits = 3)]
//! foo: u8,
//!
//! // There are 5 bits here unaccounted for
//!
//! #[wire(bytes = 1)]
//! bar: u8,
//! }
//! ```
//!
//! This can easily be fixed by using the `pre_skip` or `post_skip` attributes to realign the next
//! field to 8 bits (or skip whole bytes of the input data):
//!
//! ```rust
//! #[derive(ethercrab_wire::EtherCrabWireReadWrite)]
//! #[wire(bytes = 2)]
//! struct Fixed {
//! #[wire(bits = 3, post_skip = 5)]
//! foo: u8,
//! #[wire(bytes = 1)]
//! bar: u8,
//! }
//! ```
//!
//! A field in the middle of a byte can be written as such, maintaining 8 bit alignment:
//!
//! ```rust
//! #[derive(ethercrab_wire::EtherCrabWireReadWrite)]
//! #[wire(bytes = 1)]
//! struct Middle {
//! #[wire(pre_skip = 2, bits = 3, post_skip = 3)]
//! foo: u8,
//! }
//! ```
//!
//! [`ethercrab`]: https://docs.rs/ethercrab
//! [`ethercrab-wire`]: https://docs.rs/ethercrab-wire
use ;
use ;
use parse_enum;
use parse_struct;
use TokenStream;
use ;
/// Items that can be written to and read from the wire.
///
/// Please see the [crate documentation](index.html) for examples and supported
/// attributes.
///
/// For write-only items, see [`EtherCrabWireWrite`]. For read-only items, see
/// [`EtherCrabWireRead`].
/// Items that can only be read from the wire.
///
/// Please see the [crate documentation](index.html) for examples and supported attributes.
///
/// For read/write items, see [`EtherCrabWireReadWrite`]. For write-only items, see
/// [`EtherCrabWireWrite`].
/// Items that can only be written to the wire.
///
/// Please see the [crate documentation](index.html) for examples and supported attributes.
///
/// For read/write items, see [`EtherCrabWireReadWrite`]. For read-only items, see
/// [`EtherCrabWireRead`].