use thiserror::Error;
#[derive(Error, Debug)]
pub enum GoapError {
#[error("Operation failed: {0}")]
OperationFailed(String),
#[error("Plan error: {0}")]
Plan(String),
#[error("Failed to produce a plan")]
PlanFailed,
#[error("No valid plan found to achieve the goal")]
NoPlanFound,
#[error("No plan has been generated yet")]
NoPlanGenerated,
#[error("Invalid state transition: {0}")]
InvalidStateTransition(String),
#[error("Sensor error: {0}")]
Sensor(String),
#[error("Sensor has multiple types")]
SensorMultipleType,
#[error("Sensor does not exist")]
SensorDoesNotExist,
#[error("Sensor already in collection: {0}")]
SensorAlreadyInCollection(String),
#[error("Action error: {0}")]
Action(String),
#[error("Action has multiple types")]
ActionMultipleType,
#[error("Action already in collection: {0}")]
ActionAlreadyInCollection(String),
#[error("Action cost must be positive")]
InvalidActionCost,
#[error("Action precondition not met: {0}")]
PreconditionNotMet(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Command execution failed: {0}")]
CommandExecution(String),
#[error("Graph error: {0}")]
Graph(String),
#[error("No path found in graph")]
NoPathFound,
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Other error: {0}")]
Other(String),
}
pub type Result<T> = std::result::Result<T, GoapError>;
#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;
#[test]
fn test_no_plan_found_display() {
let err = GoapError::NoPlanFound;
assert_eq!(
format!("{}", err),
"No valid plan found to achieve the goal"
);
}
#[test]
fn test_invalid_state_transition_display() {
let err = GoapError::InvalidStateTransition("foo".to_string());
assert_eq!(format!("{}", err), "Invalid state transition: foo");
}
#[test]
fn test_precondition_not_met_display() {
let err = GoapError::PreconditionNotMet("bar".to_string());
assert_eq!(format!("{}", err), "Action precondition not met: bar");
}
#[test]
fn test_invalid_action_cost_display() {
let err = GoapError::InvalidActionCost;
assert_eq!(format!("{}", err), "Action cost must be positive");
}
#[test]
fn test_error_trait() {
let err = GoapError::NoPlanFound;
let _ = err.source(); }
}