From 708d89d452d45455dc2dec912f56957e61e7a4c8 Mon Sep 17 00:00:00 2001 From: Jon Seager Date: Mon, 2 Feb 2026 16:12:13 +0000 Subject: [PATCH] fix: resolve clippy warnings for argument count and FromStr trait - Allow too_many_arguments for AppState::new since 8 repos are needed - Implement FromStr trait for GearCategory instead of custom from_str method to follow Rust conventions - Update callers to use map_err for Result handling --- src/application/routes/gear.rs | 6 ++++-- src/application/server.rs | 1 + src/domain/gear.rs | 22 ++++++++++++++-------- src/infrastructure/repositories/gear.rs | 4 +++- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/application/routes/gear.rs b/src/application/routes/gear.rs index cd1357b..fd415dd 100644 --- a/src/application/routes/gear.rs +++ b/src/application/routes/gear.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use axum::Json; use axum::extract::{Path, Query, State}; use axum::http::{HeaderMap, StatusCode}; @@ -140,7 +142,7 @@ pub(crate) async fn list_gear( let filter = match params.category { Some(ref cat_str) => { let category = GearCategory::from_str(cat_str) - .ok_or_else(|| AppError::validation("invalid category"))?; + .map_err(|_| AppError::validation("invalid category"))?; GearFilter::for_category(category) } None => GearFilter::all(), @@ -206,7 +208,7 @@ pub(crate) struct NewGearSubmission { impl NewGearSubmission { fn into_new_gear(self) -> Result { let category = GearCategory::from_str(&self.category) - .ok_or_else(|| AppError::validation("invalid category"))?; + .map_err(|_| AppError::validation("invalid category"))?; if self.make.trim().is_empty() { return Err(AppError::validation("make cannot be empty")); diff --git a/src/application/server.rs b/src/application/server.rs index bfdf2fa..22337d8 100644 --- a/src/application/server.rs +++ b/src/application/server.rs @@ -44,6 +44,7 @@ pub struct AppState { } impl AppState { + #[allow(clippy::too_many_arguments)] pub fn new( roaster_repo: Arc, roast_repo: Arc, diff --git a/src/domain/gear.rs b/src/domain/gear.rs index f0a6c44..8bc8a5a 100644 --- a/src/domain/gear.rs +++ b/src/domain/gear.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; @@ -19,14 +21,6 @@ impl GearCategory { } } - pub fn from_str(s: &str) -> Option { - match s.to_lowercase().as_str() { - "grinder" => Some(GearCategory::Grinder), - "brewer" => Some(GearCategory::Brewer), - _ => None, - } - } - pub fn display_label(&self) -> &'static str { match self { GearCategory::Grinder => "Grinder", @@ -35,6 +29,18 @@ impl GearCategory { } } +impl FromStr for GearCategory { + type Err = (); + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "grinder" => Ok(GearCategory::Grinder), + "brewer" => Ok(GearCategory::Brewer), + _ => Err(()), + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Gear { pub id: GearId, diff --git a/src/infrastructure/repositories/gear.rs b/src/infrastructure/repositories/gear.rs index 6739080..8fd7c70 100644 --- a/src/infrastructure/repositories/gear.rs +++ b/src/infrastructure/repositories/gear.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use async_trait::async_trait; use chrono::{DateTime, Utc}; use sqlx::{QueryBuilder, query_as}; @@ -35,7 +37,7 @@ impl SqlGearRepository { } fn to_domain(record: GearRecord) -> Result { - let category = GearCategory::from_str(&record.category).ok_or_else(|| { + let category = GearCategory::from_str(&record.category).map_err(|_| { RepositoryError::unexpected(format!("invalid category: {}", record.category)) })?;