From 2869d692e85720922a122dd5b4623a36ca0cfcf5 Mon Sep 17 00:00:00 2001 From: newt Date: Sat, 7 Sep 2024 02:11:07 +0100 Subject: [PATCH] fix: ensure only latest version of crate appears --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 16 +++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 6a9997e..e131386 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -150,6 +150,7 @@ dependencies = [ "color-eyre", "forgejo-api", "futures", + "semver", "tokio", "url", ] @@ -856,6 +857,12 @@ dependencies = [ "libc", ] +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + [[package]] name = "serde" version = "1.0.210" diff --git a/Cargo.toml b/Cargo.toml index 166b4bb..90e8880 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,6 @@ cargo_toml = "0.20.4" color-eyre = "0.6.3" forgejo-api = "0.4.1" futures = "0.3.30" +semver = "1.0.23" tokio = { version = "1.40.0", features = ["full"] } url = "2.5.2" diff --git a/src/main.rs b/src/main.rs index 76ea871..da0a5f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use forgejo_api::{ Forgejo, }; use futures::future::join_all; +use semver::Version; use std::env; use url::Url; @@ -58,7 +59,20 @@ async fn main() -> Result<()> { ) }) .filter_map(|(name, version)| version.map(|version| (name, version))) - .collect(); + .map(|(name, version)| (name, Version::parse(&version))) + .filter_map(|(name, version)| version.ok().map(|version| (name, version))) + // ensure we only have the latest version of each crate + .fold(Vec::new(), |mut acc, (name, version)| { + if let Some((_, existing_version)) = acc.iter().find(|(n, _)| n == &name) { + if &version > existing_version { + acc.retain(|(n, _)| n != &name); + acc.push((name, version)); + } + } else { + acc.push((name, version)); + } + acc + }); println!("{:?}", crates);