use std::collections::HashSet;
use crate::entity::{Entity, EntityId};
use crate::workspace::MountCapability;
use super::{Engine, EngineError, UpdateEntityArgs};
pub(super) struct ResolvedUpdate {
pub(super) args: UpdateEntityArgs,
pub(super) mount_idx: usize,
pub(super) mem: String,
pub(super) entity: Entity,
pub(super) prev_body_targets: HashSet<EntityId>,
}
impl Engine {
pub(super) fn resolve_update(
&self,
args: UpdateEntityArgs,
) -> Result<ResolvedUpdate, EngineError> {
let id = &args.id;
let mem = id.mem().to_string();
let mount_idx = self
.mounts
.iter()
.position(|m| m.mount.mem == mem)
.ok_or_else(|| self.unknown_mem_error(&mem))?;
if self.mounts[mount_idx].mount.capability != MountCapability::Write {
return Err(EngineError::ReadOnlyMount(mem));
}
let entity = self
.store
.get(id)
.ok_or_else(|| EngineError::NotFound { id: id.to_string() })?;
let prev_body_targets = super::super::collect_body_link_targets(entity);
if entity.stub {
return Err(EngineError::StubNotUpdatable { id: id.to_string() });
}
if !args.dry_run
&& let Some(expected) = args.expected_hash.as_deref()
&& entity.content_hash != expected
{
return Err(EngineError::HashMismatch {
id: id.to_string(),
current: entity.content_hash.clone(),
is_stub: entity.stub,
});
}
let entity = entity.clone();
Ok(ResolvedUpdate {
args,
mount_idx,
mem,
entity,
prev_body_targets,
})
}
}