Skip to content

refactor!: rename ArchiveError and EmbeddedError #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions postgresql_archive/src/archive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Manage PostgreSQL archive
#![allow(dead_code)]
use crate::error::ArchiveError::{AssetHashNotFound, AssetNotFound, ReleaseNotFound, Unexpected};
use crate::error::Error::{AssetHashNotFound, AssetNotFound, ReleaseNotFound, Unexpected};
use crate::error::Result;
use crate::github::{Asset, Release};
use crate::version::Version;
Expand Down Expand Up @@ -145,7 +145,7 @@ pub async fn get_version(version: &Version) -> Result<Version> {
/// Gets the assets for a given [version](Version) of PostgreSQL and
/// [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html).
/// If the [version](Version) or [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html)
/// is not found, then an [error](crate::error::ArchiveError) is returned.
/// is not found, then an [error](crate::error::Error) is returned.
///
/// Two assets are returned. The first [asset](Asset) is the archive, and the second [asset](Asset) is the archive hash.
async fn get_asset<S: AsRef<str>>(version: &Version, target: S) -> Result<(Version, Asset, Asset)> {
Expand Down Expand Up @@ -177,7 +177,7 @@ async fn get_asset<S: AsRef<str>>(version: &Version, target: S) -> Result<(Versi

/// Gets the archive for a given [version](Version) of PostgreSQL for the current target.
/// If the [version](Version) is not found for this target, then an
/// [error](crate::error::ArchiveError) is returned.
/// [error](crate::error::Error) is returned.
///
/// Returns the archive bytes and the archive hash.
pub async fn get_archive(version: &Version) -> Result<(Version, Bytes, String)> {
Expand All @@ -187,7 +187,7 @@ pub async fn get_archive(version: &Version) -> Result<(Version, Bytes, String)>
/// Gets the archive for a given [version](Version) of PostgreSQL and
/// [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html).
/// If the [version](Version) or [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html)
/// is not found, then an [error](crate::error::ArchiveError) is returned.
/// is not found, then an [error](crate::error::Error) is returned.
///
/// Returns the archive bytes and the archive hash.
pub async fn get_archive_for_target<S: AsRef<str>>(
Expand Down
6 changes: 3 additions & 3 deletions postgresql_archive/src/blocking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ lazy_static! {

/// Gets the version of PostgreSQL for the specified [version](Version). If the version minor or release is not
/// specified, then the latest version is returned. If a release for the [version](Version) is not found, then a
/// [ReleaseNotFound](crate::ArchiveError::ReleaseNotFound) error is returned.
/// [ReleaseNotFound](crate::Error::ReleaseNotFound) error is returned.
pub fn get_version(version: &Version) -> crate::Result<Version> {
RUNTIME
.handle()
Expand All @@ -18,7 +18,7 @@ pub fn get_version(version: &Version) -> crate::Result<Version> {

/// Gets the archive for a given [version](Version) of PostgreSQL for the current target.
/// If the [version](Version) is not found for this target, then an
/// [error](crate::ArchiveError) is returned.
/// [error](crate::Error) is returned.
///
/// Returns the archive bytes and the archive hash.
pub fn get_archive(version: &Version) -> crate::Result<(Version, Bytes, String)> {
Expand All @@ -30,7 +30,7 @@ pub fn get_archive(version: &Version) -> crate::Result<(Version, Bytes, String)>
/// Gets the archive for a given [version](Version) of PostgreSQL and
/// [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html).
/// If the [version](Version) or [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html)
/// is not found, then an [error](crate::error::ArchiveError) is returned.
/// is not found, then an [error](crate::error::Error) is returned.
///
/// Returns the archive bytes and the archive hash.
pub fn get_archive_for_target<S: AsRef<str>>(
Expand Down
56 changes: 27 additions & 29 deletions postgresql_archive/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use thiserror::Error;

/// PostgreSQL archive result type
pub type Result<T, E = ArchiveError> = core::result::Result<T, E>;
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// PostgreSQL archive errors
#[derive(Debug, Error)]
pub enum ArchiveError {
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Asset not found
#[error("asset [{0}] not found")]
AssetNotFound(String),
Expand All @@ -29,45 +27,45 @@ pub enum ArchiveError {
Unexpected(String),
}

/// Converts a [`regex::Error`] into an [`ParseError`](ArchiveError::ParseError)
impl From<regex::Error> for ArchiveError {
/// Converts a [`regex::Error`] into an [`ParseError`](Error::ParseError)
impl From<regex::Error> for Error {
fn from(error: regex::Error) -> Self {
ArchiveError::ParseError(error.into())
Error::ParseError(error.into())
}
}

/// Converts a [`reqwest::Error`] into an [`IoError`](ArchiveError::IoError)
impl From<reqwest::Error> for ArchiveError {
/// Converts a [`reqwest::Error`] into an [`IoError`](Error::IoError)
impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Self {
ArchiveError::IoError(error.into())
Error::IoError(error.into())
}
}

/// Converts a [`std::io::Error`] into an [`IoError`](ArchiveError::IoError)
impl From<std::io::Error> for ArchiveError {
/// Converts a [`std::io::Error`] into an [`IoError`](Error::IoError)
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
ArchiveError::IoError(error.into())
Error::IoError(error.into())
}
}

/// Converts a [`std::num::ParseIntError`] into an [`ParseError`](ArchiveError::ParseError)
impl From<std::num::ParseIntError> for ArchiveError {
/// Converts a [`std::num::ParseIntError`] into an [`ParseError`](Error::ParseError)
impl From<std::num::ParseIntError> for Error {
fn from(error: std::num::ParseIntError) -> Self {
ArchiveError::ParseError(error.into())
Error::ParseError(error.into())
}
}

/// Converts a [`std::path::StripPrefixError`] into an [`ParseError`](ArchiveError::ParseError)
impl From<std::path::StripPrefixError> for ArchiveError {
/// Converts a [`std::path::StripPrefixError`] into an [`ParseError`](Error::ParseError)
impl From<std::path::StripPrefixError> for Error {
fn from(error: std::path::StripPrefixError) -> Self {
ArchiveError::ParseError(error.into())
Error::ParseError(error.into())
}
}

/// Converts a [`anyhow::Error`] into an [`Unexpected`](ArchiveError::Unexpected)
impl From<anyhow::Error> for ArchiveError {
/// Converts a [`anyhow::Error`] into an [`Unexpected`](Error::Unexpected)
impl From<anyhow::Error> for Error {
fn from(error: anyhow::Error) -> Self {
ArchiveError::Unexpected(error.to_string())
Error::Unexpected(error.to_string())
}
}

Expand All @@ -82,7 +80,7 @@ mod test {
#[test]
fn test_from_regex_error() {
let regex_error = regex::Error::Syntax("test".to_string());
let error = ArchiveError::from(regex_error);
let error = Error::from(regex_error);
assert_eq!(error.to_string(), "test");
}

Expand All @@ -91,15 +89,15 @@ mod test {
let result = reqwest::get("https://a.com").await;
assert!(result.is_err());
if let Err(error) = result {
let error = ArchiveError::from(error);
let error = Error::from(error);
assert!(error.to_string().contains("https://a.com"));
}
}

#[test]
fn test_from_io_error() {
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "test");
let error = ArchiveError::from(io_error);
let error = Error::from(io_error);
assert_eq!(error.to_string(), "test");
}

Expand All @@ -108,7 +106,7 @@ mod test {
let result = u64::from_str("test");
assert!(result.is_err());
if let Err(error) = result {
let error = ArchiveError::from(error);
let error = Error::from(error);
assert_eq!(error.to_string(), "invalid digit found in string");
}
}
Expand All @@ -119,15 +117,15 @@ mod test {
let result = path.strip_prefix("foo");
assert!(result.is_err());
if let Err(error) = result {
let error = ArchiveError::from(error);
let error = Error::from(error);
assert_eq!(error.to_string(), "prefix not found");
}
}

#[test]
fn test_from_anyhow_error() {
let anyhow_error = anyhow::Error::msg("test");
let error = ArchiveError::from(anyhow_error);
let error = Error::from(anyhow_error);
assert_eq!(error.to_string(), "test");
}
}
2 changes: 1 addition & 1 deletion postgresql_archive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ mod github;
mod version;

pub use archive::{extract, get_archive, get_archive_for_target, get_version};
pub use error::{ArchiveError, Result};
pub use error::{Error, Result};
#[allow(deprecated)]
pub use version::{Version, LATEST, V12, V13, V14, V15, V16};
6 changes: 3 additions & 3 deletions postgresql_archive/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! PostgreSQL version
#![allow(dead_code)]

use crate::error::ArchiveError::InvalidVersion;
use crate::error::{ArchiveError, Result};
use crate::error::Error::InvalidVersion;
use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
Expand Down Expand Up @@ -98,7 +98,7 @@ impl fmt::Display for Version {
}

impl FromStr for Version {
type Err = ArchiveError;
type Err = Error;

fn from_str(version: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = version.split('.').collect();
Expand Down
4 changes: 2 additions & 2 deletions postgresql_embedded/src/command/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl CommandExecutor for std::process::Command {
if output.status.success() {
Ok((stdout, stderr))
} else {
Err(crate::EmbeddedError::CommandError { stdout, stderr })
Err(crate::Error::CommandError { stdout, stderr })
}
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ impl CommandExecutor for tokio::process::Command {
if output.status.success() {
Ok((stdout, stderr))
} else {
Err(crate::EmbeddedError::CommandError { stdout, stderr })
Err(crate::Error::CommandError { stdout, stderr })
}
}
}
Expand Down
44 changes: 21 additions & 23 deletions postgresql_embedded/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use postgresql_archive::ArchiveError;
use std::string::FromUtf8Error;
use thiserror::Error;

/// PostgreSQL embedded result type
pub type Result<T, E = EmbeddedError> = core::result::Result<T, E>;
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// Errors that can occur when using PostgreSQL embedded
#[derive(Debug, Error)]
pub enum EmbeddedError {
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Error when PostgreSQL archive operations fail
#[error(transparent)]
ArchiveError(anyhow::Error),
Expand Down Expand Up @@ -46,32 +44,32 @@ pub enum EmbeddedError {
TimeoutError(anyhow::Error),
}

/// Convert PostgreSQL [archive errors](ArchiveError) to an [embedded errors](EmbeddedError::ArchiveError)
impl From<ArchiveError> for EmbeddedError {
fn from(error: ArchiveError) -> Self {
EmbeddedError::ArchiveError(error.into())
/// Convert PostgreSQL [archive errors](postgresql_archive::Error) to an [embedded errors](Error::ArchiveError)
impl From<postgresql_archive::Error> for Error {
fn from(error: postgresql_archive::Error) -> Self {
Error::ArchiveError(error.into())
}
}

/// Convert [standard IO errors](std::io::Error) to a [embedded errors](EmbeddedError::IoError)
impl From<std::io::Error> for EmbeddedError {
/// Convert [standard IO errors](std::io::Error) to a [embedded errors](Error::IoError)
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
EmbeddedError::IoError(error.into())
Error::IoError(error.into())
}
}

/// Convert [utf8 errors](FromUtf8Error) to [embedded errors](EmbeddedError::IoError)
impl From<FromUtf8Error> for EmbeddedError {
/// Convert [utf8 errors](FromUtf8Error) to [embedded errors](Error::IoError)
impl From<FromUtf8Error> for Error {
fn from(error: FromUtf8Error) -> Self {
EmbeddedError::IoError(error.into())
Error::IoError(error.into())
}
}

#[cfg(feature = "tokio")]
/// Convert [elapsed time errors](tokio::time::error::Elapsed) to [embedded errors](EmbeddedError::TimeoutError)
impl From<tokio::time::error::Elapsed> for EmbeddedError {
/// Convert [elapsed time errors](tokio::time::error::Elapsed) to [embedded errors](Error::TimeoutError)
impl From<tokio::time::error::Elapsed> for Error {
fn from(error: tokio::time::error::Elapsed) -> Self {
EmbeddedError::TimeoutError(error.into())
Error::TimeoutError(error.into())
}
}

Expand All @@ -83,15 +81,15 @@ mod test {

#[test]
fn test_from_archive_error() {
let archive_error = ArchiveError::ReleaseNotFound("test".to_string());
let error = EmbeddedError::from(archive_error);
let archive_error = postgresql_archive::Error::ReleaseNotFound("test".to_string());
let error = Error::from(archive_error);
assert_eq!(error.to_string(), "release not found for version [test]");
}

#[test]
fn test_from_io_error() {
let io_error = std::io::Error::new(std::io::ErrorKind::Other, "test");
let error = EmbeddedError::from(io_error);
let error = Error::from(io_error);
assert_eq!(error.to_string(), "test");
}

Expand All @@ -101,7 +99,7 @@ mod test {
let result = String::from_utf8(invalid_utf8);
assert!(result.is_err());
if let Err(error) = result {
let error = EmbeddedError::from(error);
let error = Error::from(error);
assert_eq!(
error.to_string(),
"invalid utf-8 sequence of 1 bytes from index 1"
Expand All @@ -118,7 +116,7 @@ mod test {
.await;
assert!(result.is_err());
if let Err(elapsed_error) = result {
let error = EmbeddedError::from(elapsed_error);
let error = Error::from(elapsed_error);
assert_eq!(error.to_string(), "deadline has elapsed");
}
}
Expand Down
2 changes: 1 addition & 1 deletion postgresql_embedded/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ mod error;
mod postgresql;
mod settings;

pub use error::{EmbeddedError, Result};
pub use error::{Error, Result};
pub use postgresql::{PostgreSQL, Status};
pub use settings::Settings;
4 changes: 2 additions & 2 deletions postgresql_embedded/src/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::command::pg_ctl::Mode::{Start, Stop};
use crate::command::pg_ctl::PgCtlBuilder;
use crate::command::pg_ctl::ShutdownMode::Fast;
use crate::command::traits::{CommandBuilder, CommandExecutor};
use crate::error::EmbeddedError::{
use crate::error::Error::{
ArchiveHashMismatch, ArchiveNotFound, DatabaseInitializationError, DatabaseStartError,
DatabaseStopError,
};
Expand All @@ -23,7 +23,7 @@ use std::str::FromStr;
use tracing::{debug, info};

use crate::command::psql::PsqlBuilder;
use crate::EmbeddedError::{CreateDatabaseError, DatabaseExistsError, DropDatabaseError};
use crate::Error::{CreateDatabaseError, DatabaseExistsError, DropDatabaseError};

#[cfg(feature = "bundled")]
lazy_static::lazy_static! {
Expand Down