use super::{CapturedConfiguration, GateError, OwnerState, Result, RuntimeOwner};
use crate::config_gate::GateSnapshot;
pub(super) fn prepare(
owner: &RuntimeOwner,
state: &mut OwnerState,
id: u64,
) -> Result<(CapturedConfiguration, GateSnapshot)> {
let mut target = GateSnapshot::from_store(&state.store.rollback_configuration(id)?)?;
target.inputs.retain(|key, _| !key.is_project());
let delivery = state
.store
.body()?
.generations
.get(&id)
.and_then(|entry| entry.delivery.as_ref())
.ok_or_else(|| {
GateError::new(
"rollback_snapshot_unavailable",
"retained raw configuration is absent",
)
})?;
let incompatible: Vec<_> = state
.active_declarations
.iter()
.filter_map(|(module, connection)| {
let (_, admitted) = state.declarations.get(connection)?;
(delivery.declarations.get(module) != Some(admitted)).then_some(module.as_str())
})
.collect();
if !incompatible.is_empty() {
return Err(GateError::new(
"rollback_declaration_mismatch",
format!(
"Rollback target {id} was approved for a different build of {}. Change the settings values with an ordinary edit instead.",
incompatible.join(", ")
),
));
}
Ok((owner.capturer.capture_retained(&target, delivery)?, target))
}