use anyhow::{Context as _, Result};
use openmoat_core::Policy;
use crate::approvals::{DirGrant, Overlay, Places};
use crate::home::Home;
pub(super) struct Migration {
pub before: String,
pub after: String,
overlay: Overlay,
made: Vec<DirGrant>,
}
impl Migration {
pub fn for_home(home: &Home, policy_text: &str) -> Result<Option<Self>> {
let current = Overlay::load(&home.overlay_path())?;
if current.old_dir_rules().next().is_none() {
return Ok(None);
}
let policy = Policy::parse(policy_text).context("reading the upgraded policy")?;
let mut overlay = current.clone();
let made = overlay.migrate_dirs(&policy, &Places::here(home)?);
Ok(Some(Self {
before: current.text()?,
after: overlay.text()?,
overlay,
made,
}))
}
pub fn lines(&self) -> Vec<String> {
self.made.iter().map(line).collect()
}
pub fn save(&self, home: &Home) -> Result<()> {
self.overlay.save(&home.overlay_path())
}
}
fn line(grant: &DirGrant) -> String {
let kept = match grant.except.len() {
0 => String::new(),
n => format!(
"; it keeps out {} the policy no longer names",
crate::report::count(n, "path")
),
};
format!(
"`{}` ({}): what the policy holds for review in it stays held, protections \
added later included{kept}",
grant.id,
grant.dir.join(", ")
)
}