use crate::{FileTree, RelativePath};
pub const MODERN_PRIMARY_PATHS: [&str; 6] = [
"morphir.toml",
"morphir.yaml",
".morphir/morphir.toml",
".morphir/morphir.yaml",
".config/morphir/config.toml",
".config/morphir/config.yaml",
];
#[must_use]
pub fn modern_primary_candidates(directory: &RelativePath) -> Vec<RelativePath> {
MODERN_PRIMARY_PATHS
.into_iter()
.map(|candidate| {
directory
.join(candidate)
.expect("built-in candidate paths are confined")
})
.collect()
}
#[must_use]
pub fn found_primary_candidates(tree: &FileTree, directory: &RelativePath) -> Vec<RelativePath> {
let modern = modern_primary_candidates(directory)
.into_iter()
.filter(|candidate| tree.contains_file(candidate))
.collect::<Vec<_>>();
if !modern.is_empty() {
return modern;
}
let legacy = directory
.join("morphir.json")
.expect("legacy candidate path is confined");
tree.contains_file(&legacy)
.then_some(legacy)
.into_iter()
.collect()
}
#[must_use]
pub fn adjacent_user_candidates(primary: &RelativePath) -> Vec<RelativePath> {
let primary_name = primary.as_str().rsplit('/').next().unwrap_or_default();
let names = match primary_name {
"morphir.toml" | "morphir.yaml" => Some(["morphir.user.toml", "morphir.user.yaml"]),
"config.toml" | "config.yaml" => Some(["config.user.toml", "config.user.yaml"]),
_ => None,
};
names
.into_iter()
.flatten()
.map(|name| {
primary
.parent()
.join(name)
.expect("built-in user override paths are confined")
})
.collect()
}
#[must_use]
pub fn found_adjacent_user_candidates(
tree: &FileTree,
primary: &RelativePath,
) -> Vec<RelativePath> {
adjacent_user_candidates(primary)
.into_iter()
.filter(|candidate| tree.contains_file(candidate))
.collect()
}