use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
pub const SYSMAP_DIR: &str = ".sysmap";
pub const MAP_FILE: &str = "map.json";
#[allow(dead_code)]
pub const CONFIG_FILE: &str = "config.toml";
pub fn find_sysmap_root(start: &Path) -> Option<PathBuf> {
let mut current = start.to_path_buf();
loop {
let sysmap_dir = current.join(SYSMAP_DIR);
if sysmap_dir.is_dir() {
return Some(current);
}
if !current.pop() {
return None;
}
}
}
pub fn sysmap_dir(root: &Path) -> PathBuf {
root.join(SYSMAP_DIR)
}
pub fn map_path(root: &Path) -> PathBuf {
sysmap_dir(root).join(MAP_FILE)
}
#[allow(dead_code)]
pub fn config_path(root: &Path) -> PathBuf {
sysmap_dir(root).join(CONFIG_FILE)
}
pub fn ensure_sysmap_dir(root: &Path) -> Result<PathBuf> {
let dir = sysmap_dir(root);
std::fs::create_dir_all(&dir)
.with_context(|| format!("Failed to create directory: {}", dir.display()))?;
Ok(dir)
}
pub fn is_initialized(root: &Path) -> bool {
map_path(root).exists()
}