Skip to content

Commit 9afd0a8

Browse files
remagpiemergify[bot]
authored andcommitted
Accept arbitrary string as a chain type
1 parent c3520bc commit 9afd0a8

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

codechain/config/chain_type.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ use std::{fmt, fs};
1919

2020
use ccore::Scheme;
2121
use never_type::Never;
22+
use serde::de::{Error, Visitor};
23+
use serde::{Deserialize, Deserializer};
2224

23-
#[derive(Clone, Debug, PartialEq, Deserialize)]
24-
#[serde(rename_all = "snake_case")]
25+
#[derive(Clone, Debug, PartialEq)]
2526
pub enum ChainType {
2627
Mainnet,
2728
Solo,
28-
#[serde(rename = "simple_poa")]
2929
SimplePoA,
3030
Tendermint,
3131
Cuckoo,
32-
#[serde(rename = "blake_pow")]
3332
BlakePoW,
3433
Husky,
3534
Saluki,
@@ -65,6 +64,36 @@ impl FromStr for ChainType {
6564
}
6665
}
6766

67+
impl<'a> Deserialize<'a> for ChainType {
68+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
69+
where
70+
D: Deserializer<'a>, {
71+
struct ChainTypeVisitor;
72+
73+
impl<'a> Visitor<'a> for ChainTypeVisitor {
74+
type Value = ChainType;
75+
76+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
77+
write!(formatter, "a valid chain type string")
78+
}
79+
80+
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
81+
where
82+
E: Error, {
83+
Ok(ChainType::from_str(value).expect("ChainType can always be deserialized"))
84+
}
85+
86+
fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
87+
where
88+
E: Error, {
89+
self.visit_str(value.as_ref())
90+
}
91+
}
92+
93+
deserializer.deserialize_any(ChainTypeVisitor)
94+
}
95+
}
96+
6897
impl fmt::Display for ChainType {
6998
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7099
f.write_str(match self {

0 commit comments

Comments
 (0)