From 2fbbb4cf15b96b57b9425176bd57c97a1e9b9a71 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Wed, 4 Oct 2023 17:28:55 -0400 Subject: [PATCH] Resolve warnings in memorycache and dashboard --- dashboard/src/app_window.rs | 8 ++-- dashboard/src/components/date.rs | 6 ++- dashboard/src/components/events.rs | 27 ++++-------- dashboard/src/components/label.rs | 3 +- dashboard/src/components/transit_card.rs | 8 ++-- dashboard/src/components/transit_clock.rs | 15 +++---- dashboard/src/main.rs | 2 +- dashboard/src/solstices.rs | 50 ++++++++++++++++------- dashboard/src/soluna_client.rs | 9 ++-- memorycache/src/lib.rs | 14 ++++--- 10 files changed, 77 insertions(+), 65 deletions(-) diff --git a/dashboard/src/app_window.rs b/dashboard/src/app_window.rs index 57456b4..482352a 100644 --- a/dashboard/src/app_window.rs +++ b/dashboard/src/app_window.rs @@ -40,16 +40,16 @@ impl ApplicationWindow { .vexpand(true) .build(); - let date_label = Date::new(); + let date_label = Date::default(); layout.append(&date_label); - let events = Events::new(); + let events = Events::default(); layout.append(&events); - let transit_card = TransitCard::new(); + let transit_card = TransitCard::default(); layout.append(&transit_card); - let transit_clock = TransitClock::new(); + let transit_clock = TransitClock::default(); layout.append(&transit_clock); window.set_content(Some(&layout)); diff --git a/dashboard/src/components/date.rs b/dashboard/src/components/date.rs index f8698e4..0dd84e1 100644 --- a/dashboard/src/components/date.rs +++ b/dashboard/src/components/date.rs @@ -35,8 +35,8 @@ glib::wrapper! { pub struct Date(ObjectSubclass) @extends gtk::Box, gtk::Widget; } -impl Date { - pub fn new() -> Self { +impl Default for Date { + fn default() -> Self { let s: Self = Object::builder().build(); s.set_margin_bottom(8); s.set_margin_top(8); @@ -48,7 +48,9 @@ impl Date { s.redraw(); s } +} +impl Date { pub fn update_date(&self, date: IFC) { *self.imp().date.borrow_mut() = date; self.redraw(); diff --git a/dashboard/src/components/events.rs b/dashboard/src/components/events.rs index f1d072c..88a411d 100644 --- a/dashboard/src/components/events.rs +++ b/dashboard/src/components/events.rs @@ -1,13 +1,12 @@ use crate::{ components::Date, solstices::{self, YearlyEvents}, - soluna_client::SunMoon, }; -use chrono::TimeZone; use glib::Object; -use gtk::{prelude::*, subclass::prelude::*, IconLookupFlags}; +use gtk::{prelude::*, subclass::prelude::*}; use ifc::IFC; +/* #[derive(PartialEq)] pub enum UpcomingEvent { SpringEquinox, @@ -15,25 +14,15 @@ pub enum UpcomingEvent { AutumnEquinox, WinterSolstice, } +*/ +#[derive(Default)] pub struct EventsPrivate { spring_equinox: Date, summer_solstice: Date, autumn_equinox: Date, winter_solstice: Date, - next: UpcomingEvent, -} - -impl Default for EventsPrivate { - fn default() -> Self { - Self { - spring_equinox: Date::new(), - summer_solstice: Date::new(), - autumn_equinox: Date::new(), - winter_solstice: Date::new(), - next: UpcomingEvent::SpringEquinox, - } - } + // next: UpcomingEvent, } #[glib::object_subclass] @@ -51,8 +40,8 @@ glib::wrapper! { pub struct Events(ObjectSubclass) @extends gtk::Widget, gtk::Box, @implements gtk::Orientable; } -impl Events { - pub fn new() -> Self { +impl Default for Events { + fn default() -> Self { let s: Self = Object::builder().build(); s.set_orientation(gtk::Orientation::Horizontal); s.set_spacing(8); @@ -64,7 +53,9 @@ impl Events { s } +} +impl Events { pub fn set_events(&self, events: YearlyEvents, next_event: solstices::Event) { self.imp() .spring_equinox diff --git a/dashboard/src/components/label.rs b/dashboard/src/components/label.rs index b793ccc..17686f6 100644 --- a/dashboard/src/components/label.rs +++ b/dashboard/src/components/label.rs @@ -1,6 +1,5 @@ -use crate::soluna_client::SunMoon; use glib::Object; -use gtk::{prelude::*, subclass::prelude::*, IconLookupFlags}; +use gtk::{prelude::*, subclass::prelude::*}; #[derive(Default)] pub struct LabelPrivate { diff --git a/dashboard/src/components/transit_card.rs b/dashboard/src/components/transit_card.rs index 27334e7..3c8eb13 100644 --- a/dashboard/src/components/transit_card.rs +++ b/dashboard/src/components/transit_card.rs @@ -1,6 +1,6 @@ use crate::{components::Label, soluna_client::SunMoon}; use glib::Object; -use gtk::{prelude::*, subclass::prelude::*, IconLookupFlags}; +use gtk::{prelude::*, subclass::prelude::*}; pub struct TransitCardPrivate { sunrise: Label, @@ -35,8 +35,8 @@ glib::wrapper! { pub struct TransitCard(ObjectSubclass) @extends gtk::Grid, gtk::Widget; } -impl TransitCard { - pub fn new() -> Self { +impl Default for TransitCard { + fn default() -> Self { let s: Self = Object::builder().build(); s.add_css_class("card"); s.set_column_homogeneous(true); @@ -48,7 +48,9 @@ impl TransitCard { s } +} +impl TransitCard { pub fn update_transit(&self, transit_info: &SunMoon) { self.imp() .sunrise diff --git a/dashboard/src/components/transit_clock.rs b/dashboard/src/components/transit_clock.rs index be2f0de..44bef09 100644 --- a/dashboard/src/components/transit_clock.rs +++ b/dashboard/src/components/transit_clock.rs @@ -7,18 +7,11 @@ use glib::Object; use gtk::{prelude::*, subclass::prelude::*}; use std::{cell::RefCell, f64::consts::PI, rc::Rc}; +#[derive(Default)] pub struct TransitClockPrivate { info: Rc>>, } -impl Default for TransitClockPrivate { - fn default() -> Self { - Self { - info: Rc::new(RefCell::new(None)), - } - } -} - #[glib::object_subclass] impl ObjectSubclass for TransitClockPrivate { const NAME: &'static str = "TransitClock"; @@ -34,8 +27,8 @@ glib::wrapper! { pub struct TransitClock(ObjectSubclass) @extends gtk::DrawingArea, gtk::Widget; } -impl TransitClock { - pub fn new() -> Self { +impl Default for TransitClock { + fn default() -> Self { let s: Self = Object::builder().build(); s.set_width_request(500); s.set_height_request(500); @@ -100,7 +93,9 @@ impl TransitClock { s } +} +impl TransitClock { pub fn update_transit(&self, transit_info: SunMoon) { *self.imp().info.borrow_mut() = Some(transit_info); self.queue_draw(); diff --git a/dashboard/src/main.rs b/dashboard/src/main.rs index 07f10d8..fc932ae 100644 --- a/dashboard/src/main.rs +++ b/dashboard/src/main.rs @@ -90,7 +90,7 @@ pub fn main() { tx: Arc::new(RwLock::new(None)), }; - let _ = runtime.spawn({ + runtime.spawn({ let core = core.clone(); async move { let soluna_client = SolunaClient::new(); diff --git a/dashboard/src/solstices.rs b/dashboard/src/solstices.rs index 4908292..040d4c6 100644 --- a/dashboard/src/solstices.rs +++ b/dashboard/src/solstices.rs @@ -1,11 +1,10 @@ -use chrono; use chrono::prelude::*; use lazy_static::lazy_static; use serde_derive::{Deserialize, Serialize}; use std::collections::HashMap; // http://astropixels.com/ephemeris/soleq2001.html -const SOLSTICE_TEXT: &'static str = " +const SOLSTICE_TEXT: &str = " 2001 Mar 20 13:31 Jun 21 07:38 Sep 22 23:05 Dec 21 19:22 2002 Mar 20 19:16 Jun 21 13:25 Sep 23 04:56 Dec 22 01:15 2003 Mar 21 01:00 Jun 21 19:11 Sep 23 10:47 Dec 22 07:04 @@ -91,12 +90,14 @@ impl Event { } fn parse_time<'a>( - jaro: &str, + year: &str, iter: impl Iterator, ) -> chrono::DateTime { - let partoj = iter.collect::>(); - let p = format!("{} {} {} {}", jaro, partoj[0], partoj[1], partoj[2]); - chrono::Utc.datetime_from_str(&p, "%Y %b %d %H:%M").unwrap() + let parts = iter.collect::>(); + let p = format!("{} {} {} {}", year, parts[0], parts[1], parts[2]); + NaiveDateTime::parse_from_str(&p, "%Y %b %d %H:%M") + .unwrap() + .and_utc() } fn parse_line(year: &str, rest: &[&str]) -> YearlyEvents { @@ -118,7 +119,7 @@ fn parse_events() -> Vec> { .lines() .map(|line| { match line - .split(" ") + .split(' ') .filter(|elem| !elem.is_empty()) .collect::>() .as_slice() @@ -134,7 +135,7 @@ pub struct Solstices(HashMap); impl Solstices { pub fn yearly_events(&self, year: i32) -> Option { - self.0.get(&year).map(|c| c.clone()) + self.0.get(&year).copied() } pub fn next_event(&self, date: chrono::DateTime) -> Option { @@ -142,17 +143,17 @@ impl Solstices { match year_events { Some(year_events) => { if date <= year_events.spring_equinox { - Some(Event::SpringEquinox(year_events.spring_equinox.clone())) + Some(Event::SpringEquinox(year_events.spring_equinox)) } else if date <= year_events.summer_solstice { - Some(Event::SummerSolstice(year_events.summer_solstice.clone())) + Some(Event::SummerSolstice(year_events.summer_solstice)) } else if date <= year_events.autumn_equinox { - Some(Event::AutumnEquinox(year_events.autumn_equinox.clone())) + Some(Event::AutumnEquinox(year_events.autumn_equinox)) } else if date <= year_events.winter_solstice { - Some(Event::WinterSolstice(year_events.winter_solstice.clone())) + Some(Event::WinterSolstice(year_events.winter_solstice)) } else { self.0 .get(&(date.year() + 1)) - .map(|_| Event::SpringEquinox(year_events.spring_equinox.clone())) + .map(|_| Event::SpringEquinox(year_events.spring_equinox)) } } None => None, @@ -165,7 +166,7 @@ impl From>> for Solstices { Solstices(event_list.iter().fold(HashMap::new(), |mut m, record| { match record { Some(record) => { - m.insert(record.year, record.clone()); + m.insert(record.year, *record); } None => (), } @@ -177,3 +178,24 @@ impl From>> for Solstices { lazy_static! { pub static ref EVENTS: Solstices = Solstices::from(parse_events()); } + +#[cfg(test)] +mod test { + use chrono::{NaiveDate, NaiveDateTime}; + + #[test] + fn it_can_parse_a_solstice_time() { + let p = "2001 Mar 20 13:31".to_owned(); + let parsed_date = NaiveDateTime::parse_from_str(&p, "%Y %b %d %H:%M") + .unwrap() + .and_utc(); + assert_eq!( + parsed_date, + NaiveDate::from_ymd_opt(2001, 03, 20) + .unwrap() + .and_hms_opt(13, 31, 0) + .unwrap() + .and_utc() + ); + } +} diff --git a/dashboard/src/soluna_client.rs b/dashboard/src/soluna_client.rs index c533104..d783311 100644 --- a/dashboard/src/soluna_client.rs +++ b/dashboard/src/soluna_client.rs @@ -4,7 +4,6 @@ use chrono::{DateTime, Duration, Local, NaiveTime, Offset, TimeZone, Timelike, Utc}; use geo_types::{Latitude, Longitude}; use memorycache::MemoryCache; -use reqwest; use serde::Deserialize; const ENDPOINT: &str = "https://api.solunar.org/solunar"; @@ -26,8 +25,8 @@ impl SunMoon { let sunrise = parse_time(val.sunrise).unwrap(); let sunset = parse_time(val.sunset).unwrap(); - let moonrise = val.moonrise.and_then(|v| parse_time(v)); - let moonset = val.moonset.and_then(|v| parse_time(v)); + let moonrise = val.moonrise.and_then(parse_time); + let moonset = val.moonset.and_then(parse_time); Self { sunrise, @@ -82,7 +81,7 @@ impl SolunaClient { pub fn new() -> Self { Self { client: reqwest::Client::new(), - memory_cache: MemoryCache::new(), + memory_cache: MemoryCache::default(), } } @@ -110,7 +109,7 @@ impl SolunaClient { .get(reqwest::header::EXPIRES) .and_then(|header| header.to_str().ok()) .and_then(|expiration| DateTime::parse_from_rfc2822(expiration).ok()) - .map(|dt_local| DateTime::::from(dt_local)) + .map(DateTime::::from) .unwrap_or( Local::now() .with_hour(0) diff --git a/memorycache/src/lib.rs b/memorycache/src/lib.rs index 2245688..7ac9fb0 100644 --- a/memorycache/src/lib.rs +++ b/memorycache/src/lib.rs @@ -13,11 +13,13 @@ pub struct MemoryCacheRecord { pub struct MemoryCache(Arc>>>); -impl MemoryCache { - pub fn new() -> MemoryCache { - MemoryCache(Arc::new(RwLock::new(HashMap::new()))) +impl Default for MemoryCache { + fn default() -> Self { + Self(Arc::new(RwLock::new(HashMap::new()))) } +} +impl MemoryCache { pub async fn find(&self, key: &str, f: impl Future, T)>) -> T { let val = { let cache = self.0.read().unwrap(); @@ -53,7 +55,7 @@ mod tests { #[tokio::test] async fn it_runs_the_requestor_when_the_value_does_not_exist() { - let cache = MemoryCache::new(); + let cache = MemoryCache::default(); let value = cache .find("my_key", async { (Utc::now(), Value(15)) }) .await; @@ -63,7 +65,7 @@ mod tests { #[tokio::test] async fn it_runs_the_requestor_when_the_value_is_old() { let run = Arc::new(RwLock::new(false)); - let cache = MemoryCache::new(); + let cache = MemoryCache::default(); let _ = cache .find("my_key", async { (Utc::now() - Duration::seconds(10), Value(15)) @@ -82,7 +84,7 @@ mod tests { #[tokio::test] async fn it_returns_the_cached_value_when_the_value_is_new() { let run = Arc::new(RwLock::new(false)); - let cache = MemoryCache::new(); + let cache = MemoryCache::default(); let _ = cache .find("my_key", async { (Utc::now() + Duration::seconds(10), Value(15))