Skip to main content

decode_exact_alloc/
decode_exact_alloc.rs

1//! Decode exactly one frame and re-encode it into a vector.
2//!
3//! This is the allocating counterpart to `decode_exact_no_alloc`. It requires
4//! the crate's `alloc` feature.
5//!
6//! Run with:
7//!
8//! ```sh
9//! cargo run -p meterbus-wired-datalink --example decode_exact_alloc --features alloc
10//! ```
11
12use meterbus_wired_datalink::{
13    CommunicationType, Frame,
14    decoder::exact::{DecodeError, decode},
15};
16
17fn main() -> Result<(), DecodeError> {
18    let frame = decode(&[0x10, 0x5b, 0x01, 0x5c, 0x16])?;
19
20    if let Frame::Short(short) = &frame {
21        assert_eq!(
22            short.control().communication_type(),
23            CommunicationType::ReqUd2
24        );
25        assert_eq!(short.address().value(), 1);
26    }
27
28    assert_eq!(frame.encode(), [0x10, 0x5b, 0x01, 0x5c, 0x16]);
29    Ok(())
30}