Skip to content

Commit 0147a6e

Browse files
authored
controllers/krate/versions: Extract ListQueryParams::include() fn (#10283)
1 parent 899eeb1 commit 0147a6e

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/controllers/krate/versions.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ pub struct ListQueryParams {
4343
sort: Option<String>,
4444
}
4545

46+
impl ListQueryParams {
47+
fn include(&self) -> AppResult<ShowIncludeMode> {
48+
let include = self
49+
.include
50+
.as_ref()
51+
.map(|mode| ShowIncludeMode::from_str(mode))
52+
.transpose()?
53+
.unwrap_or_default();
54+
Ok(include)
55+
}
56+
}
57+
4658
/// List all versions of a crate.
4759
#[utoipa::path(
4860
get,
@@ -73,18 +85,12 @@ pub async fn list_versions(
7385
None => None,
7486
};
7587

76-
let include = params
77-
.include
78-
.map(|mode| ShowIncludeMode::from_str(&mode))
79-
.transpose()?
80-
.unwrap_or_default();
81-
8288
// Sort by semver by default
83-
let versions_and_publishers = match params.sort.map(|s| s.to_lowercase()).as_deref() {
89+
let versions_and_publishers = match &params.sort.as_ref().map(|s| s.to_lowercase()).as_deref() {
8490
Some("date") => {
85-
list_by_date(crate_id, pagination.as_ref(), include, &req, &mut conn).await?
91+
list_by_date(crate_id, pagination.as_ref(), &params, &req, &mut conn).await?
8692
}
87-
_ => list_by_semver(crate_id, pagination.as_ref(), include, &req, &mut conn).await?,
93+
_ => list_by_semver(crate_id, pagination.as_ref(), &params, &req, &mut conn).await?,
8894
};
8995

9096
let versions = versions_and_publishers
@@ -111,7 +117,7 @@ pub async fn list_versions(
111117
async fn list_by_date(
112118
crate_id: i32,
113119
options: Option<&PaginationOptions>,
114-
include: ShowIncludeMode,
120+
params: &ListQueryParams,
115121
req: &Parts,
116122
conn: &mut AsyncPgConnection,
117123
) -> AppResult<PaginatedVersionsAndPublishers> {
@@ -148,7 +154,7 @@ async fn list_by_date(
148154
.map(|p| req.query_with_params(p));
149155
};
150156

151-
let release_tracks = if include.release_tracks {
157+
let release_tracks = if params.include()?.release_tracks {
152158
let mut sorted_versions = IndexSet::new();
153159
if options.is_some() {
154160
versions::table
@@ -216,12 +222,13 @@ async fn list_by_date(
216222
async fn list_by_semver(
217223
crate_id: i32,
218224
options: Option<&PaginationOptions>,
219-
include: ShowIncludeMode,
225+
params: &ListQueryParams,
220226
req: &Parts,
221227
conn: &mut AsyncPgConnection,
222228
) -> AppResult<PaginatedVersionsAndPublishers> {
223229
use seek::*;
224230

231+
let include = params.include()?;
225232
let (data, total, release_tracks) = if let Some(options) = options {
226233
// Since versions will only increase in the future and both sorting and pagination need to
227234
// happen on the app server, implementing it with fetching only the data needed for sorting

0 commit comments

Comments
 (0)