use std::error::Error;
use ohno::{ErrorExt, OhnoCore};
type Core = OhnoCore;
use ohno::OhnoCore as RenamedCore;
#[derive(ohno::Error)]
#[display("alias failed for {path}")]
pub struct AliasedCoreError {
pub path: String,
#[error]
inner: Core,
}
#[derive(ohno::Error)]
#[display("rename failed for {path}")]
pub struct RenamedCoreError {
pub path: String,
#[error]
inner: RenamedCore,
}
#[derive(ohno::Error)]
#[display("tuple alias failed for {0}")]
pub struct TupleAliasedCoreError(pub String, #[error] Core);
#[test]
fn aliased_core_is_the_error_representation() {
let cause = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "root cause");
let error = AliasedCoreError::caused_by("/etc/config.toml".to_string(), cause);
let rendered = error.to_string();
assert!(rendered.starts_with("alias failed for /etc/config.toml"), "got: {rendered}");
assert!(rendered.contains("caused by: root cause"), "got: {rendered}");
assert_eq!(error.source().expect("source").to_string(), "root cause");
}
#[test]
fn renamed_core_is_the_error_representation() {
let cause = std::io::Error::new(std::io::ErrorKind::NotFound, "missing");
let error = RenamedCoreError::caused_by("/tmp/x".to_string(), cause);
assert!(error.to_string().starts_with("rename failed for /tmp/x"), "got: {error}");
assert_eq!(error.source().expect("source").to_string(), "missing");
}
#[test]
fn aliased_core_in_tuple_position_is_the_error_representation() {
let cause = std::io::Error::new(std::io::ErrorKind::TimedOut, "timed out");
let error = TupleAliasedCoreError::caused_by("/var/run".to_string(), cause);
assert!(error.to_string().starts_with("tuple alias failed for /var/run"), "got: {error}");
assert_eq!(error.source().expect("source").to_string(), "timed out");
}
#[test]
fn aliased_core_carries_no_source_when_nothing_is_wrapped() {
let error = AliasedCoreError::new("/tmp/y".to_string());
assert_eq!(error.message(), "alias failed for /tmp/y");
assert!(error.source().is_none());
}