use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
NotFound {
key: String,
},
Connection(String),
Query(String),
InvalidKey {
reason: String,
},
InvalidValue {
reason: String,
},
CasMismatch {
key: String,
},
Expired {
key: String,
},
Transaction(String),
TableNotFound {
table: String,
},
Config(String),
Io(std::io::Error),
#[cfg(feature = "serde")]
Serialization(String),
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::NotFound { key } => write!(f, "key not found: {}", key),
Error::Connection(msg) => write!(f, "connection error: {}", msg),
Error::Query(msg) => write!(f, "query error: {}", msg),
Error::InvalidKey { reason } => write!(f, "invalid key: {}", reason),
Error::InvalidValue { reason } => write!(f, "invalid value: {}", reason),
Error::CasMismatch { key } => {
write!(f, "compare-and-swap failed for key: {}", key)
}
Error::Expired { key } => write!(f, "key has expired: {}", key),
Error::Transaction(msg) => write!(f, "transaction error: {}", msg),
Error::TableNotFound { table } => {
write!(f, "table not found: {}", table)
}
Error::Config(msg) => write!(f, "configuration error: {}", msg),
Error::Io(e) => write!(f, "I/O error: {}", e),
#[cfg(feature = "serde")]
Error::Serialization(msg) => write!(f, "serialization error: {}", msg),
}
}
}
impl From<postgres::Error> for Error {
fn from(err: postgres::Error) -> Self {
if err.is_closed() {
Error::Connection(err.to_string())
} else {
Error::Query(err.to_string())
}
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err)
}
}
impl Error {
#[inline]
pub fn is_not_found(&self) -> bool {
matches!(self, Error::NotFound { .. })
}
#[inline]
pub fn is_expired(&self) -> bool {
matches!(self, Error::Expired { .. })
}
#[inline]
pub fn is_cas_mismatch(&self) -> bool {
matches!(self, Error::CasMismatch { .. })
}
#[inline]
pub fn is_connection(&self) -> bool {
matches!(self, Error::Connection(_))
}
#[inline]
pub fn is_recoverable(&self) -> bool {
matches!(
self,
Error::Connection(_) | Error::CasMismatch { .. } | Error::Expired { .. }
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = Error::NotFound {
key: "test".to_string(),
};
assert_eq!(err.to_string(), "key not found: test");
let err = Error::CasMismatch {
key: "test".to_string(),
};
assert!(err.to_string().contains("compare-and-swap"));
}
#[test]
fn test_error_predicates() {
let not_found = Error::NotFound {
key: "test".to_string(),
};
assert!(not_found.is_not_found());
assert!(!not_found.is_expired());
assert!(!not_found.is_recoverable());
let cas = Error::CasMismatch {
key: "test".to_string(),
};
assert!(cas.is_cas_mismatch());
assert!(cas.is_recoverable());
let conn = Error::Connection("test".to_string());
assert!(conn.is_connection());
assert!(conn.is_recoverable());
}
}