use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct CliConfiguration {
pub log: String,
pub site: String,
#[serde(default)]
pub read_only: bool,
pub manta_server_url: String,
pub socks5_proxy: Option<String>,
#[serde(default)]
pub request_timeout_secs: Option<u64>,
#[serde(default)]
pub power_poll_interval_secs: Option<u64>,
#[serde(default)]
pub power_max_poll_attempts: Option<u32>,
#[serde(default)]
pub sat_file_poll_interval_secs: Option<u64>,
#[serde(default)]
pub sat_file_poll_budget_secs: Option<u64>,
#[serde(default)]
pub sat_file_not_visible_budget_secs: Option<u64>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cli_configuration_roundtrip_toml_minimal() {
let cfg = CliConfiguration {
log: "info".to_string(),
site: "alps".to_string(),
read_only: false,
manta_server_url: "https://manta-server.cscs.ch:8443".to_string(),
socks5_proxy: Some("socks5h://127.0.0.1:1080".to_string()),
request_timeout_secs: None,
power_poll_interval_secs: None,
power_max_poll_attempts: None,
sat_file_poll_interval_secs: None,
sat_file_poll_budget_secs: None,
sat_file_not_visible_budget_secs: None,
};
let toml_str = toml::to_string(&cfg).unwrap();
let parsed: CliConfiguration = toml::from_str(&toml_str).unwrap();
assert_eq!(parsed.site, "alps");
assert_eq!(parsed.manta_server_url, "https://manta-server.cscs.ch:8443");
assert_eq!(
parsed.socks5_proxy.as_deref(),
Some("socks5h://127.0.0.1:1080")
);
}
#[test]
fn cli_configuration_socks5_proxy_optional() {
let toml_str = r#"
log = "info"
site = "alps"
manta_server_url = "https://manta-server.cscs.ch:8443"
"#;
let parsed: CliConfiguration = toml::from_str(toml_str).unwrap();
assert!(parsed.socks5_proxy.is_none());
}
#[test]
fn cli_configuration_missing_manta_server_url_fails() {
let bad_toml = r#"
log = "info"
site = "alps"
# missing manta_server_url
"#;
let result = toml::from_str::<CliConfiguration>(bad_toml);
assert!(result.is_err());
}
fn minimal_toml() -> String {
r#"
log = "info"
site = "alps"
manta_server_url = "https://manta-server.example.com:8443"
"#
.to_string()
}
#[test]
fn read_only_defaults_to_false_when_absent() {
let cfg: CliConfiguration =
toml::from_str(&minimal_toml()).expect("parse");
assert!(!cfg.read_only);
}
#[test]
fn read_only_parses_true_when_present() {
let mut s = minimal_toml();
s.push_str("read_only = true\n");
let cfg: CliConfiguration = toml::from_str(&s).expect("parse");
assert!(cfg.read_only);
}
#[test]
fn read_only_parses_false_when_explicitly_false() {
let mut s = minimal_toml();
s.push_str("read_only = false\n");
let cfg: CliConfiguration = toml::from_str(&s).expect("parse");
assert!(!cfg.read_only);
}
}