Skip to content
This repository was archived by the owner on Dec 10, 2022. It is now read-only.

Commit 3548925

Browse files
committed
feat(config): make config:keys-file required
1 parent 5b67060 commit 3548925

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ extern crate config;
1717
#[macro_use]
1818
extern crate serde_derive;
1919
extern crate serde;
20-
extern crate serde_ignored;
2120
extern crate serde_yaml;
2221
extern crate tox;
2322

@@ -305,8 +304,8 @@ fn main() {
305304
LogType::None => { },
306305
}
307306

308-
for arg_unused in config.unused.clone() {
309-
warn!("Unused configuration key: {:?}", arg_unused);
307+
for (key, _) in &config.unused {
308+
warn!("Unused configuration key: {:?}", key);
310309
}
311310

312311
let (dht_pk, dht_sk) = if let Some(ref sk) = config.sk {

src/node_config.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use std::net::{SocketAddr, ToSocketAddrs};
22
use std::num::ParseIntError;
33
use std::str::FromStr;
44
use std::path::Path;
5-
use std::collections::BTreeSet as Set;
5+
use std::collections::HashMap;
66

77
use config::{Config, File as CfgFile};
88
use serde::de::{self, Deserialize, Deserializer};
9+
use serde_yaml::Value;
910
use clap::{App, AppSettings, Arg, SubCommand, ArgMatches};
1011
use hex::FromHex;
1112
use itertools::Itertools;
@@ -135,10 +136,8 @@ pub struct NodeConfig {
135136
#[serde(skip_deserializing)]
136137
pub sk_passed_as_arg: bool,
137138
/// Path to the file where DHT keys are stored.
138-
/// When run with config, this field is required.
139+
/// Required with config.
139140
#[serde(rename = "keys-file")]
140-
pub keys_file_config: String,
141-
#[serde(skip_deserializing)]
142141
pub keys_file: Option<String>,
143142
/// List of bootstrap nodes.
144143
#[serde(rename = "bootstrap-nodes")]
@@ -157,8 +156,8 @@ pub struct NodeConfig {
157156
#[serde(rename = "lan-discovery")]
158157
pub lan_discovery_enabled: bool,
159158
/// Unused fields while parsing config file
160-
#[serde(skip_deserializing)]
161-
pub unused: Set<String>,
159+
#[serde(flatten)]
160+
pub unused: HashMap<String, Value>,
162161
}
163162

164163
/// Parse command line arguments.
@@ -276,15 +275,11 @@ fn parse_config(config_path: String) -> NodeConfig {
276275

277276
settings.merge(config_file).expect("Merging config file with default values failed");
278277

279-
// Collect unrecognized fields to warn about them
280-
let mut unused = Set::new();
281-
let mut config: NodeConfig = serde_ignored::deserialize(settings, |path| {
282-
unused.insert(path.to_string());
283-
}).expect("Can't deserialize config");
278+
let config: NodeConfig = settings.try_into().expect("Can't deserialize config");
284279

285-
config.unused = unused;
286-
config.sk_passed_as_arg = false;
287-
config.keys_file = Some(config.keys_file_config.clone());
280+
if config.keys_file.is_none() {
281+
panic!("Can't deserialize config: 'keys-file' is not set");
282+
}
288283

289284
config
290285
}
@@ -343,20 +338,17 @@ fn run_args(matches: &ArgMatches) -> NodeConfig {
343338

344339
let lan_discovery_enabled = matches.is_present("lan-discovery");
345340

346-
let keys_file_config = String::new();
347-
348341
NodeConfig {
349342
udp_addr,
350343
tcp_addrs,
351344
sk,
352345
sk_passed_as_arg,
353346
keys_file,
354-
keys_file_config,
355347
bootstrap_nodes,
356348
threads,
357349
log_type,
358350
motd,
359351
lan_discovery_enabled,
360-
unused: Set::new(),
352+
unused: HashMap::new(),
361353
}
362354
}

0 commit comments

Comments
 (0)