Refactor ClientConfig, add serde to it

This commit is contained in:
Paul Hauner
2019-06-07 19:44:27 -04:00
parent 39ec96ad82
commit e73a31c37f
15 changed files with 223 additions and 229 deletions

View File

@@ -1,7 +1,9 @@
use clap::ArgMatches;
use serde_derive::{Deserialize, Serialize};
use std::net::Ipv4Addr;
/// RPC Configuration
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
/// Enable the RPC server.
pub enabled: bool,
@@ -20,3 +22,23 @@ impl Default for Config {
}
}
}
impl Config {
pub fn apply_cli_args(&mut self, args: &ArgMatches) -> Result<(), &'static str> {
if args.is_present("rpc") {
self.enabled = true;
}
if let Some(rpc_address) = args.value_of("rpc-address") {
self.listen_address = rpc_address
.parse::<Ipv4Addr>()
.map_err(|_| "rpc-address is not IPv4 address")?;
}
if let Some(rpc_port) = args.value_of("rpc-port") {
self.port = rpc_port.parse::<u16>().map_err(|_| "rpc-port is not u16")?;
}
Ok(())
}
}