-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Showing
5 changed files
with
128 additions
and
36 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
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,68 @@ | ||
use crate::{chain::BaseFeeParamsWrapper, BaseFeeParams, Hardfork}; | ||
use serde::{ | ||
de::{self, SeqAccess, Visitor}, | ||
ser::SerializeSeq, | ||
Deserializer, Serializer, | ||
}; | ||
|
||
/// Deserialize the [BaseFeeParamsWrapper] | ||
pub fn deserialize_base_fee_params<'de, D>( | ||
deserializer: D, | ||
) -> Result<BaseFeeParamsWrapper, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
struct BaseFeeParamsWrapperVisitor; | ||
|
||
impl<'de> Visitor<'de> for BaseFeeParamsWrapperVisitor { | ||
type Value = BaseFeeParamsWrapper; | ||
|
||
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
formatter.write_str("struct BaseFeeParamsWrapper") | ||
} | ||
|
||
fn visit_seq<V>(self, mut seq: V) -> Result<BaseFeeParamsWrapper, V::Error> | ||
where | ||
V: SeqAccess<'de>, | ||
{ | ||
let first = seq.next_element::<BaseFeeParams>()?; | ||
if let Some(base_fee_params) = first { | ||
// Found a Constant variant | ||
Ok(BaseFeeParamsWrapper::Constant(base_fee_params)) | ||
} else { | ||
// Expecting a Variable variant, which should be a sequence | ||
let variable_params = seq | ||
.next_element::<Vec<(Hardfork, BaseFeeParams)>>()? | ||
.ok_or_else(|| de::Error::invalid_length(1, &self))?; | ||
Ok(BaseFeeParamsWrapper::Variable(variable_params)) | ||
} | ||
} | ||
} | ||
|
||
deserializer.deserialize_any(BaseFeeParamsWrapperVisitor) | ||
} | ||
|
||
/// Serialize the [BaseFeeParamsWrapper] | ||
pub fn serialize_base_fee_params<S>( | ||
base_fee_params: &BaseFeeParamsWrapper, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
match base_fee_params { | ||
BaseFeeParamsWrapper::Constant(base_fee_params) => serializer.serialize_newtype_variant( | ||
"BaseFeeParamsWrapper", | ||
0, | ||
"Constant", | ||
base_fee_params, | ||
), | ||
BaseFeeParamsWrapper::Variable(variable_params) => { | ||
let mut seq = serializer.serialize_seq(Some(variable_params.len()))?; | ||
for (hardfork, params) in variable_params { | ||
seq.serialize_element(&(hardfork, params))?; | ||
} | ||
seq.end() | ||
} | ||
} | ||
} |
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