Skip to content

Commit e868fd7

Browse files
committed
models/krate: Allow retrieval of crate versions from slice of reference
1 parent 9519ec2 commit e868fd7

File tree

3 files changed

+13
-24
lines changed

3 files changed

+13
-24
lines changed

src/controllers/krate/search.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,7 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
214214
)
215215
};
216216

217-
let perfect_matches = data.iter().map(|&(_, b, _, _, _)| b).collect::<Vec<_>>();
218-
let downloads = data
219-
.iter()
220-
.map(|&(_, _, total, recent, _)| (total, recent.unwrap_or(0)))
221-
.collect::<Vec<_>>();
222-
let crates = data
223-
.into_iter()
224-
.map(|(c, _, _, _, _)| c)
225-
.collect::<Vec<_>>();
217+
let crates = data.iter().map(|(c, ..)| c).collect::<Vec<_>>();
226218

227219
let versions: Vec<Version> = info_span!("db.query", message = "SELECT ... FROM versions")
228220
.in_scope(|| crates.versions().load(conn))?;
@@ -232,17 +224,15 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
232224
.map(TopVersions::from_versions);
233225

234226
let crates = versions
235-
.zip(crates)
236-
.zip(perfect_matches)
237-
.zip(downloads)
238-
.map(|(((max_version, krate), perfect_match), (total, recent))| {
227+
.zip(data)
228+
.map(|(max_version, (krate, perfect_match, total, recent, _))| {
239229
EncodableCrate::from_minimal(
240230
krate,
241231
Some(&max_version),
242232
Some(vec![]),
243233
perfect_match,
244234
total,
245-
Some(recent),
235+
Some(recent.unwrap_or(0)),
246236
)
247237
})
248238
.collect::<Vec<_>>();

src/controllers/summary.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,14 @@ pub async fn summary(state: AppState) -> AppResult<Json<Value>> {
2727
conn: &mut impl Conn,
2828
data: Vec<(Crate, i64, Option<i64>)>,
2929
) -> AppResult<Vec<EncodableCrate>> {
30-
let downloads = data
31-
.iter()
32-
.map(|&(_, total, recent)| (total, recent))
33-
.collect::<Vec<_>>();
34-
35-
let krates = data.into_iter().map(|(c, _, _)| c).collect::<Vec<_>>();
36-
30+
let krates = data.iter().map(|(c, ..)| c).collect::<Vec<_>>();
3731
let versions: Vec<Version> = krates.versions().load(conn)?;
3832
versions
3933
.grouped_by(&krates)
4034
.into_iter()
4135
.map(TopVersions::from_versions)
42-
.zip(krates)
43-
.zip(downloads)
44-
.map(|((top_versions, krate), (total, recent))| {
36+
.zip(data)
37+
.map(|(top_versions, (krate, total, recent))| {
4538
Ok(EncodableCrate::from_minimal(
4639
krate,
4740
Some(&top_versions),

src/models/krate.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,12 @@ impl CrateVersions for [Crate] {
494494
}
495495
}
496496

497+
impl CrateVersions for [&Crate] {
498+
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg> {
499+
Version::belonging_to(self).into_boxed()
500+
}
501+
}
502+
497503
#[derive(Debug, Eq, PartialEq, thiserror::Error)]
498504
pub enum InvalidFeature {
499505
#[error("feature cannot be empty")]

0 commit comments

Comments
 (0)