use crate::error::DynamoError;
pub trait MaybeError {
fn from_err(err: impl std::error::Error + 'static) -> Self;
fn err(&self) -> Option<DynamoError>;
fn is_ok(&self) -> bool {
!self.is_err()
}
fn is_err(&self) -> bool {
self.err().is_some()
}
}
#[cfg(test)]
mod tests {
use super::*;
struct TestError {
error: Option<DynamoError>,
}
impl MaybeError for TestError {
fn from_err(err: impl std::error::Error + 'static) -> Self {
TestError {
error: Some(DynamoError::from(
Box::new(err) as Box<dyn std::error::Error + 'static>
)),
}
}
fn err(&self) -> Option<DynamoError> {
self.error.clone()
}
}
#[test]
fn test_maybe_error_default_implementations() {
let dynamo_err = DynamoError::msg("Test error");
let err = TestError::from_err(dynamo_err);
assert!(err.err().unwrap().to_string().contains("Test error"));
assert!(!err.is_ok());
assert!(err.is_err());
}
#[test]
fn test_from_std_error() {
let std_err = std::io::Error::other("io failure");
let test_err = TestError::from_err(std_err);
assert!(test_err.is_err());
assert!(test_err.err().unwrap().to_string().contains("io failure"));
}
#[test]
fn test_not_error() {
let test = TestError { error: None };
assert!(test.is_ok());
assert!(!test.is_err());
assert!(test.err().is_none());
}
}