-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deserialization seems to be working. Need some refactoring
- Loading branch information
1 parent
7cdf544
commit 81a9a98
Showing
7 changed files
with
219 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
pub trait PingMessage | ||
where | ||
Self: Sized, | ||
Self: Sized + Serialize + Deserialize, | ||
{ | ||
fn message_id(&self) -> u16; | ||
fn message_name(&self) -> &'static str; | ||
|
||
fn message_id_from_name(name: &str) -> Result<u16, &'static str>; | ||
|
||
fn serialize(self, buffer: &mut [u8]) -> usize; | ||
} | ||
|
||
pub trait Serialize { | ||
fn serialize(self, buffer: &mut [u8]) -> usize; | ||
fn serialize(&self, buffer: &mut [u8]) -> usize; | ||
} | ||
|
||
pub trait Deserialize | ||
where | ||
Self: Sized, | ||
{ | ||
fn deserialize(buffer: &[u8]) -> Result<Self, &'static str>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::convert::TryFrom; | ||
|
||
use ping_rs::common::Messages as common_messages; | ||
use ping_rs::{common, Messages}; | ||
|
||
#[test] | ||
fn test_simple_deserialization() { | ||
let general_request = | ||
common_messages::GeneralRequest(common::GeneralRequestStruct { requested_id: 5 }); | ||
|
||
let buffer: Vec<u8> = vec![ | ||
0x42, 0x52, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00, 0x05, 0x00, 0xa1, 0x00, | ||
]; | ||
let Messages::Common(parsed) = Messages::try_from(&buffer).unwrap() else { | ||
panic!(""); | ||
}; | ||
|
||
// From official ping protocol documentation | ||
assert_eq!(general_request, parsed); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters