#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum RunTimeErrorOption {
ShowDialog = 0,
Continue = 1,
Ignore = 2,
Abort = 3,
Retry = 4,
}
impl RunTimeErrorOption {
pub const ALL: [Self; 5] = [
Self::ShowDialog,
Self::Continue,
Self::Ignore,
Self::Abort,
Self::Retry,
];
#[must_use]
pub const fn bits(self) -> i32 {
self as i32
}
pub const fn from_bits(raw: i32) -> Result<Self, i32> {
Ok(match raw {
0 => Self::ShowDialog,
1 => Self::Continue,
2 => Self::Ignore,
3 => Self::Abort,
4 => Self::Retry,
other => return Err(other),
})
}
#[must_use]
pub const fn is_interactive(self) -> bool {
matches!(self, Self::ShowDialog)
}
}
#[cfg(test)]
mod tests {
use super::RunTimeErrorOption;
#[test]
fn only_the_dialog_response_blocks() {
assert!(RunTimeErrorOption::ShowDialog.is_interactive());
for option in [
RunTimeErrorOption::Continue,
RunTimeErrorOption::Ignore,
RunTimeErrorOption::Abort,
RunTimeErrorOption::Retry,
] {
assert!(!option.is_interactive(), "{option:?}");
}
}
#[test]
fn every_option_round_trips_through_its_raw_value() {
for option in RunTimeErrorOption::ALL {
assert_eq!(RunTimeErrorOption::from_bits(option.bits()), Ok(option));
}
}
#[test]
fn an_unknown_value_is_returned_rather_than_guessed() {
assert_eq!(RunTimeErrorOption::from_bits(99), Err(99));
}
}