use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use openmoat_hosts::Host;
use super::Lock;
#[derive(Clone, Copy)]
pub(super) struct Form {
pub shape: fn(serde_json::Value) -> Option<serde_json::Value>,
pub yaml: bool,
}
pub(super) fn form_of(host: Host) -> Option<Form> {
let install = host.spec().install.as_ref()?;
Some(Form {
shape: install.pinned_form?,
yaml: install.hook_file.is_yaml(),
})
}
fn legacy_owner(stored: &Path) -> Option<Host> {
let name = stored.file_name()?;
Host::ALL.into_iter().find(|host| {
host.spec()
.install
.as_ref()
.is_some_and(|install| install.pinned_form.is_some() && name == install.config_file)
})
}
impl Lock {
pub(super) fn form_host(&self, stored: &str) -> Option<Host> {
let host = match &self.pinned_forms {
Some(forms) => forms.get(stored)?.parse().ok()?,
None => legacy_owner(Path::new(stored))?,
};
form_of(host).map(|_| host)
}
pub(super) fn form(&self, stored: &str) -> Option<Form> {
form_of(self.form_host(stored)?)
}
pub(super) fn owners(&self) -> Vec<(PathBuf, Host)> {
self.entries
.keys()
.filter_map(|stored| Some((PathBuf::from(stored), self.form_host(stored)?)))
.collect()
}
pub(super) fn owner_ids(&self) -> BTreeMap<String, String> {
self.owners()
.into_iter()
.map(|(path, host)| (path.to_string_lossy().into_owned(), host.id().to_owned()))
.collect()
}
}
#[cfg(test)]
mod tests;