use super::action::Action;
#[derive(Debug, Clone, PartialEq)]
pub enum PreconditionResult {
Satisfied { rule: String, reason: String },
Violated {
rule: String,
reason: String,
situation: String,
attempted_action: String,
},
}
impl PreconditionResult {
pub fn is_satisfied(&self) -> bool {
matches!(self, PreconditionResult::Satisfied { .. })
}
pub fn satisfied(rule: &str, reason: &str) -> Self {
PreconditionResult::Satisfied {
rule: rule.to_string(),
reason: reason.to_string(),
}
}
pub fn violated(rule: &str, reason: &str, situation: &str, action: &str) -> Self {
PreconditionResult::Violated {
rule: rule.to_string(),
reason: reason.to_string(),
situation: situation.to_string(),
attempted_action: action.to_string(),
}
}
pub fn rule(&self) -> &str {
match self {
PreconditionResult::Satisfied { rule, .. } => rule,
PreconditionResult::Violated { rule, .. } => rule,
}
}
pub fn reason(&self) -> &str {
match self {
PreconditionResult::Satisfied { reason, .. } => reason,
PreconditionResult::Violated { reason, .. } => reason,
}
}
}
pub trait Precondition<A: Action> {
fn check(&self, situation: &A::Sit, action: &A) -> PreconditionResult;
fn describe(&self) -> &str;
}