-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jormungandr-lib: add FragmentDef to use serde on fragments
- Loading branch information
1 parent
6185c33
commit db43812
Showing
2 changed files
with
97 additions
and
0 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,95 @@ | ||
use chain_impl_mockchain::fragment::Fragment; | ||
|
||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
use serde_with::{DeserializeAs, SerializeAs}; | ||
|
||
#[derive(Debug)] | ||
pub struct FragmentDef(Fragment); | ||
|
||
impl From<Fragment> for FragmentDef { | ||
fn from(fragment: Fragment) -> Self { | ||
Self(fragment) | ||
} | ||
} | ||
|
||
impl From<FragmentDef> for Fragment { | ||
fn from(fragment_def: FragmentDef) -> Self { | ||
fragment_def.0 | ||
} | ||
} | ||
|
||
impl FragmentDef { | ||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Fragment, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
<Self as Deserialize>::deserialize(deserializer).map(|fragment_def| fragment_def.0) | ||
} | ||
|
||
pub fn serialize<S>(fragment: &Fragment, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serialize_fragment(&fragment, serializer) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for FragmentDef { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let bytes = if deserializer.is_human_readable() { | ||
let h = String::deserialize(deserializer)?; | ||
hex::decode(&h).map_err(serde::de::Error::custom)? | ||
} else { | ||
Vec::<u8>::deserialize(deserializer)? | ||
}; | ||
|
||
let fragment = <Fragment as chain_core::property::Deserialize>::deserialize(bytes.as_ref()) | ||
.map_err(serde::de::Error::custom)?; | ||
|
||
Ok(FragmentDef(fragment)) | ||
} | ||
} | ||
|
||
impl Serialize for FragmentDef { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serialize_fragment(&self.0, serializer) | ||
} | ||
} | ||
|
||
impl<'de> DeserializeAs<'de, Fragment> for FragmentDef { | ||
fn deserialize_as<D>(deserializer: D) -> Result<Fragment, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
Self::deserialize(deserializer) | ||
} | ||
} | ||
|
||
impl SerializeAs<Fragment> for FragmentDef { | ||
fn serialize_as<S>(source: &Fragment, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serialize_fragment(source, serializer) | ||
} | ||
} | ||
|
||
fn serialize_fragment<S>(fragment: &Fragment, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let bytes = <Fragment as chain_core::property::Serialize>::serialize_as_vec(fragment) | ||
.map_err(serde::ser::Error::custom)?; | ||
|
||
if serializer.is_human_readable() { | ||
serializer.serialize_str(&hex::encode(&bytes)) | ||
} else { | ||
serializer.serialize_bytes(&bytes) | ||
} | ||
} |
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