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

Commit a63d604

Browse files
authored
Merge pull request #39 from tox-rs/pretty_fixes
Pretty fixes
2 parents 52f80b3 + c7732de commit a63d604

File tree

5 files changed

+29
-49
lines changed

5 files changed

+29
-49
lines changed

Cargo.lock

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ tokio-codec = "0.1"
2929
config = "0.9.1"
3030
serde = "1.0"
3131
serde_derive = "1.0"
32-
serde_ignored = "0.0.4"
3332
serde_yaml = "0.8.8"
3433
tox = { git = "https://github.com/tox-rs/tox/" }
3534

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A server application to run tox bootstrap node.
44

55
## Building and running
66

7-
You'll need [Rust] >= 1.26.0 and [libsodium].
7+
You'll need [Rust] >= 1.31.0.
88

99
Build with:
1010

@@ -78,7 +78,7 @@ In order to run with config, run with `--config <file>`.
7878
Example config.yml is below.
7979
```yaml
8080
log-type: Stderr
81-
keys-file: keys
81+
keys-file: ./keys
8282
udp-address: 0.0.0.0:33445
8383
tcp-addresses:
8484
- 0.0.0.0:33445
@@ -88,12 +88,13 @@ bootstrap-nodes:
8888
addr: 198.98.51.198:33445
8989
- pk: DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43
9090
addr: 185.25.116.107:33445
91-
no-lan: True
91+
threads: auto # or any u16 > 0
92+
lan-discovery: True
9293
```
9394
Or you can use it with CLI like this
9495
```sh
9596
tox-node --keys-file keys \
96-
--bootstrap-node 1D5A5F2F5D6233058BF0259B09622FB40B482E4FA0931EB8FD3AB8E7BF7DAF6F 198.98.51.198:33445 \
97+
--bootstrap-node 1D5A5F2F5D6233058BF0259B09622FB40B482E4FA0931EB8FD3AB8E7BF7DAF6F 198.98.51.198:33445 \
9798
--udp-address '0.0.0.0:33445' --tcp-address '0.0.0.0:33445' \
9899
--motd "{{start_date}} {{uptime}} Tcp: incoming {{tcp_packets_in}}, outgoing {{tcp_packets_out}}, Udp: incoming {{udp_packets_in}}, outgoing {{udp_packets_out}}"
99100
```

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: 21 additions & 30 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;
@@ -89,15 +90,15 @@ fn de_log_type<'de, D>(deserializer: D) -> Result<LogType, D::Error> where D: De
8990
#[cfg(unix)]
9091
"Syslog" => Ok(LogType::Syslog),
9192
"None" => Ok(LogType::None),
92-
e => Err(de::Error::custom(format!("Invalid LogType {}", e))),
93+
other => Err(de::Error::custom(format!("log-type: invalid value '{}'", other))),
9394
}
9495
}
9596

96-
fn de_thread<'de, D>(deserializer: D) -> Result<Threads, D::Error> where D: Deserializer<'de> {
97+
fn de_threads<'de, D>(deserializer: D) -> Result<Threads, D::Error> where D: Deserializer<'de> {
9798
let s = String::deserialize(deserializer)?;
9899

99100
Threads::from_str(&s)
100-
.map_err(|e| de::Error::custom(format!("Can't parse Threads {:?}", e)))
101+
.map_err(|e| de::Error::custom(format!("threads: {:?}", e)))
101102
}
102103

103104
impl BootstrapNode {
@@ -135,17 +136,15 @@ 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")]
145144
#[serde(default)]
146145
pub bootstrap_nodes: Vec<BootstrapNode>,
147146
/// Number of threads for execution.
148-
#[serde(deserialize_with = "de_thread")]
147+
#[serde(deserialize_with = "de_threads")]
149148
pub threads: Threads,
150149
/// Specifies where to write logs.
151150
#[serde(deserialize_with = "de_log_type")]
@@ -154,11 +153,11 @@ pub struct NodeConfig {
154153
/// Message of the day
155154
pub motd: String,
156155
/// Whether LAN discovery is enabled
157-
#[serde(rename = "no-lan")]
156+
#[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.
@@ -248,9 +247,9 @@ pub fn cli_parse() -> NodeConfig {
248247
}
249248
})
250249
.default_value("This is tox-rs"))
251-
.arg(Arg::with_name("no-lan")
252-
.long("no-lan")
253-
.help("Disable LAN discovery"))
250+
.arg(Arg::with_name("lan-discovery")
251+
.long("lan-discovery")
252+
.help("Enable LAN discovery (disabled by default)"))
254253
.get_matches();
255254

256255
match matches.subcommand() {
@@ -265,7 +264,7 @@ fn parse_config(config_path: String) -> NodeConfig {
265264

266265
settings.set_default("log-type", "Stderr").expect("Can't set default value for `log-type`");
267266
settings.set_default("motd", "This is tox-rs").expect("Can't set default value for `motd`");
268-
settings.set_default("no-lan", "False").expect("Can't set default value for `no-lan`");
267+
settings.set_default("lan-discovery", "False").expect("Can't set default value for `lan-discovery`");
269268
settings.set_default("threads", "1").expect("Can't set default value for `threads`");
270269

271270
let config_file = if !Path::new(&config_path).exists() {
@@ -274,18 +273,13 @@ fn parse_config(config_path: String) -> NodeConfig {
274273
CfgFile::with_name(&config_path)
275274
};
276275

277-
settings.merge(config_file).expect("Merging config file with default value fails");
276+
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.lan_discovery_enabled = !config.lan_discovery_enabled;
288-
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+
}
289283

290284
config
291285
}
@@ -342,22 +336,19 @@ fn run_args(matches: &ArgMatches) -> NodeConfig {
342336

343337
let motd = value_t!(matches.value_of("motd"), String).unwrap_or_else(|e| e.exit());
344338

345-
let lan_discovery_enabled = !matches.is_present("no-lan");
346-
347-
let keys_file_config = String::new();
339+
let lan_discovery_enabled = matches.is_present("lan-discovery");
348340

349341
NodeConfig {
350342
udp_addr,
351343
tcp_addrs,
352344
sk,
353345
sk_passed_as_arg,
354346
keys_file,
355-
keys_file_config,
356347
bootstrap_nodes,
357348
threads,
358349
log_type,
359350
motd,
360351
lan_discovery_enabled,
361-
unused: Set::new(),
352+
unused: HashMap::new(),
362353
}
363354
}

0 commit comments

Comments
 (0)