use std::string::FromUtf8Error;
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
ArchiveError(postgresql_archive::Error),
#[error("Command error: stdout={stdout}; stderr={stderr}")]
CommandError { stdout: String, stderr: String },
#[error("{0}")]
CreateDatabaseError(String),
#[error(transparent)]
DatabaseError(#[from] sqlx::Error),
#[error("{0}")]
DatabaseExistsError(String),
#[error("{0}")]
DatabaseInitializationError(String),
#[error("{0}")]
DatabaseStartError(String),
#[error("{0}")]
DatabaseStopError(String),
#[error("{0}")]
DropDatabaseError(String),
#[error("Invalid URL: {url}; {message}")]
InvalidUrl { url: String, message: String },
#[error("{0}")]
IoError(String),
#[error(transparent)]
ParseError(#[from] semver::Error),
}
impl From<postgresql_archive::Error> for Error {
fn from(error: postgresql_archive::Error) -> Self {
Error::ArchiveError(error)
}
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Error::IoError(error.to_string())
}
}
impl From<FromUtf8Error> for Error {
fn from(error: FromUtf8Error) -> Self {
Error::IoError(error.to_string())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_from_archive_error() {
let archive_error = postgresql_archive::Error::VersionNotFound("test".to_string());
let error = Error::from(archive_error);
assert_eq!(error.to_string(), "version not found for 'test'");
}
#[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");
}
#[test]
fn test_from_utf8_error() {
let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
let from_utf8_error = String::from_utf8(invalid_utf8).expect_err("from utf8 error");
let error = Error::from(from_utf8_error);
assert_eq!(
error.to_string(),
"invalid utf-8 sequence of 1 bytes from index 1"
);
}
}