use std::path::PathBuf;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct CacheConfig {
pub root: Option<PathBuf>,
}
impl CacheConfig {
pub fn new(root: Option<PathBuf>) -> Self {
Self { root }
}
pub fn resolve(&self) -> PathBuf {
if let Some(p) = &self.root {
return p.clone();
}
if let Ok(custom) = std::env::var(crate::env::VOXORA_CACHE_DIR)
&& !custom.is_empty()
{
return PathBuf::from(custom);
}
if let Some(base) = dirs::cache_dir() {
return base.join("voxora");
}
PathBuf::from(".voxora-cache")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_resolves_via_xdg_or_fallback() {
let cfg = CacheConfig::default();
let resolved = cfg.resolve();
assert!(!resolved.as_os_str().is_empty());
let s = resolved.to_string_lossy();
assert!(
s.ends_with("voxora") || s.ends_with("voxora-cache"),
"got {s:?}"
);
}
#[test]
fn explicit_root_wins() {
let cfg = CacheConfig {
root: Some(PathBuf::from("/tmp/explicit")),
};
assert_eq!(cfg.resolve(), PathBuf::from("/tmp/explicit"));
}
#[test]
fn new_matches_struct_expression() {
let cfg = CacheConfig::new(Some(PathBuf::from("/tmp/via-new")));
assert_eq!(
cfg,
CacheConfig {
root: Some(PathBuf::from("/tmp/via-new")),
}
);
}
}