Skip to content

Commit

Permalink
refactor: fix clippy lints and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sciencefidelity committed Jun 20, 2024
1 parent eeac0fd commit 81c3ccd
Show file tree
Hide file tree
Showing 14 changed files with 1,409 additions and 39 deletions.
13 changes: 12 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ repository = "https://github.com/sciencefidelity/eroteme"
license = "MIT or Apache-2.0"
exclude = ["./scripts"]

[lints.rust]
unsafe_code = "forbid"

[lints.clippy]
enum_glob_use = "deny"
pedantic = { level = "deny", priority = 1 }
Expand All @@ -30,6 +27,7 @@ chrono = "0.4"
clap = { version = "4.5.7", features = ["derive"] }
dotenv = "0.15.0"
handle-errors = { path = "handle-errors" }
mock-server = { path ="mock-server" }
openssl = { version = "0.10.32", features = ["vendored"] }
paseto = "2.0"
rand = "0.8.5"
Expand Down
8 changes: 4 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use platforms::*;
use platforms::{target::Env, TARGET_ARCH, TARGET_ENV, TARGET_OS};
use std::{borrow::Cow, process::Command};

/// Generate the `cargo:` key output
pub fn generate_cargo_keys() {
let output = Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.args(["rev-parse", "--short", "HEAD"])
.output();

let commit = match output {
Expand All @@ -17,7 +17,7 @@ pub fn generate_cargo_keys() {
Cow::from("unknown")
}
Err(err) => {
println!("cargo:warning=Failed to execute git command: {}", err);
println!("cargo:warning=Failed to execute git command: {err}");
Cow::from("unknown")
}
};
Expand All @@ -33,7 +33,7 @@ fn get_platform() -> String {
TARGET_ARCH.as_str(),
TARGET_OS.as_str(),
env_dash,
TARGET_ENV.map(|x| x.as_str()).unwrap_or("")
TARGET_ENV.map_or("", Env::as_str),
)
}

Expand Down
2 changes: 1 addition & 1 deletion handle-errors/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion handle-errors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
[package]
name = "handle-errors"
version = "0.1.0"
version = "1.0.0"
authors = ["Matt Cook <hello@mattcook.dev>"]
edition = "2021"
repository = "https://github.com/sciencefidelity/eroteme/handle-errors"
license = "MIT or Apache-2.0"

[lints.clippy]
enum_glob_use = "deny"
pedantic = { level = "deny", priority = 1 }
nursery = { level = "deny", priority = 2 }
unwrap_used = "deny"

[profile.release]
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"
strip = "symbols"

[dependencies]
reqwest = "0.12"
Expand Down
33 changes: 20 additions & 13 deletions handle-errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ impl std::fmt::Display for APILayerError {
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::ParseError(ref err) => write!(f, "cannot parse parameter: {err}"),
Error::MissingParameters => write!(f, "missing parameter"),
Error::WrongPassword => write!(f, "wrong password"),
Error::CannotDecryptToken => write!(f, "cannot decrypt token"),
Error::Unauthorized => write!(f, "no permission to change the underlying resource"),
Error::ArgonLibraryError(_) => write!(f, "cannot verify password"),
Error::DatabaseQueryError(_) => write!(f, "cannot update, invalid data"),
Error::MigrationError(_) => write!(f, "cannot migrate database"),
Error::ReqwestAPIError(err) => write!(f, "cannot execute: {err}"),
Error::MiddlewareReqwestError(err) => write!(f, "cannot execute: {err}"),
Error::ClientError(err) => write!(f, "external client error: {err}"),
Error::ServerError(err) => write!(f, "external server error: {err}"),
Self::ParseError(ref err) => write!(f, "cannot parse parameter: {err}"),
Self::MissingParameters => write!(f, "missing parameter"),
Self::WrongPassword => write!(f, "wrong password"),
Self::CannotDecryptToken => write!(f, "cannot decrypt token"),
Self::Unauthorized => write!(f, "no permission to change the underlying resource"),
Self::ArgonLibraryError(_) => write!(f, "cannot verify password"),
Self::DatabaseQueryError(_) => write!(f, "cannot update, invalid data"),
Self::MigrationError(_) => write!(f, "cannot migrate database"),
Self::ReqwestAPIError(err) => write!(f, "cannot execute: {err}"),
Self::MiddlewareReqwestError(err) => write!(f, "cannot execute: {err}"),
Self::ClientError(err) => write!(f, "external client error: {err}"),
Self::ServerError(err) => write!(f, "external server error: {err}"),
}
}
}
Expand All @@ -59,11 +59,18 @@ const DUPLICATE_KEY: u32 = 23505;

#[instrument]
pub async fn return_error(r: Rejection) -> Result<impl Reply, Rejection> {
#[allow(clippy::equatable_if_let)]
if let Some(crate::Error::DatabaseQueryError(e)) = r.find() {
event!(Level::ERROR, "database query error");
match e {
sqlx::Error::Database(err) => {
if err.code().unwrap().parse::<u32>().unwrap() == DUPLICATE_KEY {
if err
.code()
.expect("error code not found")
.parse::<u32>()
.expect("failed to parse error code")
== DUPLICATE_KEY
{
Ok(warp::reply::with_status(
"Account already exsists".to_string(),
StatusCode::UNPROCESSABLE_ENTITY,
Expand Down
Loading

0 comments on commit 81c3ccd

Please sign in to comment.