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
//
// Copyright (c) 2015-2019 Plausible Labs Cooperative, Inc.
// All rights reserved.
//
// HList macro implementations based on:
// https://github.com/epsilonz/shoggoth.rs
//
//
// Scala-style for/yield macros
//
/// Scala-like for-comprehension macro.
///
/// # Examples
///
/// ```
/// use rcodec::codec::*;
///
/// # fn main() {
/// let x = forcomp!({
/// foo <- Some(1u8);
/// bar <- None;
/// } yield { foo + bar });
/// assert!(x.is_none());
/// # }
/// ```
///
/// This is equivalent to:
///
/// ```
/// let x = Some(1u8).and_then(|foo| {
/// None.map(|bar| {
/// foo + bar
/// })
/// });
/// assert!(x.is_none());
/// ```
yield $yld:block } => ;
}
=> ;
=> ;
}
//
// ByteVector-related macros
//
/// Creates a new `ByteVector` from the given `u8` values.
///
/// # Examples
///
/// ```
/// use rcodec::byte_vector;
///
/// # fn main() {
/// let bv = byte_vector!(1, 2, 3, 4);
/// assert_eq!(bv, byte_vector::from_vec(vec!(1, 2, 3, 4)));
/// # }
/// ```
=> ;
}
//
// Codec-related macros
//
/// Converts an `HList` of `Codec`s into a `Codec` that operates on an `HList` of values.
///
/// Note that we require braces around each element so that we have more freedom with operators.
/// Rust macro rules state that simple exprs (without the braces) can only be followed by
/// `=> , ;` whereas blocks (with the braces) can be followed by any token like `>>` or `::`.
///
/// # Examples
///
/// ```
/// use pl_hlist::*;
/// use rcodec::{byte_vector, hcodec};
/// use rcodec::codec::*;
///
/// # fn main() {
/// let c = byte_vector!(0xCA, 0xFE);
/// let codec = hcodec!(
/// { "magic" => constant(&c) } >>
/// { "field1" => uint8 } ::
/// { "field2" => uint8 }
/// );
///
/// let bytes = byte_vector!(0xCA, 0xFE, 0x01, 0x02);
/// let decoded = codec.decode(&bytes).unwrap().value;
/// assert_eq!(decoded, hlist!(1, 2));
/// # }
/// ```
=> ;
=> ;
=> ;
=> ;
=> ;
}
=> ;
=> ;
}
/// Shorthand for creating a `Codec` for a struct.
///
/// The given struct must support `HList` conversions, either by using the `HListSupport` attribute
/// or by manually implementing the `FromHList` and `ToHList` traits.
///
/// # Examples
///
/// ```
/// use pl_hlist::*;
/// use rcodec::{byte_vector, struct_codec};
/// use rcodec::codec::*;
///
/// #[derive(Debug, PartialEq, Eq, HListSupport)]
/// pub struct Header {
/// foo: u8,
/// bar: u32
/// }
///
/// # fn main() {
/// let magic = byte_vector!(0xCA, 0xFE);
/// let header_codec = struct_codec!(
/// Header from
/// { "magic" => constant(&magic) } >>
/// { "foo" => uint8 } ::
/// { "junk" => ignore(2) } >>
/// { "bar" => uint32 }
/// );
///
/// let bytes = byte_vector!(0xCA, 0xFE, 0x07, 0xBE, 0xEF, 0x00, 0x00, 0x00, 0x06);
/// let header = header_codec.decode(&bytes).unwrap().value;
/// assert_eq!(header, Header { foo: 7, bar: 6 });
/// # }
/// ```
=> ;
}
/// Defines a struct that has derived impls for some common traits along with implementations
/// of the `FromHList` and `ToHList` traits, taking all fields into account.
///
/// # Examples
///
/// ```
/// use pl_hlist::*;
/// use rcodec::*;
///
/// record_struct!(
/// TestStruct,
/// foo: u8,
/// bar: u32
/// );
///
/// # fn main() {
/// let hlist = hlist!(7u8, 666u32);
/// let s = TestStruct::from_hlist(hlist);
/// assert_eq!(s, TestStruct { foo: 7, bar: 666 });
/// # }
/// ```
=>