Skip to content

Commit 28ac9cb

Browse files
Merge pull request #8 from theseus-rs/update-error-names
refactor!: rename ArchiveError and EmbeddedError
2 parents 9a4535a + 73e7095 commit 28ac9cb

File tree

9 files changed

+64
-68
lines changed

9 files changed

+64
-68
lines changed

postgresql_archive/src/archive.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Manage PostgreSQL archive
22
#![allow(dead_code)]
3-
use crate::error::ArchiveError::{AssetHashNotFound, AssetNotFound, ReleaseNotFound, Unexpected};
3+
use crate::error::Error::{AssetHashNotFound, AssetNotFound, ReleaseNotFound, Unexpected};
44
use crate::error::Result;
55
use crate::github::{Asset, Release};
66
use crate::version::Version;
@@ -145,7 +145,7 @@ pub async fn get_version(version: &Version) -> Result<Version> {
145145
/// Gets the assets for a given [version](Version) of PostgreSQL and
146146
/// [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html).
147147
/// If the [version](Version) or [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html)
148-
/// is not found, then an [error](crate::error::ArchiveError) is returned.
148+
/// is not found, then an [error](crate::error::Error) is returned.
149149
///
150150
/// Two assets are returned. The first [asset](Asset) is the archive, and the second [asset](Asset) is the archive hash.
151151
async fn get_asset<S: AsRef<str>>(version: &Version, target: S) -> Result<(Version, Asset, Asset)> {
@@ -177,7 +177,7 @@ async fn get_asset<S: AsRef<str>>(version: &Version, target: S) -> Result<(Versi
177177

178178
/// Gets the archive for a given [version](Version) of PostgreSQL for the current target.
179179
/// If the [version](Version) is not found for this target, then an
180-
/// [error](crate::error::ArchiveError) is returned.
180+
/// [error](crate::error::Error) is returned.
181181
///
182182
/// Returns the archive bytes and the archive hash.
183183
pub async fn get_archive(version: &Version) -> Result<(Version, Bytes, String)> {
@@ -187,7 +187,7 @@ pub async fn get_archive(version: &Version) -> Result<(Version, Bytes, String)>
187187
/// Gets the archive for a given [version](Version) of PostgreSQL and
188188
/// [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html).
189189
/// If the [version](Version) or [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html)
190-
/// is not found, then an [error](crate::error::ArchiveError) is returned.
190+
/// is not found, then an [error](crate::error::Error) is returned.
191191
///
192192
/// Returns the archive bytes and the archive hash.
193193
pub async fn get_archive_for_target<S: AsRef<str>>(

postgresql_archive/src/blocking/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ lazy_static! {
99

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

1919
/// Gets the archive for a given [version](Version) of PostgreSQL for the current target.
2020
/// If the [version](Version) is not found for this target, then an
21-
/// [error](crate::ArchiveError) is returned.
21+
/// [error](crate::Error) is returned.
2222
///
2323
/// Returns the archive bytes and the archive hash.
2424
pub fn get_archive(version: &Version) -> crate::Result<(Version, Bytes, String)> {
@@ -30,7 +30,7 @@ pub fn get_archive(version: &Version) -> crate::Result<(Version, Bytes, String)>
3030
/// Gets the archive for a given [version](Version) of PostgreSQL and
3131
/// [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html).
3232
/// If the [version](Version) or [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html)
33-
/// is not found, then an [error](crate::error::ArchiveError) is returned.
33+
/// is not found, then an [error](crate::error::Error) is returned.
3434
///
3535
/// Returns the archive bytes and the archive hash.
3636
pub fn get_archive_for_target<S: AsRef<str>>(

postgresql_archive/src/error.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use thiserror::Error;
2-
31
/// PostgreSQL archive result type
4-
pub type Result<T, E = ArchiveError> = core::result::Result<T, E>;
2+
pub type Result<T, E = Error> = core::result::Result<T, E>;
53

64
/// PostgreSQL archive errors
7-
#[derive(Debug, Error)]
8-
pub enum ArchiveError {
5+
#[derive(Debug, thiserror::Error)]
6+
pub enum Error {
97
/// Asset not found
108
#[error("asset [{0}] not found")]
119
AssetNotFound(String),
@@ -29,45 +27,45 @@ pub enum ArchiveError {
2927
Unexpected(String),
3028
}
3129

32-
/// Converts a [`regex::Error`] into an [`ParseError`](ArchiveError::ParseError)
33-
impl From<regex::Error> for ArchiveError {
30+
/// Converts a [`regex::Error`] into an [`ParseError`](Error::ParseError)
31+
impl From<regex::Error> for Error {
3432
fn from(error: regex::Error) -> Self {
35-
ArchiveError::ParseError(error.into())
33+
Error::ParseError(error.into())
3634
}
3735
}
3836

39-
/// Converts a [`reqwest::Error`] into an [`IoError`](ArchiveError::IoError)
40-
impl From<reqwest::Error> for ArchiveError {
37+
/// Converts a [`reqwest::Error`] into an [`IoError`](Error::IoError)
38+
impl From<reqwest::Error> for Error {
4139
fn from(error: reqwest::Error) -> Self {
42-
ArchiveError::IoError(error.into())
40+
Error::IoError(error.into())
4341
}
4442
}
4543

46-
/// Converts a [`std::io::Error`] into an [`IoError`](ArchiveError::IoError)
47-
impl From<std::io::Error> for ArchiveError {
44+
/// Converts a [`std::io::Error`] into an [`IoError`](Error::IoError)
45+
impl From<std::io::Error> for Error {
4846
fn from(error: std::io::Error) -> Self {
49-
ArchiveError::IoError(error.into())
47+
Error::IoError(error.into())
5048
}
5149
}
5250

53-
/// Converts a [`std::num::ParseIntError`] into an [`ParseError`](ArchiveError::ParseError)
54-
impl From<std::num::ParseIntError> for ArchiveError {
51+
/// Converts a [`std::num::ParseIntError`] into an [`ParseError`](Error::ParseError)
52+
impl From<std::num::ParseIntError> for Error {
5553
fn from(error: std::num::ParseIntError) -> Self {
56-
ArchiveError::ParseError(error.into())
54+
Error::ParseError(error.into())
5755
}
5856
}
5957

60-
/// Converts a [`std::path::StripPrefixError`] into an [`ParseError`](ArchiveError::ParseError)
61-
impl From<std::path::StripPrefixError> for ArchiveError {
58+
/// Converts a [`std::path::StripPrefixError`] into an [`ParseError`](Error::ParseError)
59+
impl From<std::path::StripPrefixError> for Error {
6260
fn from(error: std::path::StripPrefixError) -> Self {
63-
ArchiveError::ParseError(error.into())
61+
Error::ParseError(error.into())
6462
}
6563
}
6664

67-
/// Converts a [`anyhow::Error`] into an [`Unexpected`](ArchiveError::Unexpected)
68-
impl From<anyhow::Error> for ArchiveError {
65+
/// Converts a [`anyhow::Error`] into an [`Unexpected`](Error::Unexpected)
66+
impl From<anyhow::Error> for Error {
6967
fn from(error: anyhow::Error) -> Self {
70-
ArchiveError::Unexpected(error.to_string())
68+
Error::Unexpected(error.to_string())
7169
}
7270
}
7371

@@ -82,7 +80,7 @@ mod test {
8280
#[test]
8381
fn test_from_regex_error() {
8482
let regex_error = regex::Error::Syntax("test".to_string());
85-
let error = ArchiveError::from(regex_error);
83+
let error = Error::from(regex_error);
8684
assert_eq!(error.to_string(), "test");
8785
}
8886

@@ -91,15 +89,15 @@ mod test {
9189
let result = reqwest::get("https://a.com").await;
9290
assert!(result.is_err());
9391
if let Err(error) = result {
94-
let error = ArchiveError::from(error);
92+
let error = Error::from(error);
9593
assert!(error.to_string().contains("https://a.com"));
9694
}
9795
}
9896

9997
#[test]
10098
fn test_from_io_error() {
10199
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "test");
102-
let error = ArchiveError::from(io_error);
100+
let error = Error::from(io_error);
103101
assert_eq!(error.to_string(), "test");
104102
}
105103

@@ -108,7 +106,7 @@ mod test {
108106
let result = u64::from_str("test");
109107
assert!(result.is_err());
110108
if let Err(error) = result {
111-
let error = ArchiveError::from(error);
109+
let error = Error::from(error);
112110
assert_eq!(error.to_string(), "invalid digit found in string");
113111
}
114112
}
@@ -119,15 +117,15 @@ mod test {
119117
let result = path.strip_prefix("foo");
120118
assert!(result.is_err());
121119
if let Err(error) = result {
122-
let error = ArchiveError::from(error);
120+
let error = Error::from(error);
123121
assert_eq!(error.to_string(), "prefix not found");
124122
}
125123
}
126124

127125
#[test]
128126
fn test_from_anyhow_error() {
129127
let anyhow_error = anyhow::Error::msg("test");
130-
let error = ArchiveError::from(anyhow_error);
128+
let error = Error::from(anyhow_error);
131129
assert_eq!(error.to_string(), "test");
132130
}
133131
}

postgresql_archive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,6 @@ mod github;
114114
mod version;
115115

116116
pub use archive::{extract, get_archive, get_archive_for_target, get_version};
117-
pub use error::{ArchiveError, Result};
117+
pub use error::{Error, Result};
118118
#[allow(deprecated)]
119119
pub use version::{Version, LATEST, V12, V13, V14, V15, V16};

postgresql_archive/src/version.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! PostgreSQL version
22
#![allow(dead_code)]
33

4-
use crate::error::ArchiveError::InvalidVersion;
5-
use crate::error::{ArchiveError, Result};
4+
use crate::error::Error::InvalidVersion;
5+
use crate::error::{Error, Result};
66
use serde::{Deserialize, Serialize};
77
use std::fmt;
88
use std::str::FromStr;
@@ -98,7 +98,7 @@ impl fmt::Display for Version {
9898
}
9999

100100
impl FromStr for Version {
101-
type Err = ArchiveError;
101+
type Err = Error;
102102

103103
fn from_str(version: &str) -> Result<Self, Self::Err> {
104104
let parts: Vec<&str> = version.split('.').collect();

postgresql_embedded/src/command/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl CommandExecutor for std::process::Command {
103103
if output.status.success() {
104104
Ok((stdout, stderr))
105105
} else {
106-
Err(crate::EmbeddedError::CommandError { stdout, stderr })
106+
Err(crate::Error::CommandError { stdout, stderr })
107107
}
108108
}
109109
}
@@ -137,7 +137,7 @@ impl CommandExecutor for tokio::process::Command {
137137
if output.status.success() {
138138
Ok((stdout, stderr))
139139
} else {
140-
Err(crate::EmbeddedError::CommandError { stdout, stderr })
140+
Err(crate::Error::CommandError { stdout, stderr })
141141
}
142142
}
143143
}

postgresql_embedded/src/error.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
use postgresql_archive::ArchiveError;
21
use std::string::FromUtf8Error;
3-
use thiserror::Error;
42

53
/// PostgreSQL embedded result type
6-
pub type Result<T, E = EmbeddedError> = core::result::Result<T, E>;
4+
pub type Result<T, E = Error> = core::result::Result<T, E>;
75

86
/// Errors that can occur when using PostgreSQL embedded
9-
#[derive(Debug, Error)]
10-
pub enum EmbeddedError {
7+
#[derive(Debug, thiserror::Error)]
8+
pub enum Error {
119
/// Error when PostgreSQL archive operations fail
1210
#[error(transparent)]
1311
ArchiveError(anyhow::Error),
@@ -46,32 +44,32 @@ pub enum EmbeddedError {
4644
TimeoutError(anyhow::Error),
4745
}
4846

49-
/// Convert PostgreSQL [archive errors](ArchiveError) to an [embedded errors](EmbeddedError::ArchiveError)
50-
impl From<ArchiveError> for EmbeddedError {
51-
fn from(error: ArchiveError) -> Self {
52-
EmbeddedError::ArchiveError(error.into())
47+
/// Convert PostgreSQL [archive errors](postgresql_archive::Error) to an [embedded errors](Error::ArchiveError)
48+
impl From<postgresql_archive::Error> for Error {
49+
fn from(error: postgresql_archive::Error) -> Self {
50+
Error::ArchiveError(error.into())
5351
}
5452
}
5553

56-
/// Convert [standard IO errors](std::io::Error) to a [embedded errors](EmbeddedError::IoError)
57-
impl From<std::io::Error> for EmbeddedError {
54+
/// Convert [standard IO errors](std::io::Error) to a [embedded errors](Error::IoError)
55+
impl From<std::io::Error> for Error {
5856
fn from(error: std::io::Error) -> Self {
59-
EmbeddedError::IoError(error.into())
57+
Error::IoError(error.into())
6058
}
6159
}
6260

63-
/// Convert [utf8 errors](FromUtf8Error) to [embedded errors](EmbeddedError::IoError)
64-
impl From<FromUtf8Error> for EmbeddedError {
61+
/// Convert [utf8 errors](FromUtf8Error) to [embedded errors](Error::IoError)
62+
impl From<FromUtf8Error> for Error {
6563
fn from(error: FromUtf8Error) -> Self {
66-
EmbeddedError::IoError(error.into())
64+
Error::IoError(error.into())
6765
}
6866
}
6967

7068
#[cfg(feature = "tokio")]
71-
/// Convert [elapsed time errors](tokio::time::error::Elapsed) to [embedded errors](EmbeddedError::TimeoutError)
72-
impl From<tokio::time::error::Elapsed> for EmbeddedError {
69+
/// Convert [elapsed time errors](tokio::time::error::Elapsed) to [embedded errors](Error::TimeoutError)
70+
impl From<tokio::time::error::Elapsed> for Error {
7371
fn from(error: tokio::time::error::Elapsed) -> Self {
74-
EmbeddedError::TimeoutError(error.into())
72+
Error::TimeoutError(error.into())
7573
}
7674
}
7775

@@ -83,15 +81,15 @@ mod test {
8381

8482
#[test]
8583
fn test_from_archive_error() {
86-
let archive_error = ArchiveError::ReleaseNotFound("test".to_string());
87-
let error = EmbeddedError::from(archive_error);
84+
let archive_error = postgresql_archive::Error::ReleaseNotFound("test".to_string());
85+
let error = Error::from(archive_error);
8886
assert_eq!(error.to_string(), "release not found for version [test]");
8987
}
9088

9189
#[test]
9290
fn test_from_io_error() {
9391
let io_error = std::io::Error::new(std::io::ErrorKind::Other, "test");
94-
let error = EmbeddedError::from(io_error);
92+
let error = Error::from(io_error);
9593
assert_eq!(error.to_string(), "test");
9694
}
9795

@@ -101,7 +99,7 @@ mod test {
10199
let result = String::from_utf8(invalid_utf8);
102100
assert!(result.is_err());
103101
if let Err(error) = result {
104-
let error = EmbeddedError::from(error);
102+
let error = Error::from(error);
105103
assert_eq!(
106104
error.to_string(),
107105
"invalid utf-8 sequence of 1 bytes from index 1"
@@ -118,7 +116,7 @@ mod test {
118116
.await;
119117
assert!(result.is_err());
120118
if let Err(elapsed_error) = result {
121-
let error = EmbeddedError::from(elapsed_error);
119+
let error = Error::from(elapsed_error);
122120
assert_eq!(error.to_string(), "deadline has elapsed");
123121
}
124122
}

postgresql_embedded/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ mod error;
115115
mod postgresql;
116116
mod settings;
117117

118-
pub use error::{EmbeddedError, Result};
118+
pub use error::{Error, Result};
119119
pub use postgresql::{PostgreSQL, Status};
120120
pub use settings::Settings;

postgresql_embedded/src/postgresql.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::command::pg_ctl::Mode::{Start, Stop};
33
use crate::command::pg_ctl::PgCtlBuilder;
44
use crate::command::pg_ctl::ShutdownMode::Fast;
55
use crate::command::traits::{CommandBuilder, CommandExecutor};
6-
use crate::error::EmbeddedError::{
6+
use crate::error::Error::{
77
ArchiveHashMismatch, ArchiveNotFound, DatabaseInitializationError, DatabaseStartError,
88
DatabaseStopError,
99
};
@@ -23,7 +23,7 @@ use std::str::FromStr;
2323
use tracing::{debug, info};
2424

2525
use crate::command::psql::PsqlBuilder;
26-
use crate::EmbeddedError::{CreateDatabaseError, DatabaseExistsError, DropDatabaseError};
26+
use crate::Error::{CreateDatabaseError, DatabaseExistsError, DropDatabaseError};
2727

2828
#[cfg(feature = "bundled")]
2929
lazy_static::lazy_static! {

0 commit comments

Comments
 (0)