use super::RunError;
use dpm_model::{ActorId, ActorKind, RunId, RunRecord, RunState};
pub fn authorize(
record: &RunRecord,
writer: &ActorId,
action: &'static str,
) -> Result<(), RunError> {
authorize_writer(writer, &record.executor, record.id, action)
}
pub(super) fn authorize_writer(
writer: &ActorId,
executor: &ActorId,
run: RunId,
action: &'static str,
) -> Result<(), RunError> {
if writer == executor || writer.kind != ActorKind::Agent {
return Ok(());
}
Err(RunError::ActorNotAllowed {
actor: writer.clone(),
action,
run,
executor: executor.clone(),
})
}
pub fn check_transition(run: RunId, from: RunState, to: RunState) -> Result<(), RunError> {
if from.is_terminal() {
return Err(RunError::Terminal { run, state: from });
}
if from == to {
return Err(RunError::InvalidTransition { run, from, to });
}
Ok(())
}