diff --git a/src/controllers/krate/publish.rs b/src/controllers/krate/publish.rs index e29e4e4c99d..e34c0326c60 100644 --- a/src/controllers/krate/publish.rs +++ b/src/controllers/krate/publish.rs @@ -283,12 +283,12 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult MAX_DESCRIPTION_LENGTH { - return Err(bad_request(format!( - "The `description` is too long. A maximum of {MAX_DESCRIPTION_LENGTH} characters are currently allowed." - ))); - } + if let Some(description) = &description + && description.len() > MAX_DESCRIPTION_LENGTH + { + return Err(bad_request(format!( + "The `description` is too long. A maximum of {MAX_DESCRIPTION_LENGTH} characters are currently allowed." + ))); } if let Some(ref license) = license { diff --git a/src/controllers/krate/search.rs b/src/controllers/krate/search.rs index cec74691430..107bb0e990e 100644 --- a/src/controllers/krate/search.rs +++ b/src/controllers/krate/search.rs @@ -118,43 +118,42 @@ pub async fn list_crates( .left_join(versions::table.on(default_versions::version_id.eq(versions::id))) .select(selection); - if let Some(q_string) = &filter_params.q_string { - if !q_string.is_empty() { - let q_string = q_string.as_str(); - - let sort = sort.unwrap_or("relevance"); - - query = query.order(Crate::with_name(q_string).desc()); - - if sort == "relevance" { - let q = - plainto_tsquery_with_search_config(TsConfigurationByName("english"), q_string); - let rank = ts_rank_cd(crates::textsearchable_index_col, q); - query = query.select(( - ALL_COLUMNS, - Crate::with_name(q_string), - crate_downloads::downloads, - recent_crate_downloads::downloads.nullable(), - rank, - versions::num.nullable(), - versions::yanked.nullable(), - default_versions::num_versions.nullable(), - )); - seek = Some(Seek::Relevance); - query = query.then_order_by(rank.desc()) - } else { - query = query.select(( - ALL_COLUMNS, - Crate::with_name(q_string), - crate_downloads::downloads, - recent_crate_downloads::downloads.nullable(), - 0_f32.into_sql::(), - versions::num.nullable(), - versions::yanked.nullable(), - default_versions::num_versions.nullable(), - )); - seek = Some(Seek::Query); - } + if let Some(q_string) = &filter_params.q_string + && !q_string.is_empty() + { + let q_string = q_string.as_str(); + + let sort = sort.unwrap_or("relevance"); + + query = query.order(Crate::with_name(q_string).desc()); + + if sort == "relevance" { + let q = plainto_tsquery_with_search_config(TsConfigurationByName("english"), q_string); + let rank = ts_rank_cd(crates::textsearchable_index_col, q); + query = query.select(( + ALL_COLUMNS, + Crate::with_name(q_string), + crate_downloads::downloads, + recent_crate_downloads::downloads.nullable(), + rank, + versions::num.nullable(), + versions::yanked.nullable(), + default_versions::num_versions.nullable(), + )); + seek = Some(Seek::Relevance); + query = query.then_order_by(rank.desc()) + } else { + query = query.select(( + ALL_COLUMNS, + Crate::with_name(q_string), + crate_downloads::downloads, + recent_crate_downloads::downloads.nullable(), + 0_f32.into_sql::(), + versions::num.nullable(), + versions::yanked.nullable(), + default_versions::num_versions.nullable(), + )); + seek = Some(Seek::Query); } } @@ -384,17 +383,17 @@ impl FilterParams { fn make_query(&self) -> crates::BoxedQuery<'_, diesel::pg::Pg> { let mut query = crates::table.into_boxed(); - if let Some(q_string) = &self.q_string { - if !q_string.is_empty() { - let q = plainto_tsquery_with_search_config( - TsConfigurationByName("english"), - q_string.as_str(), - ); - query = query.filter( - q.matches(crates::textsearchable_index_col) - .or(Crate::loosly_matches_name(q_string.as_str())), - ); - } + if let Some(q_string) = &self.q_string + && !q_string.is_empty() + { + let q = plainto_tsquery_with_search_config( + TsConfigurationByName("english"), + q_string.as_str(), + ); + query = query.filter( + q.matches(crates::textsearchable_index_col) + .or(Crate::loosly_matches_name(q_string.as_str())), + ); } if let Some(cat) = &self.category { diff --git a/src/typosquat/checks.rs b/src/typosquat/checks.rs index dea6ad1dfb2..95ad0e60350 100644 --- a/src/typosquat/checks.rs +++ b/src/typosquat/checks.rs @@ -37,13 +37,13 @@ impl Check for Affixes { // If the package being examined starts with this prefix and separator combo, then // we should see if it exists without that prefix in the popular crate corpus. let combo = format!("{affix}{separator}"); - if let Some(stem) = name.strip_prefix(&combo) { - if corpus.possible_squat(stem, name, package)? { - squats.push(Squat::Custom { - message: format!("adds the {combo} prefix"), - package: stem.to_string(), - }) - } + if let Some(stem) = name.strip_prefix(&combo) + && corpus.possible_squat(stem, name, package)? + { + squats.push(Squat::Custom { + message: format!("adds the {combo} prefix"), + package: stem.to_string(), + }) } // Alternatively, let's see if adding the prefix and separator combo to the package @@ -59,13 +59,13 @@ impl Check for Affixes { // If the package being examined ends in this separator and suffix combo, then we // should see if it exists without that suffix in the popular crate corpus. let combo = format!("{separator}{affix}"); - if let Some(stem) = name.strip_suffix(&combo) { - if corpus.possible_squat(stem, name, package)? { - squats.push(Squat::Custom { - message: format!("adds the {combo} suffix"), - package: stem.to_string(), - }) - } + if let Some(stem) = name.strip_suffix(&combo) + && corpus.possible_squat(stem, name, package)? + { + squats.push(Squat::Custom { + message: format!("adds the {combo} suffix"), + package: stem.to_string(), + }) } // Alternatively, let's see if adding the separator and suffix combo to the package