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
// --------------------------------------------------
// local
// --------------------------------------------------
pub use *;
/// Encodes the value portion of a KLV field to owned stream-type `O`.
///
/// Decode counterpart: [`DecodeValue`](crate::traits::DecodeValue)
///
/// Trait for encoding ***data only*** to owned stream-type `O`, where `O` is an owned stream-type of [`winnow::stream::Stream`], with elements `T`.
///
/// ```text
/// This is what is encoded
/// vvvvvvvvvvvvvvvvvvvvvvvvvv
/// [ ... key ... | ... length ... | ..... value (self) ..... ]
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^
/// ```
///
/// ***This trait IS automatically implemented for structs deriving the [`tinyklv::Klv`](crate::Klv) trait, in which every field has an associated encoder for it's type. Otherwise, this trait CAN be implemented manually.***
///
/// Common stream types include `&[u8]` and `&str`, therefore the return type of encoding is likely an owned value like [`Vec<u8>`] or [`String`].
///
/// For custom encoding functions, ***no need to use this trait***. Instead, please ensure the functions signature matches the following:
///
/// ```rust ignore
/// fn encoder_fn_name(..) -> O;
/// ```
///
/// # Example
///
/// ```rust
/// use tinyklv::Klv;
/// use tinyklv::prelude::*;
/// use tinyklv::traits::EncodeValue;
///
/// struct InnerValue {}
///
/// fn ex01_encoder(input: &InnerValue) -> Vec<u8> {
/// return vec![0x65, 0x66, 0x67, 0x68];
/// }
///
/// fn ex02_encoder(input: &InnerValue) -> Vec<u8> {
/// return String::from("Y2K").into_bytes();
/// }
///
/// impl EncodeValue<Vec<u8>> for InnerValue {
/// fn encode_value(&self) -> Vec<u8> {
/// return String::from("KLV").to_lowercase().into_bytes();
/// }
/// }
///
/// #[derive(Klv)]
/// #[klv(
/// stream = &[u8],
/// sentinel = b"\x00",
/// key(enc = tinyklv::codecs::binary::enc::u8),
/// len(enc = tinyklv::codecs::binary::enc::u8_from_usize),
/// )]
/// #[klv(allow_unimplemented_decode)]
/// struct MyStruct {
/// #[klv(key = 0x07, enc = ex01_encoder)]
/// example_one: InnerValue,
///
/// #[klv(key = 0x0A, enc = ex02_encoder)]
/// example_two: InnerValue,
///
/// #[klv(key = 0x8A, enc = InnerValue::encode_value)]
/// example_three: InnerValue,
/// }
///
/// let my_struct_value_encoded = MyStruct {
/// example_one: InnerValue {},
/// example_two: InnerValue {},
/// example_three: InnerValue {},
/// }.encode_value();
///
/// assert_eq!(my_struct_value_encoded, vec![
/// // example 1
/// 0x07, // example 1 key
/// 0x04, // example 1 length
/// // example 1 value
/// 0x65, 0x66, 0x67, 0x68,
///
/// // example 2
/// 0x0A, // example 2 key
/// 0x03, // example 2 length
/// 0x59, 0x32, 0x4B, // example 2 value
///
/// // example 3
/// 0x8A, // example 3 key
/// 0x03, // example 3 length
/// 0x6B, 0x6C, 0x76, // example 3 value
/// ]);
///
/// let my_struct_encoded = MyStruct {
/// example_one: InnerValue {},
/// example_two: InnerValue {},
/// example_three: InnerValue {},
/// }.encode_frame(); // See: `tinyklv::prelude::EncodeFrame` -> This prepends the key and length
///
/// assert_eq!(my_struct_encoded, vec![
/// 0x00, // sentinel
/// 0x10, // total length
///
/// // example 1
/// 0x07, // example 1 key
/// 0x04, // example 1 length
/// // example 1 value
/// 0x65, 0x66, 0x67, 0x68,
///
/// // example 2
/// 0x0A, // example 2 key
/// 0x03, // example 2 length
/// 0x59, 0x32, 0x4B, // example 2 value
///
/// // example 3
/// 0x8A, // example 3 key
/// 0x03, // example 3 length
/// 0x6B, 0x6C, 0x76, // example 3 value
/// ]);
/// ```
/// Trait for prepending encoded data with its key and length.
///
/// ```text
/// This is what is prepended
/// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
/// [ ... key ... | ... length ... | ..... value (self) ..... ]
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/// ```
///
/// ***This trait is automatically implemented for all potential encoded-like datatypes. There is no need to implement it manually.***
///
/// # Example
///
/// ```rust
/// use tinyklv::prelude::*;
/// use tinyklv::traits::EncodeValue;
///
/// struct MyStruct {}
///
/// impl EncodeValue<Vec<u8>> for MyStruct {
/// fn encode_value(&self) -> Vec<u8> {
/// return "a value".as_bytes().to_vec();
/// }
/// }
///
/// let my_struct = MyStruct {};
///
/// let key_len_val_of_my_struct = my_struct.encode_value().into_klv(
/// [0xFF, 0xBB], // encoded key (must implement into iter)
/// |x: usize| // length encoder
/// (x as u8).to_be_bytes().to_vec(),
/// );
///
/// assert_eq!(key_len_val_of_my_struct, [
/// 0xFF, 0xBB, // key
/// 0x07, // length
/// 97, 32, 118, 97, 108, 117, 101, // value
/// ]);
/// ```
///
/// See [`EncodeValue`] for more information.
/// [`IntoKlv`] implementation for all types O that implement [`EncodedOutput<T>`]
/// [`IntoKlv`] implementation for all types [`Option<O>`] that implement [`EncodedOutput<T>`]
///
/// `None` produces empty output (field omitted from encoded packet). This is
/// typically the correct behavior for optional KLV fields.
/// Full KLV encode pipeline: prepends key and length to [`EncodeValue`] output.
///
/// Decode counterpart: [`DecodeFrame`](crate::traits::DecodeFrame)
///
/// Trait for encoding data to its full key-length-value representation.
///
/// ```text
/// This is what is encoded
/// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
/// [ ... key ... | ... length ... | ..... value (self) ..... ]
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/// ```
///
/// ***This trait IS automatically implemented for structs deriving the [`tinyklv::Klv`](crate::Klv) trait, in which every field has an associated encoder for it's type. Otherwise, this trait CAN be implemented manually.***
///
/// Since this function and [`EncodeValue`] have the same function signature, this could cause confusion. Therefore, the general workflow for all KLV encoding should be:
///
/// 1. Encode the struct/value
///
/// This can be done by implementing the [`EncodeValue`] trait.
///
/// 2. Use the [`IntoKlv`] function to convert the struct/value into its key-length-value, providing the encoded key/recognition sentinel, alongside the length encoder.
///
/// Then, youre done: now you can produce the key-length-value representation of your struct with the following snippet:
///
/// ```rust
/// use tinyklv::prelude::*;
/// use tinyklv::traits::EncodeValue;
///
/// struct MyStruct {}
///
/// impl EncodeValue<Vec<u8>> for MyStruct {
/// fn encode_value(&self) -> Vec<u8> {
/// return "example".as_bytes().to_vec();
/// }
/// }
///
/// let my_struct = MyStruct {};
///
/// let key_len_val_of_my_struct = my_struct.encode_value().into_klv(
/// [0xFF, 0xBB], // encoded key (must implement into iter)
/// |x: usize| // length encoder
/// (x as u8).to_be_bytes().to_vec(),
/// );
///
/// assert_eq!(key_len_val_of_my_struct, [
/// 0xFF, 0xBB, // key
/// 0x07, // length
/// 101, 120, 97, 109, 112, 108, 101, // value
/// ]);
/// ```
///
/// However, you can also implement the [`EncodeFrame`] trait instead to combine both of these operations:
///
/// ```rust
/// use tinyklv::prelude::*;
/// use tinyklv::traits::{EncodeValue, EncodeFrame};
///
/// struct MyStruct {}
///
/// impl EncodeValue<Vec<u8>> for MyStruct {
/// fn encode_value(&self) -> Vec<u8> {
/// return "example".as_bytes().to_vec();
/// }
/// }
///
/// impl EncodeFrame<Vec<u8>> for MyStruct {
/// fn encode_frame(&self) -> Vec<u8> {
/// return self.encode_value().into_klv(
/// [0xFF, 0xBB], // encoded key (must implement into iter)
/// |x: usize| // length encoder
/// (x as u8).to_be_bytes().to_vec(),
/// );
/// }
/// }
///
/// let my_struct = MyStruct {};
///
/// let key_len_val_of_my_struct = my_struct.encode_frame();
///
/// assert_eq!(key_len_val_of_my_struct, [
/// 0xFF, 0xBB, // key
/// 0x07, // length
/// 101, 120, 97, 109, 112, 108, 101, // value
/// ]);
/// ```
///
/// Furthermore, you can use the [`crate::Klv`] macro to implement both of these for you. (Note that the examples above on a blank struct aren't representative of how the macro works, so a field has been added to the struct):
///
/// ```rust
/// use tinyklv::Klv;
/// use tinyklv::prelude::*;
///
/// fn string_encoder(input: &String) -> Vec<u8> {
/// return input.as_bytes().to_vec();
/// }
///
/// #[derive(Klv)]
/// #[klv(
/// stream = &[u8],
/// sentinel = b"\x00",
/// allow_unimplemented_decode,
/// key(enc = tinyklv::codecs::binary::enc::u8),
/// len(enc = tinyklv::codecs::binary::enc::u8_from_usize),
/// )] // this implements `tinyklv::prelude::EncodeValue` and `tinyklv::prelude::EncodeFrame`
/// // given a key and length encoder are provided
/// struct MyStruct {
/// #[klv(key = 0xFF, enc = string_encoder)]
/// value: String,
/// }
///
/// // using `tinyklv::Klv` to encode as KLV
/// let mystruct_klv_1 = MyStruct {
/// value: "example".into()
/// }.encode_frame(); // `tinyklv::prelude::EncodeFrame` implementation
///
/// // using manual implementation to encode as KLV
/// let mystruct_klv_2 = MyStruct {
/// value: "example".into()
/// }.encode_value().into_klv(
/// [0x00], // recognition sentinel (must implement into iter)
/// tinyklv::codecs::binary::enc::u8_from_usize, // length encoder
/// ); // this is now equivalent to the macro call
///
/// assert_eq!(mystruct_klv_1, mystruct_klv_2);
/// ```