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:
parent
c1a5d763e2
commit
708d89d452
4 changed files with 22 additions and 11 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
use axum::extract::{Path, Query, State};
|
use axum::extract::{Path, Query, State};
|
||||||
use axum::http::{HeaderMap, StatusCode};
|
use axum::http::{HeaderMap, StatusCode};
|
||||||
|
|
@ -140,7 +142,7 @@ pub(crate) async fn list_gear(
|
||||||
let filter = match params.category {
|
let filter = match params.category {
|
||||||
Some(ref cat_str) => {
|
Some(ref cat_str) => {
|
||||||
let category = GearCategory::from_str(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)
|
GearFilter::for_category(category)
|
||||||
}
|
}
|
||||||
None => GearFilter::all(),
|
None => GearFilter::all(),
|
||||||
|
|
@ -206,7 +208,7 @@ pub(crate) struct NewGearSubmission {
|
||||||
impl NewGearSubmission {
|
impl NewGearSubmission {
|
||||||
fn into_new_gear(self) -> Result<NewGear, AppError> {
|
fn into_new_gear(self) -> Result<NewGear, AppError> {
|
||||||
let category = GearCategory::from_str(&self.category)
|
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() {
|
if self.make.trim().is_empty() {
|
||||||
return Err(AppError::validation("make cannot be empty"));
|
return Err(AppError::validation("make cannot be empty"));
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ pub struct AppState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn new(
|
pub fn new(
|
||||||
roaster_repo: Arc<dyn RoasterRepository>,
|
roaster_repo: Arc<dyn RoasterRepository>,
|
||||||
roast_repo: Arc<dyn RoastRepository>,
|
roast_repo: Arc<dyn RoastRepository>,
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
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 {
|
pub fn display_label(&self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
GearCategory::Grinder => "Grinder",
|
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)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct Gear {
|
pub struct Gear {
|
||||||
pub id: GearId,
|
pub id: GearId,
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use sqlx::{QueryBuilder, query_as};
|
use sqlx::{QueryBuilder, query_as};
|
||||||
|
|
@ -35,7 +37,7 @@ impl SqlGearRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_domain(record: GearRecord) -> Result<Gear, RepositoryError> {
|
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))
|
RepositoryError::unexpected(format!("invalid category: {}", record.category))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue