use do_memory_core::Error;
#[macro_export]
macro_rules! storage_err {
($msg:literal, $err:expr) => {{
Error::Storage(format!("{}: {}", $msg, $err))
}};
}
pub fn storage_error<
C: std::fmt::Display + Send + Sync + 'static,
E: std::error::Error + Send + Sync + 'static,
>(
context: C,
error: E,
) -> Error {
Error::Storage(format!("{}: {}", context, error))
}
pub fn into_storage_error<E: std::error::Error + Send + Sync + 'static>(error: E) -> Error {
Error::Storage(error.to_string())
}
#[macro_export]
macro_rules! wrap_storage_err {
($expr:expr, $msg:literal) => {{
$expr.map_err(|e| $crate::storage_err!($msg, e))
}};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_storage_error_with_context() {
let original = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let err = storage_error("Failed to read file", original);
let err_str = err.to_string();
assert!(err_str.contains("Failed to read file"));
assert!(err_str.contains("file not found"));
}
#[test]
fn test_storage_err_macro() {
let original = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "access denied");
let err = storage_err!("Permission error", original);
let err_str = err.to_string();
assert!(err_str.contains("Permission error"));
assert!(err_str.contains("access denied"));
}
#[test]
fn test_into_storage_error() {
let original = std::io::Error::new(std::io::ErrorKind::Other, "some error");
let err = into_storage_error(original);
assert!(err.to_string().contains("some error"));
}
#[test]
fn test_nested_context() {
let inner = std::io::Error::new(std::io::ErrorKind::NotFound, "file.txt");
let outer = std::io::Error::new(std::io::ErrorKind::InvalidInput, "invalid path");
let err = storage_error("Outer operation failed", outer);
let err2 = storage_error("Inner operation failed", inner);
assert!(err.to_string().contains("Outer operation failed"));
assert!(err2.to_string().contains("Inner operation failed"));
}
}