use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct DataDir {
path: PathBuf,
eager_fvs: Vec<String>,
allow_other_release: bool,
}
impl DataDir {
pub fn auto() -> Self {
let path = if let Ok(env_dir) = std::env::var("EDIFACT_DATA_DIR") {
PathBuf::from(env_dir)
} else if let Some(home) = home_dir() {
home.join(".edifact").join("data")
} else {
PathBuf::from("data")
};
Self {
path,
eager_fvs: vec![],
allow_other_release: false,
}
}
pub fn path<P: AsRef<Path>>(path: P) -> Self {
Self {
path: path.as_ref().to_path_buf(),
eager_fvs: vec![],
allow_other_release: false,
}
}
pub fn eager(mut self, fvs: &[&str]) -> Self {
self.eager_fvs = fvs.iter().map(|s| s.to_string()).collect();
self
}
pub fn allow_bundle_from_other_release(mut self, allow: bool) -> Self {
self.allow_other_release = allow;
self
}
pub fn allows_bundle_from_other_release(&self) -> bool {
self.allow_other_release
}
pub fn data_path(&self) -> &Path {
&self.path
}
pub fn eager_fvs(&self) -> &[String] {
&self.eager_fvs
}
pub fn bundle_path(&self, fv: &str) -> PathBuf {
self.path.join(format!("edifact-data-{fv}.bin"))
}
}
fn home_dir() -> Option<PathBuf> {
std::env::var("HOME")
.or_else(|_| std::env::var("USERPROFILE"))
.ok()
.map(PathBuf::from)
}