use std::path::PathBuf;
pub fn find_config_file() -> Option<PathBuf> {
if let Ok(custom) = std::env::var("OXUR_CONFIG_PATH") {
let path = PathBuf::from(custom);
if path.exists() {
return Some(path);
}
}
if let Some(config_dir) = dirs::config_dir() {
let path = config_dir.join("oxur").join("repl.toml");
if path.exists() {
return Some(path);
}
}
if let Some(home) = dirs::home_dir() {
let path = home.join(".oxur").join("repl.toml");
if path.exists() {
return Some(path);
}
}
None
}
pub fn default_history_path() -> PathBuf {
if let Ok(custom) = std::env::var("OXUR_HISTORY_PATH") {
return PathBuf::from(custom);
}
dirs::data_dir().unwrap_or_else(|| PathBuf::from(".")).join("oxur").join("repl_history")
}
pub fn config_dir() -> PathBuf {
dirs::config_dir().unwrap_or_else(|| PathBuf::from(".")).join("oxur")
}
pub fn data_dir() -> PathBuf {
dirs::data_dir().unwrap_or_else(|| PathBuf::from(".")).join("oxur")
}
pub fn cache_dir() -> PathBuf {
dirs::cache_dir().unwrap_or_else(|| PathBuf::from(".")).join("oxur")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_history_path_contains_oxur() {
let path = default_history_path();
assert!(path.to_string_lossy().contains("oxur"));
assert!(path.to_string_lossy().contains("repl_history"));
}
#[test]
fn test_config_dir_contains_oxur() {
let path = config_dir();
assert!(path.ends_with("oxur"));
}
#[test]
fn test_data_dir_contains_oxur() {
let path = data_dir();
assert!(path.ends_with("oxur"));
}
#[test]
fn test_cache_dir_contains_oxur() {
let path = cache_dir();
assert!(path.ends_with("oxur"));
}
#[test]
fn test_find_config_file_returns_none_when_no_file() {
let _ = find_config_file();
}
}