use std::path::PathBuf;
use std::time::Duration;
pub const DEFAULT_USER_AGENT: &str = concat!(
"uls-cli/",
env!("CARGO_PKG_VERSION"),
" (https://github.com/tgies/uls)"
);
#[derive(Debug, Clone)]
pub struct DownloadConfig {
pub cache_dir: PathBuf,
pub base_url: String,
pub timeout: Duration,
pub user_agent: String,
pub verify_ssl: bool,
pub max_retries: u32,
pub retry_delay: Duration,
pub bandwidth_limit: u64,
}
impl Default for DownloadConfig {
fn default() -> Self {
Self {
cache_dir: default_cache_dir(),
base_url: "https://data.fcc.gov/download/pub/uls".to_string(),
timeout: Duration::from_secs(300), user_agent: DEFAULT_USER_AGENT.to_string(),
verify_ssl: true,
max_retries: 3,
retry_delay: Duration::from_secs(5),
bandwidth_limit: 0,
}
}
}
impl DownloadConfig {
pub fn with_cache_dir(cache_dir: PathBuf) -> Self {
Self {
cache_dir,
..Default::default()
}
}
pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = base_url.into();
self
}
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
self.user_agent = user_agent.into();
self
}
pub fn with_bandwidth_limit(mut self, bytes_per_second: u64) -> Self {
self.bandwidth_limit = bytes_per_second;
self
}
}
fn default_cache_dir() -> PathBuf {
dirs::data_local_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("uls")
.join("cache")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = DownloadConfig::default();
assert!(config.base_url.contains("data.fcc.gov"));
assert!(config.user_agent.contains("uls-cli"));
assert!(config.verify_ssl);
assert_eq!(config.max_retries, 3);
}
#[test]
fn test_builder_pattern() {
let config = DownloadConfig::default()
.with_timeout(Duration::from_secs(60))
.with_bandwidth_limit(1_000_000);
assert_eq!(config.timeout, Duration::from_secs(60));
assert_eq!(config.bandwidth_limit, 1_000_000);
}
#[test]
fn test_with_cache_dir_keeps_other_defaults() {
let config = DownloadConfig::with_cache_dir(PathBuf::from("/tmp/uls-cache"));
assert_eq!(config.cache_dir, PathBuf::from("/tmp/uls-cache"));
assert_eq!(config.max_retries, 3);
assert!(config.verify_ssl);
assert_eq!(config.timeout, Duration::from_secs(300));
}
#[test]
fn test_full_builder_chain() {
let config = DownloadConfig::with_cache_dir(PathBuf::from("/var/cache/uls"))
.with_base_url("https://example.test")
.with_timeout(Duration::from_secs(10))
.with_user_agent("agent")
.with_bandwidth_limit(2_048);
assert_eq!(config.cache_dir, PathBuf::from("/var/cache/uls"));
assert_eq!(config.base_url, "https://example.test");
assert_eq!(config.timeout, Duration::from_secs(10));
assert_eq!(config.user_agent, "agent");
assert_eq!(config.bandwidth_limit, 2_048);
}
}