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
This commit is contained in:
Jon Seager 2026-02-02 16:12:13 +00:00
parent c1a5d763e2
commit 708d89d452
No known key found for this signature in database
4 changed files with 22 additions and 11 deletions

View file

@ -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<NewGear, AppError> {
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"));

View file

@ -44,6 +44,7 @@ pub struct AppState {
}
impl AppState {
#[allow(clippy::too_many_arguments)]
pub fn new(
roaster_repo: Arc<dyn RoasterRepository>,
roast_repo: Arc<dyn RoastRepository>,

View file

@ -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<Self> {
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<Self, Self::Err> {
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,

View file

@ -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<Gear, RepositoryError> {
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))
})?;