Edit a saved server configuration. Implements `spacetime server edit`. Returns `Err` if no such server exists. On success, returns `(old_nickname, old_host, hold_protocol)`, with `Some` for each field that was changed.
(
&mut self,
server: &str,
new_nickname: Option<&str>,
new_host: Option<&str>,
new_protocol: Option<&str>,
)
| 365 | /// On success, returns `(old_nickname, old_host, hold_protocol)`, |
| 366 | /// with `Some` for each field that was changed. |
| 367 | pub fn edit_server( |
| 368 | &mut self, |
| 369 | server: &str, |
| 370 | new_nickname: Option<&str>, |
| 371 | new_host: Option<&str>, |
| 372 | new_protocol: Option<&str>, |
| 373 | ) -> anyhow::Result<(Option<String>, Option<String>, Option<String>)> { |
| 374 | // Check if the new nickname or host name would introduce ambiguities between |
| 375 | // server configurations. |
| 376 | if let Some(new_nick) = new_nickname |
| 377 | && let Ok(other_server) = self.find_server(new_nick) |
| 378 | { |
| 379 | anyhow::bail!( |
| 380 | "Nickname {} conflicts with saved configuration for server {}: {}://{}", |
| 381 | new_nick, |
| 382 | other_server.nick_or_host(), |
| 383 | other_server.protocol, |
| 384 | other_server.host |
| 385 | ); |
| 386 | } |
| 387 | if let Some(new_host) = new_host |
| 388 | && let Ok(other_server) = self.find_server(new_host) |
| 389 | { |
| 390 | anyhow::bail!( |
| 391 | "Host {} conflicts with saved configuration for server {}: {}://{}", |
| 392 | new_host, |
| 393 | other_server.nick_or_host(), |
| 394 | other_server.protocol, |
| 395 | other_server.host |
| 396 | ); |
| 397 | } |
| 398 | |
| 399 | let cfg = self.find_server_mut(server)?; |
| 400 | let old_nickname = if let Some(new_nickname) = new_nickname { |
| 401 | cfg.nickname.replace(new_nickname.to_string()) |
| 402 | } else { |
| 403 | None |
| 404 | }; |
| 405 | let old_host = if let Some(new_host) = new_host { |
| 406 | Some(std::mem::replace(&mut cfg.host, new_host.to_string())) |
| 407 | } else { |
| 408 | None |
| 409 | }; |
| 410 | let old_protocol = if let Some(new_protocol) = new_protocol { |
| 411 | Some(std::mem::replace(&mut cfg.protocol, new_protocol.to_string())) |
| 412 | } else { |
| 413 | None |
| 414 | }; |
| 415 | |
| 416 | // If the server we edited was the default server, |
| 417 | // and we changed the identifier stored in the `default_server` field, |
| 418 | // update that field. |
| 419 | if let Some(default_server) = &mut self.default_server { |
| 420 | if let Some(old_host) = &old_host { |
| 421 | if default_server == old_host { |
| 422 | *default_server = new_host.unwrap().to_string(); |
| 423 | } |
| 424 | } else if let Some(old_nick) = &old_nickname |
no test coverage detected