From a5151a3bf89d42393459e387d183db9040fd1014 Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Sat, 29 Jan 2022 14:58:36 +0100 Subject: [PATCH] feat: Add crate_reverse_dependency_count() Add method to get just the total count of reverse dependencies of a given crate. Closes #31. --- src/async_client.rs | 18 +++++++++++++++++- src/sync_client.rs | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/async_client.rs b/src/async_client.rs index a7315c7..c33f620 100644 --- a/src/async_client.rs +++ b/src/async_client.rs @@ -178,6 +178,12 @@ impl Client { Ok(deps) } + /// Get the total count of reverse dependencies for a given crate. + pub async fn crate_reverse_dependency_count(&self, crate_name: &str) -> Result { + let page = self.crate_reverse_dependencies_page(crate_name, 1).await?; + Ok(page.meta.total) + } + /// Retrieve the authors for a crate version. pub async fn crate_authors(&self, name: &str, version: &str) -> Result { let url = self @@ -402,7 +408,6 @@ mod test { Ok(()) } - #[tokio::test] async fn test_user_get_async() -> Result<(), Error> { let client = build_test_client(); @@ -434,4 +439,15 @@ mod test { Ok(()) } + + #[tokio::test] + async fn test_crate_reverse_dependency_count() -> Result<(), Error> { + let client = build_test_client(); + let count = client + .crate_reverse_dependency_count("crates_io_api") + .await?; + assert!(count > 0); + + Ok(()) + } } diff --git a/src/sync_client.rs b/src/sync_client.rs index 55c17ea..c0f8f5b 100644 --- a/src/sync_client.rs +++ b/src/sync_client.rs @@ -150,6 +150,12 @@ impl SyncClient { Ok(deps) } + /// Get the total count of reverse dependencies for a given crate. + pub fn crate_reverse_dependency_count(&self, crate_name: &str) -> Result { + let page = self.crate_reverse_dependencies_page(crate_name, 1)?; + Ok(page.meta.total) + } + /// Retrieve the authors for a crate version. pub fn crate_authors(&self, name: &str, version: &str) -> Result { let url = self @@ -395,4 +401,13 @@ mod test { Ok(()) } + + #[test] + fn test_crate_reverse_dependency_count() -> Result<(), Error> { + let client = build_test_client(); + let count = client.crate_reverse_dependency_count("crates_io_api")?; + assert!(count > 0); + + Ok(()) + } }