kaizen/core/
home_paths.rs1use anyhow::Result;
5use std::path::{Path, PathBuf};
6
7pub fn root(workspace: &Path) -> Result<PathBuf> {
8 let home =
9 super::paths::kaizen_dir().ok_or_else(|| anyhow::anyhow!("KAIZEN_HOME / HOME unset"))?;
10 anyhow::ensure!(
11 !super::paths::path_is_within(&home, workspace),
12 "KAIZEN_HOME must be outside target repository"
13 );
14 Ok(home)
15}
16
17pub fn file_for_write(workspace: &Path, relative: &Path) -> Result<PathBuf> {
18 let home = root(workspace)?;
19 std::fs::create_dir_all(&home)?;
20 anyhow::ensure!(
21 !super::paths::path_is_within(&home, workspace),
22 "KAIZEN_HOME must be outside target repository"
23 );
24 super::paths::descendant_file_for_write(&home, relative)
25}
26
27pub fn sqlite_file_for_write(workspace: &Path, name: &str) -> Result<PathBuf> {
28 let path = file_for_write(workspace, Path::new(name))?;
29 ["-journal", "-wal", "-shm"]
30 .into_iter()
31 .try_for_each(|suffix| {
32 let sidecar = format!("{name}{suffix}");
33 super::paths::descendant_path(path.parent().unwrap(), Path::new(&sidecar)).map(drop)
34 })?;
35 Ok(path)
36}