Add serialization {de}serialize_hex

This commit is contained in:
NicolasCantu 2025-03-12 10:19:55 +01:00 committed by Nicolas Cantu
parent 0b645d5384
commit 1f67eff723

View File

@ -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;