1use serde::Serialize;
2
3use crate::config::{load_config, save_config};
4use crate::error::{AmError, AmResult};
5
6#[derive(Debug, Serialize)]
7pub struct RelayInfo {
8 pub url: String,
9}
10
11pub fn add(url: &str) -> AmResult<()> {
12 let mut config = load_config()?;
13 if config.relays.iter().any(|r| r == url) {
14 return Err(AmError::Config(format!("relay '{url}' already exists")));
15 }
16 config.relays.push(url.to_string());
17 save_config(&config)?;
18 Ok(())
19}
20
21pub fn remove(url: &str) -> AmResult<()> {
22 let mut config = load_config()?;
23 let before = config.relays.len();
24 config.relays.retain(|r| r != url);
25 if config.relays.len() == before {
26 return Err(AmError::Config(format!("relay '{url}' not found")));
27 }
28 save_config(&config)?;
29 Ok(())
30}
31
32pub fn list() -> AmResult<Vec<RelayInfo>> {
33 let config = load_config()?;
34 Ok(config
35 .relays
36 .into_iter()
37 .map(|url| RelayInfo { url })
38 .collect())
39}