use std::{error, fmt};
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Debug)]
pub enum Error {
CommandError { stdout: String, stderr: String },
IoError(String),
TimeoutError(String),
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CommandError { stdout, stderr } => {
write!(formatter, "Command error: stdout={stdout}; stderr={stderr}")
}
Self::IoError(error) | Self::TimeoutError(error) => formatter.write_str(error),
}
}
}
impl error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Error::IoError(error.to_string())
}
}
#[cfg(feature = "tokio")]
impl From<tokio::time::error::Elapsed> for Error {
fn from(error: tokio::time::error::Elapsed) -> Self {
Error::TimeoutError(error.to_string())
}
}
#[cfg(test)]
mod test {
use super::*;
fn assert_display(error: Error, expected: &str) {
assert_eq!(error.to_string(), expected);
assert!(std::error::Error::source(&error).is_none());
}
#[test]
fn test_display_messages() {
assert_display(
Error::CommandError {
stdout: "createdb output".to_string(),
stderr: "createdb error".to_string(),
},
"Command error: stdout=createdb output; stderr=createdb error",
);
assert_display(Error::IoError("io failure".to_string()), "io failure");
assert_display(
Error::TimeoutError("deadline has elapsed".to_string()),
"deadline has elapsed",
);
}
#[test]
fn test_from_io_error() {
let io_error = std::io::Error::other("test");
let error = Error::from(io_error);
assert_eq!(error.to_string(), "test");
}
#[cfg(feature = "tokio")]
#[tokio::test]
async fn test_from_elapsed_error() {
let result = tokio::time::timeout(
std::time::Duration::from_nanos(1),
tokio::time::sleep(std::time::Duration::from_secs(1)),
)
.await;
assert!(result.is_err());
let elapsed_error = result.expect_err("elapsed error");
let error = Error::from(elapsed_error);
assert_eq!(error.to_string(), "deadline has elapsed");
}
}