-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(jstz_core): introduce BinEncodable trait
- Loading branch information
Showing
9 changed files
with
134 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use bincode::{ | ||
config::{Configuration, Fixint, LittleEndian}, | ||
Decode, Encode, | ||
}; | ||
|
||
use crate::error::{Error, Result}; | ||
|
||
const BINCODE_CONFIGURATION: Configuration<LittleEndian, Fixint> = | ||
bincode::config::legacy(); | ||
|
||
/// Trait for types that can be encoded to and decoded from binary format | ||
pub trait BinEncodable { | ||
fn encode(&self) -> Result<Vec<u8>>; | ||
fn decode(bytes: &[u8]) -> Result<Self> | ||
where | ||
Self: Sized; | ||
} | ||
|
||
/// Default implementation for types that can be encoded to and decoded from binary format | ||
impl<T: Encode + Decode> BinEncodable for T { | ||
fn encode(&self) -> Result<Vec<u8>> { | ||
bincode::encode_to_vec(self, BINCODE_CONFIGURATION).map_err(|err| { | ||
Error::SerializationError { | ||
description: format!("{err}"), | ||
} | ||
}) | ||
} | ||
|
||
fn decode(bytes: &[u8]) -> Result<Self> { | ||
let (value, _) = bincode::decode_from_slice(bytes, BINCODE_CONFIGURATION) | ||
.map_err(|err| Error::SerializationError { | ||
description: format!("{err}"), | ||
})?; | ||
Ok(value) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[derive(Debug, Clone, PartialEq, Encode, Decode)] | ||
struct TestData { | ||
field1: String, | ||
field2: i32, | ||
} | ||
|
||
#[test] | ||
fn test_binencodable_roundtrip() { | ||
let original = TestData { | ||
field1: "test".to_string(), | ||
field2: 42, | ||
}; | ||
|
||
// Test encode | ||
let encoded = BinEncodable::encode(&original).unwrap(); | ||
assert!(!encoded.is_empty()); | ||
|
||
// Test decode | ||
let decoded = BinEncodable::decode(&encoded).unwrap(); | ||
assert_eq!(original, decoded); | ||
} | ||
|
||
#[test] | ||
fn test_binencodable_invalid_data() { | ||
// Try to decode invalid bytes | ||
let invalid_bytes = vec![1, 2, 3]; | ||
let result = <TestData as BinEncodable>::decode(&invalid_bytes); | ||
assert!(result.is_err()); | ||
|
||
// Verify error type | ||
match result { | ||
Err(Error::SerializationError { description: _ }) => (), | ||
_ => panic!("Expected SerializationError"), | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.