use crate::sys;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum EngineError {
EndOfFile,
WrongCommand,
Unsupported,
InvalidName,
Internal,
}
impl EngineError {
#[must_use]
pub fn to_mysql_errno(self) -> i32 {
match self {
Self::EndOfFile => sys::HA_ERR_END_OF_FILE,
Self::WrongCommand => sys::HA_ERR_WRONG_COMMAND,
Self::Unsupported => sys::HA_ERR_UNSUPPORTED,
Self::InvalidName => sys::HA_ERR_WRONG_TABLE_NAME,
Self::Internal => sys::HA_ERR_INTERNAL_ERROR,
}
}
}
impl core::fmt::Display for EngineError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let message = match self {
Self::EndOfFile => "end of table or index scan",
Self::WrongCommand => "operation not supported by the engine",
Self::Unsupported => "operation unsupported on this engine",
Self::InvalidName => "invalid table or schema name",
Self::Internal => "internal engine error",
};
f.write_str(message)
}
}
impl std::error::Error for EngineError {}
pub type EngineResult<T = ()> = Result<T, EngineError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn errno_mapping_matches_ha_err_codes() {
assert_eq!(
EngineError::EndOfFile.to_mysql_errno(),
sys::HA_ERR_END_OF_FILE
);
assert_eq!(
EngineError::WrongCommand.to_mysql_errno(),
sys::HA_ERR_WRONG_COMMAND
);
assert_eq!(
EngineError::Unsupported.to_mysql_errno(),
sys::HA_ERR_UNSUPPORTED
);
assert_eq!(
EngineError::InvalidName.to_mysql_errno(),
sys::HA_ERR_WRONG_TABLE_NAME
);
assert_eq!(
EngineError::Internal.to_mysql_errno(),
sys::HA_ERR_INTERNAL_ERROR
);
}
#[test]
fn display_renders_a_distinct_message_per_variant() {
let variants = [
EngineError::EndOfFile,
EngineError::WrongCommand,
EngineError::Unsupported,
EngineError::InvalidName,
EngineError::Internal,
];
let messages: Vec<String> = variants.iter().map(ToString::to_string).collect();
assert!(messages.iter().all(|m| !m.is_empty()));
for (i, a) in messages.iter().enumerate() {
for b in &messages[i + 1..] {
assert_ne!(a, b);
}
}
}
#[test]
fn usable_as_std_error() {
fn take(_: &dyn std::error::Error) {}
take(&EngineError::Internal);
}
}