use std::env;
use std::path::{Path, PathBuf};
use crate::error::CoreError;
const HOME_VARS: [&str; 3] = ["WANNING_HOME", "USERPROFILE", "HOME"];
pub fn home_dir() -> Option<PathBuf> {
for key in HOME_VARS {
match env::var_os(key) {
Some(value) if !value.is_empty() => return Some(PathBuf::from(value)),
_ => continue,
}
}
None
}
pub fn default_home() -> Option<PathBuf> {
home_dir().map(|home| home.join(".wanning"))
}
pub fn default_wal_path() -> Option<PathBuf> {
default_home().map(|home| home.join("wal.jsonl"))
}
pub fn ensure_wal_parent(wal_path: &Path) -> Result<(), CoreError> {
let Some(parent) = wal_path.parent() else {
return Ok(());
};
if parent.as_os_str().is_empty() {
return Ok(());
}
std::fs::create_dir_all(parent).map_err(|e| {
CoreError::WalIo(format!(
"创建审计账本目录 {parent:?} 失败: {e}(可用显式路径绕开默认位置)"
))
})
}