#![allow(dead_code)]
use std::path::{Path, PathBuf};
pub fn omne_dir(root: &Path) -> PathBuf {
root.join(".omne")
}
pub fn core_dir(root: &Path) -> PathBuf {
omne_dir(root).join("core")
}
pub fn dist_dir(root: &Path) -> PathBuf {
omne_dir(root).join("dist")
}
pub fn cfg_dir(root: &Path) -> PathBuf {
omne_dir(root).join("lib").join("cfg")
}
pub fn docs_dir(root: &Path) -> PathBuf {
omne_dir(root).join("lib").join("docs")
}
pub fn docs_baseline(root: &Path) -> PathBuf {
docs_dir(root).join("index.md")
}
pub fn var_dir(root: &Path) -> PathBuf {
omne_dir(root).join("var")
}
pub fn ulid_lock_path(root: &Path) -> PathBuf {
var_dir(root).join(".ulid-last")
}
pub fn runs_dir(root: &Path) -> PathBuf {
var_dir(root).join("runs")
}
pub fn run_dir(root: &Path, run_id: &str) -> PathBuf {
runs_dir(root).join(run_id)
}
pub fn events_log_path(root: &Path, run_id: &str) -> PathBuf {
run_dir(root, run_id).join("events.jsonl")
}
pub fn nodes_dir(root: &Path, run_id: &str) -> PathBuf {
run_dir(root, run_id).join("nodes")
}
pub fn node_capture_wire_path(run_id: &str, node_id: &str) -> String {
format!(".omne/var/runs/{run_id}/nodes/{node_id}.out")
}
pub fn wt_dir(root: &Path) -> PathBuf {
omne_dir(root).join("wt")
}
pub fn wt_for(root: &Path, run_id: &str) -> PathBuf {
wt_dir(root).join(run_id)
}
pub fn find_omne_root(start: &Path) -> Option<PathBuf> {
let mut current = start.canonicalize().ok()?;
loop {
if current.join(".omne").is_dir() {
return Some(current);
}
if !current.pop() {
return None;
}
}
}
#[cfg(test)]
mod tests {
use std::fs;
use tempfile::TempDir;
use super::*;
#[test]
fn finds_root_when_omne_exists_at_start() {
let tmp = TempDir::new().expect("create tempdir");
fs::create_dir(tmp.path().join(".omne")).expect("create .omne");
let found = find_omne_root(tmp.path());
let expected = tmp.path().canonicalize().expect("canonicalize tmpdir");
assert_eq!(found, Some(expected));
}
#[test]
fn finds_root_from_subdirectory() {
let tmp = TempDir::new().expect("create tempdir");
fs::create_dir(tmp.path().join(".omne")).expect("create .omne");
let deep = tmp.path().join("src").join("deep");
fs::create_dir_all(&deep).expect("create deep subdir");
let found = find_omne_root(&deep);
let expected = tmp.path().canonicalize().expect("canonicalize tmpdir");
assert_eq!(found, Some(expected));
}
#[test]
fn walk_up_matches_actual_ancestor_chain() {
let tmp = TempDir::new().expect("create tempdir");
let canonical_start = tmp.path().canonicalize().expect("canonicalize tmpdir");
let expected = canonical_start
.ancestors()
.find(|ancestor| ancestor.join(".omne").is_dir())
.map(Path::to_path_buf);
let found = find_omne_root(tmp.path());
assert_eq!(found, expected);
}
#[test]
fn returns_none_when_start_does_not_exist() {
let tmp = TempDir::new().expect("create tempdir");
let missing = tmp.path().join("does-not-exist");
let found = find_omne_root(&missing);
assert_eq!(found, None);
}
}