use openmoat_core::{Decision, Policy};
use openmoat_hosts::{HookRequest, Host};
use crate::context;
use crate::home::Home;
use crate::integrity::Drift;
use crate::protection::required;
pub(super) struct Call<'a> {
pub home: &'a Home,
pub host: Host,
pub drifted: &'a [Drift],
pub within_run: bool,
}
pub(super) fn unmet(call: &Call<'_>, policy: &Policy) -> Option<Decision> {
required::unmet(policy, call.host, || {
required::in_force(call.home, call.host, call.drifted, call.within_run)
})
}
pub(super) fn ungoverned(call: &Call<'_>, request: &HookRequest, decision: Decision) -> Decision {
let cwd = request
.cwd
.as_deref()
.map(std::path::PathBuf::from)
.map_or_else(std::env::current_dir, Ok);
let policy = cwd
.map_err(anyhow::Error::from)
.and_then(|cwd| context::eval_context(call.home, Some(&cwd), None))
.and_then(|ctx| required::policy_in(call.home, &ctx));
policy
.ok()
.and_then(|policy| unmet(call, &policy))
.unwrap_or(decision)
}