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
#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(clippy::unwrap_used)]
//! See: `book/tutorial/01-getting-started.md` for full example
use tinyklv::Klv; // proc-macro
use tinyklv::DecodeValue; // decode the value from bytes
use tinyklv::prelude::*; // additional prelude - helps with decoder loop
use tinyklv::dec::binary as decb; // binary decoders
#[derive(Klv, Debug, PartialEq)]
#[klv(
key(dec = decb::u8),
len(dec = decb::u8_as_usize),
allow_unimplemented_encode,
)]
struct Heartbeat {
#[klv(
key = 0x01,
dec = decb::u8,
)]
sequence: u8,
#[klv(
key = 0x02,
dec = decb::be_u16,
)]
temperature_centideg: u16,
}
fn main() {
// manually construct the packet
let original_stream = [
0x01, // sequence key
0x01, // value len = 1
0x2A, // sequence value = 42
0x02, // temperature key
0x02, // value len = 2
0x09, 0x2E, // temperature value = 2350
];
// manually construct the value
let original_constructed = Heartbeat {
sequence: 42,
temperature_centideg: 2350,
};
// decode the value
// `winnow` is used internally, which requires a &mut Stream, in
// this case a &mut &[u8]. this allows for the slice to be borrowed
// and changing the pointer in a zero-copy manner
let decoded = Heartbeat::decode_value(
&mut original_stream.as_slice()
).unwrap();
// they equal!
assert_eq!(decoded, original_constructed);
}