2019-06-07 23:44:27 +00:00
|
|
|
use clap::ArgMatches;
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-03-19 13:01:00 +00:00
|
|
|
use std::net::Ipv4Addr;
|
|
|
|
|
|
|
|
/// RPC Configuration
|
2019-06-07 23:44:27 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2019-03-19 13:01:00 +00:00
|
|
|
pub struct Config {
|
|
|
|
/// Enable the RPC server.
|
|
|
|
pub enabled: bool,
|
|
|
|
/// The IPv4 address the RPC will listen on.
|
|
|
|
pub listen_address: Ipv4Addr,
|
|
|
|
/// The port the RPC will listen on.
|
|
|
|
pub port: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Config {
|
|
|
|
enabled: false, // rpc disabled by default
|
|
|
|
listen_address: Ipv4Addr::new(127, 0, 0, 1),
|
|
|
|
port: 5051,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 23:44:27 +00:00
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|