-
Notifications
You must be signed in to change notification settings - Fork 784
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
803ce9b
commit 27f6bdc
Showing
12 changed files
with
117 additions
and
385 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use serde::de::Error; | ||
use serde::{Deserialize, Deserializer, Serializer}; | ||
|
||
const BYTES_LEN: usize = 4; | ||
|
||
pub fn serialize<S>(bytes: &[u8; BYTES_LEN], serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let mut hex_string: String = "0x".to_string(); | ||
hex_string.push_str(&hex::encode(&bytes)); | ||
|
||
serializer.serialize_str(&hex_string) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; BYTES_LEN], D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s: String = Deserialize::deserialize(deserializer)?; | ||
let mut array = [0 as u8; BYTES_LEN]; | ||
|
||
let start = s | ||
.as_str() | ||
.get(2..) | ||
.ok_or_else(|| D::Error::custom("string length too small"))?; | ||
let decoded: Vec<u8> = hex::decode(&start).map_err(D::Error::custom)?; | ||
|
||
if decoded.len() != BYTES_LEN { | ||
return Err(D::Error::custom("Fork length too long")); | ||
} | ||
|
||
for (i, item) in array.iter_mut().enumerate() { | ||
if i > decoded.len() { | ||
break; | ||
} | ||
*item = decoded[i]; | ||
} | ||
Ok(array) | ||
} |
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,3 +1,6 @@ | ||
pub mod bytes_4_hex; | ||
pub mod hex; | ||
pub mod quoted_u64; | ||
pub mod quoted_u64_vec; | ||
pub mod u32_hex; | ||
pub mod u8_hex; |
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,28 @@ | ||
use serde::de::Error; | ||
use serde::{Deserialize, Deserializer, Serializer}; | ||
|
||
pub fn serialize<S>(num: &u32, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let mut hex: String = "0x".to_string(); | ||
let bytes = num.to_le_bytes(); | ||
hex.push_str(&hex::encode(&bytes)); | ||
|
||
serializer.serialize_str(&hex) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<u32, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s: String = Deserialize::deserialize(deserializer)?; | ||
let start = s | ||
.as_str() | ||
.get(2..) | ||
.ok_or_else(|| D::Error::custom("string length too small"))?; | ||
|
||
u32::from_str_radix(&start, 16) | ||
.map_err(D::Error::custom) | ||
.map(u32::from_be) | ||
} |
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,25 @@ | ||
use serde::de::Error; | ||
use serde::{Deserialize, Deserializer, Serializer}; | ||
|
||
pub fn serialize<S>(byte: &u8, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let mut hex: String = "0x".to_string(); | ||
hex.push_str(&hex::encode(&[*byte])); | ||
|
||
serializer.serialize_str(&hex) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<u8, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s: String = Deserialize::deserialize(deserializer)?; | ||
|
||
let start = match s.as_str().get(2..) { | ||
Some(start) => start, | ||
None => return Err(D::Error::custom("string length too small")), | ||
}; | ||
u8::from_str_radix(&start, 16).map_err(D::Error::custom) | ||
} |
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.