Commit 76911dfcc7
Unsigned
Layout: unified · split
src/commands/matrix.rs +1 −1
| @@ -227,7 +227,7 @@ impl CommandCallback for MatrixCommand { | ||
| 227 | 227 | .arg( |
| 228 | 228 | Arg::with_name("homeserver") |
| 229 | 229 | .required(true) |
| 230 | .validator(MatrixServer::parse_homeserver_url), | |
| 230 | .validator(MatrixServer::parse_url), | |
| 231 | 231 | ), |
| 232 | 232 | ) |
| 233 | 233 | .subcommand( |
src/server.rs +33 −32
| @@ -257,7 +257,24 @@ impl MatrixServer { | ||
| 257 | 257 | self.inner.borrow() |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | pub fn parse_homeserver_url(value: String) -> Result<(), String> { | |
| 260 | /// Parse an URL returning a None if the string is empty. | |
| 261 | /// | |
| 262 | /// # Panics | |
| 263 | /// | |
| 264 | /// This panics if the string can't be parsed as an URL. | |
| 265 | fn parse_url_unchecked(value: &str) -> Option<Url> { | |
| 266 | if value.is_empty() { | |
| 267 | None | |
| 268 | } else { | |
| 269 | Some( | |
| 270 | Url::parse(value) | |
| 271 | .expect("Can't parse URL, did the check callback fail?"), | |
| 272 | ) | |
| 273 | } | |
| 274 | } | |
| 275 | ||
| 276 | /// Parse an URL returning an error if the parse step fails. | |
| 277 | pub fn parse_url(value: String) -> Result<(), String> { | |
| 261 | 278 | let url = Url::parse(&value); |
| 262 | 279 | |
| 263 | 280 | match url { |
| @@ -272,6 +289,15 @@ impl MatrixServer { | ||
| 272 | 289 | } |
| 273 | 290 | } |
| 274 | 291 | |
| 292 | /// Check if the provided value is a valid URL. | |
| 293 | fn is_url_valid(value: &str) -> bool { | |
| 294 | if value.is_empty() { | |
| 295 | true | |
| 296 | } else { | |
| 297 | MatrixServer::parse_url(value.to_string()).is_ok() | |
| 298 | } | |
| 299 | } | |
| 300 | ||
| 275 | 301 | fn create_server_conf( |
| 276 | 302 | server_name: &str, |
| 277 | 303 | server_section: &mut ConfigSection, |
| @@ -303,12 +329,7 @@ impl MatrixServer { | ||
| 303 | 329 | let homeserver = |
| 304 | 330 | StringOptionSettings::new(format!("{}.homeserver", server_name)) |
| 305 | 331 | .set_check_callback(|_, _, value| { |
| 306 | if value.is_empty() { | |
| 307 | true | |
| 308 | } else { | |
| 309 | MatrixServer::parse_homeserver_url(value.to_string()) | |
| 310 | .is_ok() | |
| 311 | } | |
| 332 | MatrixServer::is_url_valid(&value) | |
| 312 | 333 | }) |
| 313 | 334 | .set_change_callback(move |_, option| { |
| 314 | 335 | let server = server.clone(); |
| @@ -318,15 +339,8 @@ impl MatrixServer { | ||
| 318 | 339 | |
| 319 | 340 | let mut server = server_ref.borrow_mut(); |
| 320 | 341 | |
| 321 | if option.value().is_empty() { | |
| 322 | let homeserver = Url::parse(option.value().as_ref()).expect( | |
| 323 | "Can't parse homeserver URL, did the check callback fail?", | |
| 324 | ); | |
| 325 | ||
| 326 | server.settings.homeserver = Some(homeserver) | |
| 327 | } else { | |
| 328 | server.settings.homeserver = None; | |
| 329 | } | |
| 342 | server.settings.homeserver = | |
| 343 | MatrixServer::parse_url_unchecked(&option.value()); | |
| 330 | 344 | }); |
| 331 | 345 | |
| 332 | 346 | server_section |
| @@ -338,12 +352,7 @@ impl MatrixServer { | ||
| 338 | 352 | |
| 339 | 353 | let proxy = StringOptionSettings::new(format!("{}.proxy", server_name)) |
| 340 | 354 | .set_check_callback(|_, _, value| { |
| 341 | if value.is_empty() { | |
| 342 | true | |
| 343 | } else { | |
| 344 | MatrixServer::parse_homeserver_url(value.to_string()) | |
| 345 | .is_ok() | |
| 346 | } | |
| 355 | MatrixServer::is_url_valid(&value) | |
| 347 | 356 | }) |
| 348 | 357 | .set_change_callback(move |_, option| { |
| 349 | 358 | let server = server.clone(); |
| @@ -352,16 +361,8 @@ impl MatrixServer { | ||
| 352 | 361 | .expect("Server got deleted while server config is alive"); |
| 353 | 362 | |
| 354 | 363 | let mut server = server_ref.borrow_mut(); |
| 355 | ||
| 356 | if option.value().is_empty() { | |
| 357 | server.settings.proxy = None | |
| 358 | } else { | |
| 359 | let proxy = Url::parse(option.value().as_ref()).expect( | |
| 360 | "Can't parse proxy URL, did the check callback fail?", | |
| 361 | ); | |
| 362 | ||
| 363 | server.settings.proxy = Some(proxy) | |
| 364 | } | |
| 364 | server.settings.proxy = | |
| 365 | MatrixServer::parse_url_unchecked(&option.value()); | |
| 365 | 366 | }); |
| 366 | 367 | |
| 367 | 368 | server_section |