Skip to content

Commit 17d6218

Browse files
authored
Merge pull request #3259 from Pana/op/configCode
optimize config declare code
2 parents a46c71a + e506835 commit 17d6218

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

crates/cfx_types/src/space.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
22
use serde::ser::SerializeMap;
33
use serde_derive::{Deserialize, Serialize};
4-
use std::ops::{Add, Index, IndexMut};
4+
use std::{
5+
ops::{Add, Index, IndexMut},
6+
str::FromStr,
7+
};
58

69
#[derive(
710
Eq,
@@ -38,6 +41,18 @@ impl From<Space> for &'static str {
3841
}
3942
}
4043

44+
impl FromStr for Space {
45+
type Err = String;
46+
47+
fn from_str(s: &str) -> Result<Self, Self::Err> {
48+
match s {
49+
"native" => Ok(Space::Native),
50+
"evm" => Ok(Space::Ethereum),
51+
_ => Err(format!("Unrecognized space: {}", s)),
52+
}
53+
}
54+
}
55+
4156
impl Encodable for Space {
4257
fn rlp_append(&self, s: &mut RlpStream) {
4358
let type_int: u8 = match self {

crates/config/src/configuration.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,7 @@ build_config! {
422422
// Development related section.
423423
(
424424
log_level, (LevelFilter), LevelFilter::Info, |l| {
425-
match l {
426-
"off" => Ok(LevelFilter::Off),
427-
"error" => Ok(LevelFilter::Error),
428-
"warn" => Ok(LevelFilter::Warn),
429-
"info" => Ok(LevelFilter::Info),
430-
"debug" => Ok(LevelFilter::Debug),
431-
"trace" => Ok(LevelFilter::Trace),
432-
_ => Err("Invalid log_level".to_owned()),
433-
}
425+
LevelFilter::from_str(l).map_err(|e| e.to_string())
434426
}
435427
)
436428

@@ -449,11 +441,7 @@ build_config! {
449441
(public_rpc_apis, (ApiSet), ApiSet::Safe, ApiSet::from_str)
450442
(public_evm_rpc_apis, (EthApiSet), EthApiSet::Evm, EthApiSet::from_str)
451443
(public_evm_rpc_async_apis, (RpcModuleSelection), RpcModuleSelection::Evm, RpcModuleSelection::from_str)
452-
(single_mpt_space, (Option<Space>), None, |s| match s {
453-
"native" => Ok(Space::Native),
454-
"evm" => Ok(Space::Ethereum),
455-
_ => Err("Invalid single_mpt_space".to_owned()),
456-
})
444+
(single_mpt_space, (Option<Space>), None, Space::from_str)
457445
}
458446
}
459447

0 commit comments

Comments
 (0)