Skip to content
Permalink
Browse files

Remove Serde support from ConfigParam

  • Loading branch information...
CodeSandwich committed Apr 14, 2019
1 parent ecd700b commit 223c75fdc6dd4c8e703fd7956978bfc292e7095f
Showing with 1 addition and 131 deletions.
  1. +1 −131 chain-impl-mockchain/src/config.rs
@@ -5,12 +5,11 @@ use chain_addr::Discrimination;
use chain_core::mempack::{ReadBuf, ReadError, Readable};
use chain_core::packer::Codec;
use chain_core::property;
use chain_crypto::{bech32::Bech32 as _, PublicKey};
use chain_crypto::PublicKey;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use std::fmt::{self, Display, Formatter};
use std::io::{self, Write};
use std::str::FromStr;
use strum_macros::{AsRefStr, EnumIter, EnumString};

/// Possible errors
@@ -146,68 +145,9 @@ impl property::Serialize for ConfigParam {
}
}

#[cfg(feature = "generic-serialization")]
mod serde_impl {
use super::*;
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};

impl<'de> Deserialize<'de> for ConfigParam {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let (tag_str, value) = <(String, String)>::deserialize(deserializer)?;
let tag = Tag::from_str(tag_str).map_err(|_| D::Error::custom(Error::InvalidTag))?;
match tag {
Tag::Block0Date => Block0Date::from_cfg_str(&value).map(ConfigParam::Block0Date),
Tag::Discrimination => {
Discrimination::from_cfg_str(&value).map(ConfigParam::Discrimination)
}
Tag::ConsensusVersion => {
ConsensusVersion::from_cfg_str(&value).map(ConfigParam::ConsensusVersion)
}
Tag::SlotsPerEpoch => {
SlotsPerEpoch::from_cfg_str(&value).map(ConfigParam::SlotsPerEpoch)
}
Tag::SlotDuration => {
SlotDuration::from_cfg_str(&value).map(ConfigParam::SlotDuration)
}
Tag::ConsensusLeaderCert => {
ConsensusLeaderCert::from_cfg_str(&value).map(ConfigParam::ConsensusLeaderCert)
}
Tag::ConsensusGenesisPraosParamD => {
ConsensusGenesisPraosParamD::from_cfg_str(&value)
.map(ConfigParam::ConsensusGenesisPraosParamD)
}
Tag::ConsensusGenesisPraosParamF => {
ConsensusGenesisPraosParamF::from_cfg_str(&value)
.map(ConfigParam::ConsensusGenesisPraosParamF)
}
}
.map_err(D::Error::custom)
}
}

impl Serialize for ConfigParam {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let tag = Tag::from(&self).as_ref();
let value = match self {
ConfigParam::Block0Date(data) => data.to_cfg_string(),
ConfigParam::Discrimination(data) => data.to_cfg_string(),
ConfigParam::ConsensusVersion(data) => data.to_cfg_string(),
ConfigParam::SlotsPerEpoch(data) => data.to_cfg_string(),
ConfigParam::SlotDuration(data) => data.to_cfg_string(),
ConfigParam::ConsensusLeaderCert(data) => data.to_cfg_string(),
ConfigParam::ConsensusGenesisPraosParamD(data) => data.to_cfg_string(),
ConfigParam::ConsensusGenesisPraosParamF(data) => data.to_cfg_string(),
};
(tag, value).serialize(serializer)
}
}
}

trait ConfigParamVariant: Clone + Eq + PartialEq {
fn to_payload(&self) -> Vec<u8>;
fn from_payload(payload: &[u8]) -> Result<Self, Error>;
fn to_cfg_string(&self) -> String;
fn from_cfg_str(s: &str) -> Result<Self, Error>;
}

/// Seconds elapsed since 1-Jan-1970 (unix time)
@@ -222,14 +162,6 @@ impl ConfigParamVariant for Block0Date {
fn from_payload(payload: &[u8]) -> Result<Self, Error> {
u64::from_payload(payload).map(Block0Date)
}

fn to_cfg_string(&self) -> String {
self.0.to_string()
}

fn from_cfg_str(s: &str) -> Result<Self, Error> {
from_cfg_str(s).map(Block0Date)
}
}

const VAL_PROD: u8 = 1;
@@ -253,22 +185,6 @@ impl ConfigParamVariant for Discrimination {
_ => Err(Error::StructureInvalid),
}
}

fn to_cfg_string(&self) -> String {
match self {
Discrimination::Production => "production",
Discrimination::Test => "test",
}
.to_string()
}

fn from_cfg_str(s: &str) -> Result<Self, Error> {
match s {
"production" => Ok(Discrimination::Production),
"test" => Ok(Discrimination::Test),
_ => Err(Error::UnknownString(s.to_string())),
}
}
}

impl ConfigParamVariant for ConsensusVersion {
@@ -285,14 +201,6 @@ impl ConfigParamVariant for ConsensusVersion {
let integer = u16::from_be_bytes(bytes);
ConsensusVersion::from_u16(integer).ok_or(Error::StructureInvalid)
}

fn to_cfg_string(&self) -> String {
self.to_string()
}

fn from_cfg_str(s: &str) -> Result<Self, Error> {
from_cfg_str(s)
}
}

impl ConfigParamVariant for LeaderId {
@@ -305,16 +213,6 @@ impl ConfigParamVariant for LeaderId {
.map(Into::into)
.map_err(|_| Error::SizeInvalid)
}

fn to_cfg_string(&self) -> String {
self.as_public_key().to_bech32_str()
}

fn from_cfg_str(s: &str) -> Result<Self, Error> {
PublicKey::try_from_bech32_str(s)
.map(Into::into)
.map_err(|_| Error::UnknownString(s.to_string()))
}
}

impl ConfigParamVariant for u8 {
@@ -328,14 +226,6 @@ impl ConfigParamVariant for u8 {
_ => Err(Error::SizeInvalid),
}
}

fn to_cfg_string(&self) -> String {
self.to_string()
}

fn from_cfg_str(s: &str) -> Result<Self, Error> {
from_cfg_str(s)
}
}

impl ConfigParamVariant for u64 {
@@ -351,14 +241,6 @@ impl ConfigParamVariant for u64 {
bytes.copy_from_slice(payload);
Ok(Self::from_be_bytes(bytes))
}

fn to_cfg_string(&self) -> String {
self.to_string()
}

fn from_cfg_str(s: &str) -> Result<Self, Error> {
from_cfg_str(s)
}
}

impl ConfigParamVariant for Milli {
@@ -369,18 +251,6 @@ impl ConfigParamVariant for Milli {
fn from_payload(payload: &[u8]) -> Result<Self, Error> {
u64::from_payload(payload).map(Milli::from_millis)
}

fn to_cfg_string(&self) -> String {
self.to_string()
}

fn from_cfg_str(s: &str) -> Result<Self, Error> {
from_cfg_str(s)
}
}

fn from_cfg_str<T: FromStr>(s: &str) -> Result<T, Error> {
s.parse().map_err(|_| Error::UnknownString(s.to_string()))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]

0 comments on commit 223c75f

Please sign in to comment.
You can’t perform that action at this time.