@@ -2,10 +2,11 @@ use std::net::{SocketAddr, ToSocketAddrs};
2
2
use std:: num:: ParseIntError ;
3
3
use std:: str:: FromStr ;
4
4
use std:: path:: Path ;
5
- use std:: collections:: BTreeSet as Set ;
5
+ use std:: collections:: HashMap ;
6
6
7
7
use config:: { Config , File as CfgFile } ;
8
8
use serde:: de:: { self , Deserialize , Deserializer } ;
9
+ use serde_yaml:: Value ;
9
10
use clap:: { App , AppSettings , Arg , SubCommand , ArgMatches } ;
10
11
use hex:: FromHex ;
11
12
use itertools:: Itertools ;
@@ -89,15 +90,15 @@ fn de_log_type<'de, D>(deserializer: D) -> Result<LogType, D::Error> where D: De
89
90
#[ cfg( unix) ]
90
91
"Syslog" => Ok ( LogType :: Syslog ) ,
91
92
"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 ) ) ) ,
93
94
}
94
95
}
95
96
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 > {
97
98
let s = String :: deserialize ( deserializer) ?;
98
99
99
100
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) ) )
101
102
}
102
103
103
104
impl BootstrapNode {
@@ -135,17 +136,15 @@ pub struct NodeConfig {
135
136
#[ serde( skip_deserializing) ]
136
137
pub sk_passed_as_arg : bool ,
137
138
/// Path to the file where DHT keys are stored.
138
- /// When run with config, this field is required .
139
+ /// Required with config.
139
140
#[ serde( rename = "keys-file" ) ]
140
- pub keys_file_config : String ,
141
- #[ serde( skip_deserializing) ]
142
141
pub keys_file : Option < String > ,
143
142
/// List of bootstrap nodes.
144
143
#[ serde( rename = "bootstrap-nodes" ) ]
145
144
#[ serde( default ) ]
146
145
pub bootstrap_nodes : Vec < BootstrapNode > ,
147
146
/// Number of threads for execution.
148
- #[ serde( deserialize_with = "de_thread " ) ]
147
+ #[ serde( deserialize_with = "de_threads " ) ]
149
148
pub threads : Threads ,
150
149
/// Specifies where to write logs.
151
150
#[ serde( deserialize_with = "de_log_type" ) ]
@@ -154,11 +153,11 @@ pub struct NodeConfig {
154
153
/// Message of the day
155
154
pub motd : String ,
156
155
/// Whether LAN discovery is enabled
157
- #[ serde( rename = "no- lan" ) ]
156
+ #[ serde( rename = "lan-discovery " ) ]
158
157
pub lan_discovery_enabled : bool ,
159
158
/// Unused fields while parsing config file
160
- #[ serde( skip_deserializing ) ]
161
- pub unused : Set < String > ,
159
+ #[ serde( flatten ) ]
160
+ pub unused : HashMap < String , Value > ,
162
161
}
163
162
164
163
/// Parse command line arguments.
@@ -248,9 +247,9 @@ pub fn cli_parse() -> NodeConfig {
248
247
}
249
248
} )
250
249
. 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) " ) )
254
253
. get_matches ( ) ;
255
254
256
255
match matches. subcommand ( ) {
@@ -265,7 +264,7 @@ fn parse_config(config_path: String) -> NodeConfig {
265
264
266
265
settings. set_default ( "log-type" , "Stderr" ) . expect ( "Can't set default value for `log-type`" ) ;
267
266
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 `" ) ;
269
268
settings. set_default ( "threads" , "1" ) . expect ( "Can't set default value for `threads`" ) ;
270
269
271
270
let config_file = if !Path :: new ( & config_path) . exists ( ) {
@@ -274,18 +273,13 @@ fn parse_config(config_path: String) -> NodeConfig {
274
273
CfgFile :: with_name ( & config_path)
275
274
} ;
276
275
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 " ) ;
278
277
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" ) ;
284
279
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
+ }
289
283
290
284
config
291
285
}
@@ -342,22 +336,19 @@ fn run_args(matches: &ArgMatches) -> NodeConfig {
342
336
343
337
let motd = value_t ! ( matches. value_of( "motd" ) , String ) . unwrap_or_else ( |e| e. exit ( ) ) ;
344
338
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" ) ;
348
340
349
341
NodeConfig {
350
342
udp_addr,
351
343
tcp_addrs,
352
344
sk,
353
345
sk_passed_as_arg,
354
346
keys_file,
355
- keys_file_config,
356
347
bootstrap_nodes,
357
348
threads,
358
349
log_type,
359
350
motd,
360
351
lan_discovery_enabled,
361
- unused : Set :: new ( ) ,
352
+ unused : HashMap :: new ( ) ,
362
353
}
363
354
}
0 commit comments