Skip to content

Commit 86a8cb1

Browse files
committed
feat: add id filter to crates query
Gets information about multiple crates in a single call ```rust let query = CratesQuery { ids: Some(vec!["serde", "tokio"]), ..Default::default() }; ```
1 parent 4eb9172 commit 86a8cb1

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/async_client.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,31 @@ mod test {
569569
Ok(())
570570
}
571571

572+
#[tokio::test]
573+
async fn test_crates_filter_by_ids_async() -> Result<(), Error> {
574+
let client = build_test_client();
575+
576+
let ids = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
577+
.map(Into::into)
578+
.to_vec();
579+
let res = client
580+
.crates(CratesQuery {
581+
ids: Some(ids),
582+
per_page: 10,
583+
..Default::default()
584+
})
585+
.await?;
586+
587+
assert_eq!(
588+
res.crates.len(),
589+
10,
590+
"Expected 10 crates, actually got {}. Crates: {:#?}",
591+
res.crates.len(),
592+
res.crates
593+
);
594+
Ok(())
595+
}
596+
572597
#[tokio::test]
573598
async fn test_crate_reverse_dependency_count_async() -> Result<(), Error> {
574599
let client = build_test_client();

src/types.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
33
use chrono::{DateTime, NaiveDate, Utc};
44
use serde_derive::*;
5-
use std::collections::HashMap;
5+
use std::{collections::HashMap, fmt};
66

7-
/// Used to specify the sort behaviour of the `Client::crates()` method.
7+
/// A list of errors returned by the API.
88
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
99
pub struct ApiErrors {
1010
/// Individual errors.
1111
pub errors: Vec<ApiError>,
1212
}
1313

14-
/// Used to specify the sort behaviour of the `Client::crates()` method.
14+
/// An error returned by the API.
1515
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
1616
pub struct ApiError {
1717
/// Error message.
1818
pub detail: Option<String>,
1919
}
2020

21-
impl std::fmt::Display for ApiError {
22-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21+
impl fmt::Display for ApiError {
22+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2323
write!(
2424
f,
2525
"{}",
@@ -77,6 +77,8 @@ pub struct CratesQuery {
7777
pub(crate) category: Option<String>,
7878
/// Search query string.
7979
pub(crate) search: Option<String>,
80+
/// List of crate ids.
81+
pub(crate) ids: Option<Vec<String>>,
8082
}
8183

8284
impl CratesQuery {
@@ -93,6 +95,11 @@ impl CratesQuery {
9395
if let Some(cat) = &self.category {
9496
q.append_pair("category", cat);
9597
}
98+
if let Some(ids) = &self.ids {
99+
for id in ids {
100+
q.append_pair("ids[]", id);
101+
}
102+
}
96103
}
97104
}
98105

@@ -161,6 +168,16 @@ impl CratesQuery {
161168
pub fn set_search(&mut self, search: Option<String>) {
162169
self.search = search;
163170
}
171+
172+
/// Get a reference to the crate query's ids.
173+
pub fn ids(&self) -> Option<&Vec<String>> {
174+
self.ids.as_ref()
175+
}
176+
177+
/// Set the crate query's ids.
178+
pub fn set_ids(&mut self, ids: Option<Vec<String>>) {
179+
self.ids = ids;
180+
}
164181
}
165182

166183
impl Default for CratesQuery {
@@ -172,6 +189,7 @@ impl Default for CratesQuery {
172189
user_id: None,
173190
category: None,
174191
search: None,
192+
ids: None,
175193
}
176194
}
177195
}
@@ -235,6 +253,13 @@ impl CratesQueryBuilder {
235253
self
236254
}
237255

256+
/// List of crate ids.
257+
#[must_use]
258+
pub fn ids(mut self, ids: Vec<String>) -> Self {
259+
self.query.ids = Some(ids);
260+
self
261+
}
262+
238263
/// Finalize the builder into a usable [`CratesQuery`].
239264
#[must_use]
240265
pub fn build(self) -> CratesQuery {

0 commit comments

Comments
 (0)