Skip to content

Commit

Permalink
[WIP] refactor framing_sv2 example..
Browse files Browse the repository at this point in the history
we need to assert that the deserialized message has the same content from the original
  • Loading branch information
plebhash committed Feb 9, 2025
1 parent 19f26e3 commit a7fe4ab
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions protocols/v2/framing-sv2/examples/sv2_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
// cargo run --example sv2_frame
// ```

use binary_sv2::{binary_codec_sv2, Serialize};
use binary_sv2::{binary_codec_sv2, Serialize, Deserialize};
use framing_sv2::framing::Sv2Frame;
use std::convert::TryInto;

// Example message type (e.g., SetupConnection)
const MSG_TYPE: u8 = 1;
// Example extension type (e.g., a standard Sv2 message)
const EXT_TYPE: u16 = 0x0001;

#[derive(Serialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct CustomMessage {
pub data: u32,
}
Expand All @@ -39,11 +40,6 @@ fn main() {
Sv2Frame::from_message(message, MSG_TYPE, EXT_TYPE, false)
.expect("Failed to frame the message");

// How header information is accessed
let header = frame.get_header().expect("Frame has no header");
assert_eq!(header.msg_type(), MSG_TYPE);
assert_eq!(header.ext_type(), EXT_TYPE);

// Serialize the frame into a byte array for transmission
let mut serialized_frame = vec![0u8; frame.encoded_length()];
frame
Expand All @@ -54,6 +50,12 @@ fn main() {
let mut deserialized_frame = Sv2Frame::<CustomMessage, Vec<u8>>::from_bytes(serialized_frame)
.expect("Failed to deserialize frame");

assert_eq!(deserialized_frame.encoded_length(), 10); // 6 header bytes + 4 payload bytes
assert_eq!(deserialized_frame.payload(), [42, 0, 0, 0]);
// Assert that deserialized header has the original content
let deserialized_header = deserialized_frame.get_header().expect("Frame has no header");
assert_eq!(deserialized_header.msg_type(), MSG_TYPE);
assert_eq!(deserialized_header.ext_type(), EXT_TYPE);

// Assert that deserialized message has the original content
let deserialized_message: CustomMessage = binary_sv2::from_bytes(deserialized_frame.payload()).expect("Failed to extract the message from the payload");
assert_eq!(deserialized_message.data, message.data);
}

0 comments on commit a7fe4ab

Please sign in to comment.