Skip to content

Commit 46b557c

Browse files
authored
Use let chains to simplify nested conditional patterns (#11467)
see https://blog.rust-lang.org/2025/06/26/Rust-1.88.0/#let-chains
1 parent c971c5e commit 46b557c

File tree

3 files changed

+67
-68
lines changed

3 files changed

+67
-68
lines changed

src/controllers/krate/publish.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,12 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
283283
return Err(bad_request(&message));
284284
}
285285

286-
if let Some(description) = &description {
287-
if description.len() > MAX_DESCRIPTION_LENGTH {
288-
return Err(bad_request(format!(
289-
"The `description` is too long. A maximum of {MAX_DESCRIPTION_LENGTH} characters are currently allowed."
290-
)));
291-
}
286+
if let Some(description) = &description
287+
&& description.len() > MAX_DESCRIPTION_LENGTH
288+
{
289+
return Err(bad_request(format!(
290+
"The `description` is too long. A maximum of {MAX_DESCRIPTION_LENGTH} characters are currently allowed."
291+
)));
292292
}
293293

294294
if let Some(ref license) = license {

src/controllers/krate/search.rs

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -118,43 +118,42 @@ pub async fn list_crates(
118118
.left_join(versions::table.on(default_versions::version_id.eq(versions::id)))
119119
.select(selection);
120120

121-
if let Some(q_string) = &filter_params.q_string {
122-
if !q_string.is_empty() {
123-
let q_string = q_string.as_str();
124-
125-
let sort = sort.unwrap_or("relevance");
126-
127-
query = query.order(Crate::with_name(q_string).desc());
128-
129-
if sort == "relevance" {
130-
let q =
131-
plainto_tsquery_with_search_config(TsConfigurationByName("english"), q_string);
132-
let rank = ts_rank_cd(crates::textsearchable_index_col, q);
133-
query = query.select((
134-
ALL_COLUMNS,
135-
Crate::with_name(q_string),
136-
crate_downloads::downloads,
137-
recent_crate_downloads::downloads.nullable(),
138-
rank,
139-
versions::num.nullable(),
140-
versions::yanked.nullable(),
141-
default_versions::num_versions.nullable(),
142-
));
143-
seek = Some(Seek::Relevance);
144-
query = query.then_order_by(rank.desc())
145-
} else {
146-
query = query.select((
147-
ALL_COLUMNS,
148-
Crate::with_name(q_string),
149-
crate_downloads::downloads,
150-
recent_crate_downloads::downloads.nullable(),
151-
0_f32.into_sql::<Float>(),
152-
versions::num.nullable(),
153-
versions::yanked.nullable(),
154-
default_versions::num_versions.nullable(),
155-
));
156-
seek = Some(Seek::Query);
157-
}
121+
if let Some(q_string) = &filter_params.q_string
122+
&& !q_string.is_empty()
123+
{
124+
let q_string = q_string.as_str();
125+
126+
let sort = sort.unwrap_or("relevance");
127+
128+
query = query.order(Crate::with_name(q_string).desc());
129+
130+
if sort == "relevance" {
131+
let q = plainto_tsquery_with_search_config(TsConfigurationByName("english"), q_string);
132+
let rank = ts_rank_cd(crates::textsearchable_index_col, q);
133+
query = query.select((
134+
ALL_COLUMNS,
135+
Crate::with_name(q_string),
136+
crate_downloads::downloads,
137+
recent_crate_downloads::downloads.nullable(),
138+
rank,
139+
versions::num.nullable(),
140+
versions::yanked.nullable(),
141+
default_versions::num_versions.nullable(),
142+
));
143+
seek = Some(Seek::Relevance);
144+
query = query.then_order_by(rank.desc())
145+
} else {
146+
query = query.select((
147+
ALL_COLUMNS,
148+
Crate::with_name(q_string),
149+
crate_downloads::downloads,
150+
recent_crate_downloads::downloads.nullable(),
151+
0_f32.into_sql::<Float>(),
152+
versions::num.nullable(),
153+
versions::yanked.nullable(),
154+
default_versions::num_versions.nullable(),
155+
));
156+
seek = Some(Seek::Query);
158157
}
159158
}
160159

@@ -384,17 +383,17 @@ impl FilterParams {
384383
fn make_query(&self) -> crates::BoxedQuery<'_, diesel::pg::Pg> {
385384
let mut query = crates::table.into_boxed();
386385

387-
if let Some(q_string) = &self.q_string {
388-
if !q_string.is_empty() {
389-
let q = plainto_tsquery_with_search_config(
390-
TsConfigurationByName("english"),
391-
q_string.as_str(),
392-
);
393-
query = query.filter(
394-
q.matches(crates::textsearchable_index_col)
395-
.or(Crate::loosly_matches_name(q_string.as_str())),
396-
);
397-
}
386+
if let Some(q_string) = &self.q_string
387+
&& !q_string.is_empty()
388+
{
389+
let q = plainto_tsquery_with_search_config(
390+
TsConfigurationByName("english"),
391+
q_string.as_str(),
392+
);
393+
query = query.filter(
394+
q.matches(crates::textsearchable_index_col)
395+
.or(Crate::loosly_matches_name(q_string.as_str())),
396+
);
398397
}
399398

400399
if let Some(cat) = &self.category {

src/typosquat/checks.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ impl Check for Affixes {
3737
// If the package being examined starts with this prefix and separator combo, then
3838
// we should see if it exists without that prefix in the popular crate corpus.
3939
let combo = format!("{affix}{separator}");
40-
if let Some(stem) = name.strip_prefix(&combo) {
41-
if corpus.possible_squat(stem, name, package)? {
42-
squats.push(Squat::Custom {
43-
message: format!("adds the {combo} prefix"),
44-
package: stem.to_string(),
45-
})
46-
}
40+
if let Some(stem) = name.strip_prefix(&combo)
41+
&& corpus.possible_squat(stem, name, package)?
42+
{
43+
squats.push(Squat::Custom {
44+
message: format!("adds the {combo} prefix"),
45+
package: stem.to_string(),
46+
})
4747
}
4848

4949
// Alternatively, let's see if adding the prefix and separator combo to the package
@@ -59,13 +59,13 @@ impl Check for Affixes {
5959
// If the package being examined ends in this separator and suffix combo, then we
6060
// should see if it exists without that suffix in the popular crate corpus.
6161
let combo = format!("{separator}{affix}");
62-
if let Some(stem) = name.strip_suffix(&combo) {
63-
if corpus.possible_squat(stem, name, package)? {
64-
squats.push(Squat::Custom {
65-
message: format!("adds the {combo} suffix"),
66-
package: stem.to_string(),
67-
})
68-
}
62+
if let Some(stem) = name.strip_suffix(&combo)
63+
&& corpus.possible_squat(stem, name, package)?
64+
{
65+
squats.push(Squat::Custom {
66+
message: format!("adds the {combo} suffix"),
67+
package: stem.to_string(),
68+
})
6969
}
7070

7171
// Alternatively, let's see if adding the separator and suffix combo to the package

0 commit comments

Comments
 (0)