1
- use std;
2
1
use std:: collections:: HashMap ;
3
2
use std:: fs;
4
3
use std:: net:: IpAddr ;
@@ -8,7 +7,7 @@ use std::str::FromStr;
8
7
use config:: { Config , ConfigError , File } ;
9
8
use serde:: { Deserialize , Serialize } ;
10
9
use serde_with:: { serde_as, NoneAsEmptyString } ;
11
- use toml;
10
+ use { std , toml} ;
12
11
13
12
use crate :: databases:: database:: DatabaseDrivers ;
14
13
use crate :: mode:: TrackerMode ;
@@ -70,24 +69,21 @@ impl std::fmt::Display for ConfigurationError {
70
69
match self {
71
70
ConfigurationError :: IOError ( e) => e. fmt ( f) ,
72
71
ConfigurationError :: ParseError ( e) => e. fmt ( f) ,
73
- _ => write ! ( f, "{:?}" , self )
72
+ _ => write ! ( f, "{:?}" , self ) ,
74
73
}
75
74
}
76
75
}
77
76
78
77
impl std:: error:: Error for ConfigurationError { }
79
78
80
79
impl Configuration {
81
-
82
80
pub fn get_ext_ip ( & self ) -> Option < IpAddr > {
83
81
match & self . external_ip {
84
82
None => None ,
85
- Some ( external_ip) => {
86
- match IpAddr :: from_str ( external_ip) {
87
- Ok ( external_ip) => Some ( external_ip) ,
88
- Err ( _) => None
89
- }
90
- }
83
+ Some ( external_ip) => match IpAddr :: from_str ( external_ip) {
84
+ Ok ( external_ip) => Some ( external_ip) ,
85
+ Err ( _) => None ,
86
+ } ,
91
87
}
92
88
}
93
89
@@ -111,24 +107,23 @@ impl Configuration {
111
107
http_api : HttpApiConfig {
112
108
enabled : true ,
113
109
bind_address : String :: from ( "127.0.0.1:1212" ) ,
114
- access_tokens : [ ( String :: from ( "admin" ) , String :: from ( "MyAccessToken" ) ) ] . iter ( ) . cloned ( ) . collect ( ) ,
110
+ access_tokens : [ ( String :: from ( "admin" ) , String :: from ( "MyAccessToken" ) ) ]
111
+ . iter ( )
112
+ . cloned ( )
113
+ . collect ( ) ,
115
114
} ,
116
115
} ;
117
- configuration. udp_trackers . push (
118
- UdpTrackerConfig {
119
- enabled : false ,
120
- bind_address : String :: from ( "0.0.0.0:6969" ) ,
121
- }
122
- ) ;
123
- configuration. http_trackers . push (
124
- HttpTrackerConfig {
125
- enabled : false ,
126
- bind_address : String :: from ( "0.0.0.0:6969" ) ,
127
- ssl_enabled : false ,
128
- ssl_cert_path : None ,
129
- ssl_key_path : None ,
130
- }
131
- ) ;
116
+ configuration. udp_trackers . push ( UdpTrackerConfig {
117
+ enabled : false ,
118
+ bind_address : String :: from ( "0.0.0.0:6969" ) ,
119
+ } ) ;
120
+ configuration. http_trackers . push ( HttpTrackerConfig {
121
+ enabled : false ,
122
+ bind_address : String :: from ( "0.0.0.0:6969" ) ,
123
+ ssl_enabled : false ,
124
+ ssl_cert_path : None ,
125
+ ssl_key_path : None ,
126
+ } ) ;
132
127
configuration
133
128
}
134
129
@@ -142,10 +137,14 @@ impl Configuration {
142
137
eprintln ! ( "Creating config file.." ) ;
143
138
let config = Configuration :: default ( ) ;
144
139
let _ = config. save_to_file ( path) ;
145
- return Err ( ConfigError :: Message ( format ! ( "Please edit the config.TOML in the root folder and restart the tracker." ) ) ) ;
140
+ return Err ( ConfigError :: Message ( format ! (
141
+ "Please edit the config.TOML in the root folder and restart the tracker."
142
+ ) ) ) ;
146
143
}
147
144
148
- let torrust_config: Configuration = config. try_into ( ) . map_err ( |e| ConfigError :: Message ( format ! ( "Errors while processing config: {}." , e) ) ) ?;
145
+ let torrust_config: Configuration = config
146
+ . try_into ( )
147
+ . map_err ( |e| ConfigError :: Message ( format ! ( "Errors while processing config: {}." , e) ) ) ?;
149
148
150
149
Ok ( torrust_config)
151
150
}
@@ -193,7 +192,11 @@ mod tests {
193
192
194
193
[http_api.access_tokens]
195
194
admin = "MyAccessToken"
196
- "# . lines ( ) . map ( |line| line. trim_start ( ) ) . collect :: < Vec < & str > > ( ) . join ( "\n " ) ;
195
+ "#
196
+ . lines ( )
197
+ . map ( |line| line. trim_start ( ) )
198
+ . collect :: < Vec < & str > > ( )
199
+ . join ( "\n " ) ;
197
200
config
198
201
}
199
202
@@ -219,11 +222,12 @@ mod tests {
219
222
220
223
#[ test]
221
224
fn configuration_should_be_saved_in_a_toml_config_file ( ) {
222
- use std:: env;
223
- use crate :: Configuration ;
224
- use std:: fs;
225
+ use std:: { env, fs} ;
226
+
225
227
use uuid:: Uuid ;
226
228
229
+ use crate :: Configuration ;
230
+
227
231
// Build temp config file path
228
232
let temp_directory = env:: temp_dir ( ) ;
229
233
let temp_file = temp_directory. join ( format ! ( "test_config_{}.toml" , Uuid :: new_v4( ) ) ) ;
@@ -234,24 +238,27 @@ mod tests {
234
238
235
239
let default_configuration = Configuration :: default ( ) ;
236
240
237
- default_configuration. save_to_file ( & path) . expect ( "Could not save configuration to file" ) ;
241
+ default_configuration
242
+ . save_to_file ( & path)
243
+ . expect ( "Could not save configuration to file" ) ;
238
244
239
245
let contents = fs:: read_to_string ( & path) . expect ( "Something went wrong reading the file" ) ;
240
246
241
247
assert_eq ! ( contents, default_config_toml( ) ) ;
242
248
}
243
249
244
250
#[ cfg( test) ]
245
- fn create_temp_config_file_with_default_config ( ) -> String {
251
+ fn create_temp_config_file_with_default_config ( ) -> String {
246
252
use std:: env;
247
253
use std:: fs:: File ;
248
254
use std:: io:: Write ;
255
+
249
256
use uuid:: Uuid ;
250
257
251
258
// Build temp config file path
252
259
let temp_directory = env:: temp_dir ( ) ;
253
260
let temp_file = temp_directory. join ( format ! ( "test_config_{}.toml" , Uuid :: new_v4( ) ) ) ;
254
-
261
+
255
262
// Convert to argument type for Configuration::load_from_file
256
263
let config_file_path = temp_file. clone ( ) ;
257
264
let path = config_file_path. to_string_lossy ( ) . to_string ( ) ;
@@ -282,4 +289,4 @@ mod tests {
282
289
283
290
assert_eq ! ( format!( "{}" , error) , "TrackerModeIncompatible" ) ;
284
291
}
285
- }
292
+ }
0 commit comments