Skip to content

Commit

Permalink
Cache fetches to Crates.io API in check_crate_updates tool (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamGallagher737 authored Jul 11, 2024
1 parent de33cb5 commit 444acc8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions tools/check_crate_updates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
anyhow = "1.0.86"
cached = "0.52.0"
semver = "1.0.23"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
Expand Down
19 changes: 16 additions & 3 deletions tools/check_crate_updates/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::anyhow;
use cached::proc_macro::cached;
use std::{env, fs};
use table_extract::Table;
use ureq::{Agent, AgentBuilder};
Expand Down Expand Up @@ -140,6 +141,12 @@ fn main() -> anyhow::Result<()> {
Ok(())
}

#[cached(
result = true,
ty = "cached::SizedCache<String, CrateResponse>",
create = "{ cached::SizedCache::with_size(20) }",
convert = r#"{ name.to_string() }"#
)]
fn fetch_crate(name: &str, agent: Agent) -> anyhow::Result<CrateResponse> {
agent
.get(&format!("https://crates.io/api/v1/crates/{name}"))
Expand All @@ -149,6 +156,12 @@ fn fetch_crate(name: &str, agent: Agent) -> anyhow::Result<CrateResponse> {
.map_err(|e| anyhow!("Failed to parse crate data for {name:?}\n{e}"))
}

#[cached(
result = true,
ty = "cached::SizedCache<String, String>",
create = "{ cached::SizedCache::with_size(20) }",
convert = r#"{ c.data.name.to_string() }"#
)]
fn fetch_readme(c: &CrateResponse, agent: Agent) -> anyhow::Result<String> {
let path = &c.versions[0].readme_path; // index 0 is latest
agent
Expand Down Expand Up @@ -186,19 +199,19 @@ fn extract_versions_from_cell(input: &str) -> Vec<String> {
.collect()
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Clone)]
struct CrateResponse {
#[serde(rename = "crate")]
data: CrateDataResponse,
versions: Vec<CrateVersionResponse>,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Clone)]
struct CrateDataResponse {
name: String,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Clone)]
struct CrateVersionResponse {
#[serde(rename = "num")]
version: String,
Expand Down

0 comments on commit 444acc8

Please sign in to comment.