From 5d50f446a1a4612c0c931bdbc61f945760392f29 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Fri, 4 Nov 2022 23:41:21 +0100 Subject: "finished" update, added some tests --- src/apis/modrinth.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/apis') diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index 0c3eca5..c71b47f 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs @@ -1,3 +1,5 @@ +use std::io::{Error, ErrorKind}; +use chrono::{DateTime, FixedOffset}; use serde::Deserialize; use crate::{Modloader, List}; @@ -153,3 +155,24 @@ pub async fn versions(api: String, id: String, list: List) -> Vec { serde_json::from_slice(&data.await.unwrap()).unwrap() } + +pub fn extract_current_version(versions: Vec) -> Result> { + match versions.len() { + 0 => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_VERSIONS_AVAILABLE"))), + //TODO compare publish dates + 1.. => { + let mut times: Vec<(String, DateTime)> = vec![]; + for ver in versions { + let stamp = DateTime::parse_from_rfc3339(&ver.date_published)?; + times.push((ver.id, stamp)) + } + dbg!(×); + times.sort_by_key(|t| t.1); + times.reverse(); + dbg!(×); + println!("CW: {}", times[0].0); + Ok(times[0].0.to_string()) + }, + _ => panic!("available_versions should never be negative"), + } +} -- cgit v1.2.3