Skip to content

Commit

Permalink
feat: add id filter to crates query
Browse files Browse the repository at this point in the history
Gets information about multiple crates in a single call

```rust
let query = CratesQuery {
    ids: Some(vec!["serde", "tokio"]),
    ..Default::default()
};
```
  • Loading branch information
joshka authored and theduke committed Oct 16, 2024
1 parent 4eb9172 commit 9fbf1da
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
25 changes: 25 additions & 0 deletions src/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,31 @@ mod test {
Ok(())
}

#[tokio::test]
async fn test_crates_filter_by_ids_async() -> Result<(), Error> {
let client = build_test_client();

let ids = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
.map(Into::into)
.to_vec();
let res = client
.crates(CratesQuery {
ids: Some(ids),
per_page: 10,
..Default::default()
})
.await?;

assert_eq!(
res.crates.len(),
10,
"Expected 10 crates, actually got {}. Crates: {:#?}",
res.crates.len(),
res.crates
);
Ok(())
}

#[tokio::test]
async fn test_crate_reverse_dependency_count_async() -> Result<(), Error> {
let client = build_test_client();
Expand Down
35 changes: 30 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

use chrono::{DateTime, NaiveDate, Utc};
use serde_derive::*;
use std::collections::HashMap;
use std::{collections::HashMap, fmt};

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

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

impl std::fmt::Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
Expand Down Expand Up @@ -77,6 +77,8 @@ pub struct CratesQuery {
pub(crate) category: Option<String>,
/// Search query string.
pub(crate) search: Option<String>,
/// List of crate ids.
pub(crate) ids: Option<Vec<String>>,
}

impl CratesQuery {
Expand All @@ -93,6 +95,11 @@ impl CratesQuery {
if let Some(cat) = &self.category {
q.append_pair("category", cat);
}
if let Some(ids) = &self.ids {
for id in ids {
q.append_pair("ids[]", id);
}
}
}
}

Expand Down Expand Up @@ -161,6 +168,16 @@ impl CratesQuery {
pub fn set_search(&mut self, search: Option<String>) {
self.search = search;
}

/// Get a reference to the crate query's ids.
pub fn ids(&self) -> Option<&Vec<String>> {
self.ids.as_ref()
}

/// Set the crate query's ids.
pub fn set_ids(&mut self, ids: Option<Vec<String>>) {
self.ids = ids;
}
}

impl Default for CratesQuery {
Expand All @@ -172,6 +189,7 @@ impl Default for CratesQuery {
user_id: None,
category: None,
search: None,
ids: None,
}
}
}
Expand Down Expand Up @@ -235,6 +253,13 @@ impl CratesQueryBuilder {
self
}

/// List of crate ids.
#[must_use]
pub fn ids(mut self, ids: Vec<String>) -> Self {
self.query.ids = Some(ids);
self
}

/// Finalize the builder into a usable [`CratesQuery`].
#[must_use]
pub fn build(self) -> CratesQuery {
Expand Down

0 comments on commit 9fbf1da

Please sign in to comment.