use thiserror::Error;
#[derive(Debug, Error, PartialEq, Eq)]
pub enum StateError {
#[error("state machine must define at least one transition")]
NoTransitions,
#[error("state name cannot be empty or whitespace-only")]
EmptyState,
#[error("state name {state:?} must not begin or end with Unicode whitespace")]
AmbiguousStateName {
state: String,
},
#[error("self-transitions are not allowed for state {state:?}")]
SelfTransition {
state: String,
},
#[error("transition from {from:?} to {to:?} is not allowed")]
InvalidTransition {
from: String,
to: String,
},
#[error("instance initial state {state:?} does not exist")]
UnknownInitialState {
state: String,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_state_display_message() {
let error = StateError::EmptyState;
assert_eq!(
error.to_string(),
"state name cannot be empty or whitespace-only"
);
}
#[test]
fn no_transitions_display_message() {
let error = StateError::NoTransitions;
assert_eq!(
error.to_string(),
"state machine must define at least one transition"
);
}
#[test]
fn self_transition_display_message_includes_state() {
let error = StateError::SelfTransition {
state: "start".to_owned(),
};
assert_eq!(
error.to_string(),
"self-transitions are not allowed for state \"start\""
);
}
#[test]
fn invalid_transition_display_message_includes_endpoints() {
let error = StateError::InvalidTransition {
from: "start".to_owned(),
to: "finish".to_owned(),
};
assert_eq!(
error.to_string(),
"transition from \"start\" to \"finish\" is not allowed"
);
}
#[test]
fn ambiguous_state_name_display_message() {
let error = StateError::AmbiguousStateName {
state: " start ".to_owned(),
};
assert_eq!(
error.to_string(),
"state name \" start \" must not begin or end with Unicode whitespace"
);
}
#[test]
fn invalid_transition_display_escapes_whitespace() {
let error = StateError::InvalidTransition {
from: "\nstart".to_owned(),
to: "finish\t".to_owned(),
};
assert_eq!(
error.to_string(),
"transition from \"\\nstart\" to \"finish\\t\" is not allowed"
);
}
#[test]
fn unknown_initial_state_display_message_includes_state() {
let error = StateError::UnknownInitialState {
state: "missing".to_owned(),
};
assert_eq!(
error.to_string(),
"instance initial state \"missing\" does not exist"
);
}
}