From 2318a33f9b359ae27b52cd9a19db1f6782d8dae3 Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 1 Aug 2024 22:50:28 +0300 Subject: Upgrade dependencies and fix deprecated functionality I think I managed to not lose any functionality from my dependencies. sqlparser remains unupgraded, but that's mostly because it is only used in one example and it's not worth it to upgrade right now. --- templates/Cargo.toml | 11 ++++--- templates/src/lib.rs | 81 ++++++++++++++++++++++++---------------------------- 2 files changed, 45 insertions(+), 47 deletions(-) (limited to 'templates') diff --git a/templates/Cargo.toml b/templates/Cargo.toml index 9be5f30..6209e76 100644 --- a/templates/Cargo.toml +++ b/templates/Cargo.toml @@ -12,16 +12,19 @@ walkdir = "^2.3.2" [dev-dependencies] faker_rand = "^0.1.1" rand = "^0.8.5" +[dev-dependencies.time] +version = "^0.3.34" +features = ["parsing", "formatting"] [dev-dependencies.microformats] -version="^0.3.0" +version="^0.9.1" [dependencies] ellipse = "^0.2.0" -http = "^0.2.7" -markup = "^0.13.1" +http = "^1.0" +markup = "^0.15.0" serde_json = "^1.0.64" include_dir = "^0.7.2" -axum = "^0.6.18" +axum = "^0.7.5" [dependencies.chrono] version = "^0.4.19" features = ["serde"] diff --git a/templates/src/lib.rs b/templates/src/lib.rs index dd263e9..b7b9a24 100644 --- a/templates/src/lib.rs +++ b/templates/src/lib.rs @@ -56,9 +56,8 @@ mod tests { use faker_rand::en_us::internet::Domain; use faker_rand::lorem::Word; use microformats::types::{Document, Item, PropertyValue, Url}; + use rand::distributions::Distribution; use serde_json::json; - use std::cell::RefCell; - use std::rc::Rc; enum PostType { Note, @@ -99,7 +98,17 @@ mod tests { rand::random::(), rand::random::() ); - let dt = chrono::offset::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true); + let dt = time::OffsetDateTime::now_utc() + .to_offset( + time::UtcOffset::from_hms( + rand::distributions::Uniform::new(-11, 12) + .sample(&mut rand::thread_rng()), + if rand::random::() { 0 } else { 30 }, + 0 + ).unwrap() + ) + .format(&time::format_description::well_known::Rfc3339) + .unwrap(); match kind { PostType::Note => { @@ -189,46 +198,38 @@ mod tests { } } - fn check_dt_published(mf2: &serde_json::Value, item: &Rc>) { + fn check_dt_published(mf2: &serde_json::Value, item: &Item) { use microformats::types::temporal::Value as TemporalValue; - let _item = item.borrow(); - let props = _item.properties.borrow(); - assert!(props.contains_key("published")); + assert!(item.properties.contains_key("published")); if let Some(PropertyValue::Temporal(TemporalValue::Timestamp(item))) = - props.get("published").and_then(|v| v.first()) + item.properties.get("published").and_then(|v| v.first()) { - use chrono::{DateTime, FixedOffset, NaiveDateTime}; - // Faithfully reconstruct the original datetime // I wonder why not just have an Enum that would // get you either date, time or a datetime, // potentially with an offset? let offset = item.as_offset().unwrap().data; - let ndt: NaiveDateTime = item.as_date().unwrap().data - .and_time(item.as_time().unwrap().data) - // subtract the offset here, since we will add it back - - offset; - let dt = DateTime::::from_utc(ndt, offset); + let date = item.as_date().unwrap().data; + let time = item.as_time().unwrap().data; - let expected: DateTime = chrono::DateTime::parse_from_rfc3339( + let dt = date.with_time(time).assume_offset(offset); + let expected = time::OffsetDateTime::parse( mf2["properties"]["published"][0].as_str().unwrap(), - ) - .unwrap(); - + &time::format_description::well_known::Rfc3339 + ).unwrap(); + assert_eq!(dt, expected); } else { unreachable!() } } - fn check_e_content(mf2: &serde_json::Value, item: &Rc>) { - let _item = item.borrow(); - let props = _item.properties.borrow(); - assert!(props.contains_key("content")); + fn check_e_content(mf2: &serde_json::Value, item: &Item) { + assert!(item.properties.contains_key("content")); - if let Some(PropertyValue::Fragment(content)) = props.get("content").and_then(|v| v.first()) + if let Some(PropertyValue::Fragment(content)) = item.properties.get("content").and_then(|v| v.first()) { assert_eq!( content.html, @@ -240,7 +241,6 @@ mod tests { } #[test] - #[ignore = "see https://gitlab.com/maxburon/microformats-parser/-/issues/7"] fn test_note() { let mf2 = gen_random_post(&rand::random::().to_string(), PostType::Note); @@ -253,9 +253,8 @@ mod tests { .unwrap(); let parsed: Document = microformats::from_html(&html, url.clone()).unwrap(); - if let Some(PropertyValue::Item(item)) = parsed.get_item_by_url(&url) { - let _item = item.borrow(); - let props = _item.properties.borrow(); + if let Some(item) = parsed.into_iter().find(|i| i.properties.get("url").unwrap().contains(&PropertyValue::Url(url.clone()))) { + let props = &item.properties; check_e_content(&mf2, &item); check_dt_published(&mf2, &item); @@ -284,21 +283,20 @@ mod tests { .unwrap(); let parsed: Document = microformats::from_html(&html, url.clone()).unwrap(); - if let Some(PropertyValue::Item(item)) = parsed.get_item_by_url(&url) { - let _item = item.borrow(); - let props = _item.properties.borrow(); + if let Some(item) = parsed.into_iter().find(|i| i.properties.get("url").unwrap().contains(&PropertyValue::Url(url.clone()))) { check_e_content(&mf2, &item); check_dt_published(&mf2, &item); - assert!(props.contains_key("uid")); - assert!(props.contains_key("url")); - assert!(props + assert!(item.properties.contains_key("uid")); + assert!(item.properties.contains_key("url")); + assert!(item + .properties .get("url") .unwrap() .iter() - .any(|i| i == props.get("uid").and_then(|v| v.first()).unwrap())); - assert!(props.contains_key("name")); - if let Some(PropertyValue::Plain(name)) = props.get("name").and_then(|v| v.first()) { + .any(|i| i == item.properties.get("uid").and_then(|v| v.first()).unwrap())); + assert!(item.properties.contains_key("name")); + if let Some(PropertyValue::Plain(name)) = item.properties.get("name").and_then(|v| v.first()) { assert_eq!( name, mf2.pointer("/properties/name/0") @@ -337,13 +335,10 @@ mod tests { let html = crate::mf2::Entry { post: &mf2 }.to_string(); let parsed: Document = microformats::from_html(&html, url.clone()).unwrap(); - if let Some(item) = parsed.items.get(0) { - let _item = item.borrow(); - let props = _item.properties.borrow(); - + if let Some(item) = parsed.items.first() { check_dt_published(&mf2, item); - assert!(props.contains_key("like-of")); - match props.get("like-of").and_then(|v| v.first()) { + assert!(item.properties.contains_key("like-of")); + match item.properties.get("like-of").and_then(|v| v.first()) { Some(PropertyValue::Url(url)) => { assert_eq!( url, -- cgit 1.4.1