Add serialization mod, with custom serialization logic
This commit is contained in:
parent
0e9121e604
commit
3ab9ef69bf
@ -14,6 +14,7 @@ pub mod pcd;
|
||||
pub mod prd;
|
||||
pub mod process;
|
||||
pub mod secrets;
|
||||
pub mod serialization;
|
||||
pub mod signature;
|
||||
pub mod silentpayments;
|
||||
|
||||
|
103
src/serialization.rs
Normal file
103
src/serialization.rs
Normal file
@ -0,0 +1,103 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sp_client::bitcoin::OutPoint;
|
||||
use std::collections::HashMap;
|
||||
use crate::{pcd::Member, process::Process};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct MemberOutPointMap(#[serde(with = "members_map")] pub HashMap<Member, OutPoint>);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct OutPointProcessMap(#[serde(with = "outpoint_map")] pub HashMap<OutPoint, Process>);
|
||||
|
||||
pub mod members_map {
|
||||
use crate::pcd::Member;
|
||||
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||
use std::collections::HashMap;
|
||||
use sp_client::bitcoin::OutPoint;
|
||||
|
||||
pub fn serialize<S>(
|
||||
map: &HashMap<Member, OutPoint>,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let map: HashMap<String, OutPoint> = map
|
||||
.iter()
|
||||
.map(|(k, v)| (k.to_string(), *v))
|
||||
.collect();
|
||||
map.serialize(serializer)
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D>(
|
||||
deserializer: D,
|
||||
) -> Result<HashMap<Member, OutPoint>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let map: HashMap<String, OutPoint> = HashMap::deserialize(deserializer)?;
|
||||
|
||||
let result: Result<HashMap<Member, OutPoint>, D::Error> = map
|
||||
.into_iter()
|
||||
.map(|(k, v)| {
|
||||
let member = k
|
||||
.split(',')
|
||||
.map(|s| s.try_into())
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(serde::de::Error::custom)?;
|
||||
let member = Member::new(member).map_err(serde::de::Error::custom)?;
|
||||
Ok((member, v))
|
||||
})
|
||||
.collect();
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
pub mod outpoint_map {
|
||||
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||
use std::collections::HashMap;
|
||||
use sp_client::bitcoin::OutPoint;
|
||||
use crate::process::Process;
|
||||
|
||||
pub fn serialize<S>(
|
||||
map: &HashMap<OutPoint, Process>,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
// Convert OutPoint keys to Strings
|
||||
let map: HashMap<String, Process> = map
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.to_string(), v.clone()))
|
||||
.collect();
|
||||
|
||||
// Serialize as HashMap<String, Process>
|
||||
map.serialize(serializer)
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D>(
|
||||
deserializer: D,
|
||||
) -> Result<HashMap<OutPoint, Process>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
// Deserialize as HashMap<String, Process>
|
||||
let map: HashMap<String, Process> = HashMap::deserialize(deserializer)?;
|
||||
|
||||
// Convert String keys back to OutPoint
|
||||
let result: Result<HashMap<OutPoint, Process>, D::Error> = map
|
||||
.into_iter()
|
||||
.map(|(k, v)| {
|
||||
let outpoint = k
|
||||
.parse()
|
||||
.map_err(serde::de::Error::custom)?;
|
||||
Ok((outpoint, v))
|
||||
})
|
||||
.collect();
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user