Skip to content

build: Enable pedantic lints #77

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
Jun 18, 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
35 changes: 17 additions & 18 deletions postgresql_archive/src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ lazy_static! {
struct GithubMiddleware;

impl GithubMiddleware {
fn add_github_headers(&self, request: &mut Request) -> Result<()> {
#[allow(clippy::unnecessary_wraps)]
fn add_github_headers(request: &mut Request) -> Result<()> {
let headers = request.headers_mut();

headers.append(
Expand Down Expand Up @@ -82,8 +83,8 @@ impl Middleware for GithubMiddleware {
extensions: &mut Extensions,
next: Next<'_>,
) -> reqwest_middleware::Result<Response> {
match self.add_github_headers(&mut request) {
Ok(_) => next.run(request, extensions).await,
match GithubMiddleware::add_github_headers(&mut request) {
Ok(()) => next.run(request, extensions).await,
Err(error) => Err(reqwest_middleware::Error::Middleware(error.into())),
}
}
Expand Down Expand Up @@ -131,12 +132,9 @@ async fn get_release(version: &Version) -> Result<Release> {
}

for release in response_releases {
let release_version = match Version::from_str(&release.tag_name) {
Ok(release_version) => release_version,
Err(_) => {
warn!("Failed to parse release version {}", release.tag_name);
continue;
}
let Ok(release_version) = Version::from_str(&release.tag_name) else {
warn!("Failed to parse release version {}", release.tag_name);
continue;
};

if version.matches(&release_version) {
Expand Down Expand Up @@ -205,8 +203,7 @@ async fn get_asset<S: AsRef<str>>(version: &Version, target: S) -> Result<(Versi

match (asset, asset_hash) {
(Some(asset), Some(asset_hash)) => Ok((asset_version, asset, asset_hash)),
(None, _) => Err(AssetNotFound(asset_name.to_string())),
(_, None) => Err(AssetNotFound(asset_name.to_string())),
(_, None) | (None, _) => Err(AssetNotFound(asset_name.to_string())),
}
}

Expand All @@ -226,6 +223,7 @@ pub async fn get_archive(version: &Version) -> Result<(Version, Bytes)> {
/// is not found, then an [error](crate::error::Error) is returned.
///
/// Returns the archive version and bytes.
#[allow(clippy::cast_precision_loss)]
#[instrument(level = "debug", skip(target))]
pub async fn get_archive_for_target<S: AsRef<str>>(
version: &Version,
Expand Down Expand Up @@ -320,6 +318,7 @@ fn acquire_lock(out_dir: &Path) -> Result<PathBuf> {
}

/// Extracts the compressed tar [bytes](Bytes) to the [out_dir](Path).
#[allow(clippy::cast_precision_loss)]
#[instrument(skip(bytes))]
pub async fn extract(bytes: &Bytes, out_dir: &Path) -> Result<()> {
let input = BufReader::new(Cursor::new(bytes));
Expand All @@ -328,13 +327,13 @@ pub async fn extract(bytes: &Bytes, out_dir: &Path) -> Result<()> {
let mut files = 0;
let mut extracted_bytes = 0;

let parent_dir = match out_dir.parent() {
Some(parent) => parent,
None => {
debug!("No parent directory for {}", out_dir.to_string_lossy());
out_dir
}
let parent_dir = if let Some(parent) = out_dir.parent() {
parent
} else {
debug!("No parent directory for {}", out_dir.to_string_lossy());
out_dir
};

create_dir_all(parent_dir)?;

let lock_file = acquire_lock(parent_dir)?;
Expand Down Expand Up @@ -370,7 +369,7 @@ pub async fn extract(bytes: &Bytes, out_dir: &Path) -> Result<()> {
}
};
let stripped_entry_header_path = entry_header_path.strip_prefix(prefix)?.to_path_buf();
let mut entry_name = extract_dir.to_path_buf();
let mut entry_name = extract_dir.clone();
entry_name.push(stripped_entry_header_path);

if entry_type.is_dir() || entry_name.is_dir() {
Expand Down
16 changes: 16 additions & 0 deletions postgresql_archive/src/blocking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ 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::Error::ReleaseNotFound) error is returned.
///
/// # Errors
///
/// Returns an error if the version is not found.
pub fn get_version(version: &Version) -> crate::Result<Version> {
RUNTIME
.handle()
Expand All @@ -21,6 +25,10 @@ pub fn get_version(version: &Version) -> crate::Result<Version> {
/// [error](crate::Error) is returned.
///
/// Returns the archive version and bytes.
///
/// # Errors
///
/// Returns an error if the version is not found.
pub fn get_archive(version: &Version) -> crate::Result<(Version, Bytes)> {
RUNTIME
.handle()
Expand All @@ -33,6 +41,10 @@ pub fn get_archive(version: &Version) -> crate::Result<(Version, Bytes)> {
/// is not found, then an [error](crate::error::Error) is returned.
///
/// Returns the archive version and bytes.
///
/// # Errors
///
/// Returns an error if the version or target is not found.
pub fn get_archive_for_target<S: AsRef<str>>(
version: &Version,
target: S,
Expand All @@ -43,6 +55,10 @@ pub fn get_archive_for_target<S: AsRef<str>>(
}

/// Extracts the compressed tar [bytes](Bytes) to the [out_dir](Path).
///
/// # Errors
///
/// Returns an error if the extraction fails.
pub fn extract(bytes: &Bytes, out_dir: &Path) -> crate::Result<()> {
RUNTIME
.handle()
Expand Down
3 changes: 3 additions & 0 deletions postgresql_archive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
//! Uses PostgreSQL binaries from [theseus-rs/postgresql-binaries](https://github.com/theseus-rs/postgresql-binaries).

#![forbid(unsafe_code)]
#![deny(clippy::pedantic)]
#![allow(clippy::doc_markdown)]

#[macro_use]
extern crate lazy_static;

Expand Down
8 changes: 4 additions & 4 deletions postgresql_archive/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub const V13: Version = Version::new(13, None, None);
pub const V12: Version = Version::new(12, None, None);

impl Version {
#[must_use]
pub const fn new(major: u64, minor: Option<u64>, release: Option<u64>) -> Self {
Self {
major,
Expand All @@ -67,6 +68,7 @@ impl Version {
/// 4. `15` does not match `16.1.0`
/// 5. `16.0` does not match `16.1.0`
/// 6. `16.1.0` does not match `16.1.1`
#[must_use]
pub fn matches(&self, version: &Version) -> bool {
if self.major != version.major {
return false;
Expand All @@ -87,12 +89,10 @@ impl fmt::Display for Version {
let major = self.major.to_string();
let minor = self
.minor
.map(|minor| format!(".{minor}"))
.unwrap_or("".to_string());
.map_or(String::new(), |minor| format!(".{minor}"));
let release = self
.release
.map(|release| format!(".{release}"))
.unwrap_or("".to_string());
.map_or(String::new(), |release| format!(".{release}"));
write!(formatter, "{major}{minor}{release}")
}
}
Expand Down
38 changes: 33 additions & 5 deletions postgresql_embedded/src/blocking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,47 @@ lazy_static! {
static ref RUNTIME: Runtime = Runtime::new().unwrap();
}

/// PostgreSQL server
/// `PostgreSQL` server
#[derive(Clone, Debug, Default)]
pub struct PostgreSQL {
inner: crate::postgresql::PostgreSQL,
}

/// PostgreSQL server methods
/// `PostgreSQL` server methods
impl PostgreSQL {
/// Create a new [`crate::postgresql::PostgreSQL`] instance
#[must_use]
pub fn new(version: Version, settings: Settings) -> Self {
Self {
inner: crate::postgresql::PostgreSQL::new(version, settings),
}
}

/// Get the [status](Status) of the PostgreSQL server
/// Get the [status](Status) of the `PostgreSQL` server
#[must_use]
pub fn status(&self) -> Status {
self.inner.status()
}

/// Get the [version](Version) of the PostgreSQL server
/// Get the [version](Version) of the `PostgreSQL` server
#[must_use]
pub fn version(&self) -> &Version {
self.inner.version()
}

/// Get the [settings](Settings) of the PostgreSQL server
/// Get the [settings](Settings) of the `PostgreSQL` server
#[must_use]
pub fn settings(&self) -> &Settings {
self.inner.settings()
}

/// Set up the database by extracting the archive and initializing the database.
/// If the installation directory already exists, the archive will not be extracted.
/// If the data directory already exists, the database will not be initialized.
///
/// # Errors
///
/// Returns an error if the setup fails.
pub fn setup(&mut self) -> Result<()> {
RUNTIME
.handle()
Expand All @@ -48,20 +56,32 @@ impl PostgreSQL {

/// Start the database and wait for the startup to complete.
/// If the port is set to `0`, the database will be started on a random port.
///
/// # Errors
///
/// Returns an error if the startup fails.
pub fn start(&mut self) -> Result<()> {
RUNTIME
.handle()
.block_on(async move { self.inner.start().await })
}

/// Stop the database gracefully (smart mode) and wait for the shutdown to complete.
///
/// # Errors
///
/// Returns an error if the shutdown fails.
pub fn stop(&self) -> Result<()> {
RUNTIME
.handle()
.block_on(async move { self.inner.stop().await })
}

/// Create a new database with the given name.
///
/// # Errors
///
/// Returns an error if the database creation fails.
pub fn create_database<S>(&self, database_name: S) -> Result<()>
where
S: AsRef<str> + std::fmt::Debug,
Expand All @@ -72,6 +92,10 @@ impl PostgreSQL {
}

/// Check if a database with the given name exists.
///
/// # Errors
///
/// Returns an error if the database existence check fails.
pub fn database_exists<S>(&self, database_name: S) -> Result<bool>
where
S: AsRef<str> + std::fmt::Debug,
Expand All @@ -82,6 +106,10 @@ impl PostgreSQL {
}

/// Drop a database with the given name.
///
/// # Errors
///
/// Returns an error if the database drop fails.
pub fn drop_database<S>(&self, database_name: S) -> Result<()>
where
S: AsRef<str> + std::fmt::Debug,
Expand Down
8 changes: 4 additions & 4 deletions postgresql_embedded/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::string::FromUtf8Error;

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

/// Errors that can occur when using PostgreSQL embedded
/// Errors that can occur when using `PostgreSQL` embedded
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Error when PostgreSQL archive operations fail
/// Error when `PostgreSQL` archive operations fail
#[error(transparent)]
ArchiveError(postgresql_archive::Error),
/// Error when a command fails
Expand Down Expand Up @@ -38,7 +38,7 @@ pub enum Error {
IoError(anyhow::Error),
}

/// Convert PostgreSQL [archive errors](postgresql_archive::Error) to an [embedded errors](Error::ArchiveError)
/// 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)
Expand Down
2 changes: 2 additions & 0 deletions postgresql_embedded/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@
//! Uses PostgreSQL binaries from [theseus-rs/postgresql-binaries](https://github.com/theseus-rs/postgresql-binaries).

#![forbid(unsafe_code)]
#![deny(clippy::pedantic)]
#![allow(dead_code)]
#![allow(clippy::doc_markdown)]

#[cfg(feature = "blocking")]
pub mod blocking;
Expand Down
Loading
Loading