use std::path::{Path, PathBuf};
use dbmd_core::Store;
use crate::error::CliError;
pub(crate) fn lexical_absolute_before_open(file: &Path) -> Result<PathBuf, CliError> {
let parent = file.parent().unwrap_or_else(|| Path::new("."));
let parent = std::fs::canonicalize(parent).map_err(CliError::from)?;
let leaf = file.file_name().ok_or_else(|| {
CliError::runtime(format!("file path `{}` has no filename", file.display()))
})?;
Ok(parent.join(leaf))
}
pub(crate) fn locate_store(file: &Path) -> Result<Store, CliError> {
let start = file.parent().unwrap_or(Path::new("."));
let start = std::fs::canonicalize(start).unwrap_or_else(|_| start.to_path_buf());
match start.ancestors().find(|path| Store::is_db_md_store(path)) {
Some(root) => Store::open_strict(root).map_err(CliError::from),
None => Store::open_strict(&start).map_err(CliError::from),
}
}
pub(crate) fn store_relative(store: &Store, file: &Path) -> PathBuf {
store
.capability_relative(file)
.map(Path::to_path_buf)
.unwrap_or_else(|_| file.to_path_buf())
}