Add serialization {de}serialize_hex
This commit is contained in:
parent
978ebd75c1
commit
fe6d78fbb6
@ -11,6 +11,29 @@ pub struct OutPointMemberMap(#[serde(with = "members_map")] pub HashMap<OutPoint
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct OutPointProcessMap(#[serde(with = "outpoint_map")] pub HashMap<OutPoint, Process>);
|
||||
|
||||
// These helper functions convert a [u8; 32] to/from a hex string.
|
||||
pub(crate) fn serialize_hex<S>(bytes: &[u8; 32], serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let hex_str = bytes.to_lower_hex_string();
|
||||
serializer.serialize_str(&hex_str)
|
||||
}
|
||||
|
||||
pub(crate) fn deserialize_hex<'de, D>(deserializer: D) -> Result<[u8; 32], D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
let bytes = Vec::from_hex(&s).map_err(D::Error::custom)?;
|
||||
if bytes.len() != 32 {
|
||||
return Err(D::Error::custom("Invalid length for [u8;32]"));
|
||||
}
|
||||
let mut arr = [0u8; 32];
|
||||
arr.copy_from_slice(&bytes);
|
||||
Ok(arr)
|
||||
}
|
||||
|
||||
pub mod members_map {
|
||||
use super::*;
|
||||
use crate::pcd::Member;
|
||||
|
Loading…
x
Reference in New Issue
Block a user