Resolve warnings in memorycache and dashboard

This commit is contained in:
Savanni D'Gerinel 2023-10-04 17:28:55 -04:00
parent 3f2feee4dd
commit 79422b5c7a
10 changed files with 77 additions and 65 deletions

View File

@ -40,16 +40,16 @@ impl ApplicationWindow {
.vexpand(true) .vexpand(true)
.build(); .build();
let date_label = Date::new(); let date_label = Date::default();
layout.append(&date_label); layout.append(&date_label);
let events = Events::new(); let events = Events::default();
layout.append(&events); layout.append(&events);
let transit_card = TransitCard::new(); let transit_card = TransitCard::default();
layout.append(&transit_card); layout.append(&transit_card);
let transit_clock = TransitClock::new(); let transit_clock = TransitClock::default();
layout.append(&transit_clock); layout.append(&transit_clock);
window.set_content(Some(&layout)); window.set_content(Some(&layout));

View File

@ -35,8 +35,8 @@ glib::wrapper! {
pub struct Date(ObjectSubclass<DatePrivate>) @extends gtk::Box, gtk::Widget; pub struct Date(ObjectSubclass<DatePrivate>) @extends gtk::Box, gtk::Widget;
} }
impl Date { impl Default for Date {
pub fn new() -> Self { fn default() -> Self {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.set_margin_bottom(8); s.set_margin_bottom(8);
s.set_margin_top(8); s.set_margin_top(8);
@ -48,7 +48,9 @@ impl Date {
s.redraw(); s.redraw();
s s
} }
}
impl Date {
pub fn update_date(&self, date: IFC) { pub fn update_date(&self, date: IFC) {
*self.imp().date.borrow_mut() = date; *self.imp().date.borrow_mut() = date;
self.redraw(); self.redraw();

View File

@ -1,13 +1,12 @@
use crate::{ use crate::{
components::Date, components::Date,
solstices::{self, YearlyEvents}, solstices::{self, YearlyEvents},
soluna_client::SunMoon,
}; };
use chrono::TimeZone;
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*, IconLookupFlags}; use gtk::{prelude::*, subclass::prelude::*};
use ifc::IFC; use ifc::IFC;
/*
#[derive(PartialEq)] #[derive(PartialEq)]
pub enum UpcomingEvent { pub enum UpcomingEvent {
SpringEquinox, SpringEquinox,
@ -15,25 +14,15 @@ pub enum UpcomingEvent {
AutumnEquinox, AutumnEquinox,
WinterSolstice, WinterSolstice,
} }
*/
#[derive(Default)]
pub struct EventsPrivate { pub struct EventsPrivate {
spring_equinox: Date, spring_equinox: Date,
summer_solstice: Date, summer_solstice: Date,
autumn_equinox: Date, autumn_equinox: Date,
winter_solstice: Date, winter_solstice: Date,
next: UpcomingEvent, // 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,
}
}
} }
#[glib::object_subclass] #[glib::object_subclass]
@ -51,8 +40,8 @@ glib::wrapper! {
pub struct Events(ObjectSubclass<EventsPrivate>) @extends gtk::Widget, gtk::Box, @implements gtk::Orientable; pub struct Events(ObjectSubclass<EventsPrivate>) @extends gtk::Widget, gtk::Box, @implements gtk::Orientable;
} }
impl Events { impl Default for Events {
pub fn new() -> Self { fn default() -> Self {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Horizontal); s.set_orientation(gtk::Orientation::Horizontal);
s.set_spacing(8); s.set_spacing(8);
@ -64,7 +53,9 @@ impl Events {
s s
} }
}
impl Events {
pub fn set_events(&self, events: YearlyEvents, next_event: solstices::Event) { pub fn set_events(&self, events: YearlyEvents, next_event: solstices::Event) {
self.imp() self.imp()
.spring_equinox .spring_equinox

View File

@ -1,6 +1,5 @@
use crate::soluna_client::SunMoon;
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*, IconLookupFlags}; use gtk::{prelude::*, subclass::prelude::*};
#[derive(Default)] #[derive(Default)]
pub struct LabelPrivate { pub struct LabelPrivate {

View File

@ -1,6 +1,6 @@
use crate::{components::Label, soluna_client::SunMoon}; use crate::{components::Label, soluna_client::SunMoon};
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*, IconLookupFlags}; use gtk::{prelude::*, subclass::prelude::*};
pub struct TransitCardPrivate { pub struct TransitCardPrivate {
sunrise: Label, sunrise: Label,
@ -35,8 +35,8 @@ glib::wrapper! {
pub struct TransitCard(ObjectSubclass<TransitCardPrivate>) @extends gtk::Grid, gtk::Widget; pub struct TransitCard(ObjectSubclass<TransitCardPrivate>) @extends gtk::Grid, gtk::Widget;
} }
impl TransitCard { impl Default for TransitCard {
pub fn new() -> Self { fn default() -> Self {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.add_css_class("card"); s.add_css_class("card");
s.set_column_homogeneous(true); s.set_column_homogeneous(true);
@ -48,7 +48,9 @@ impl TransitCard {
s s
} }
}
impl TransitCard {
pub fn update_transit(&self, transit_info: &SunMoon) { pub fn update_transit(&self, transit_info: &SunMoon) {
self.imp() self.imp()
.sunrise .sunrise

View File

@ -7,18 +7,11 @@ use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};
use std::{cell::RefCell, f64::consts::PI, rc::Rc}; use std::{cell::RefCell, f64::consts::PI, rc::Rc};
#[derive(Default)]
pub struct TransitClockPrivate { pub struct TransitClockPrivate {
info: Rc<RefCell<Option<SunMoon>>>, info: Rc<RefCell<Option<SunMoon>>>,
} }
impl Default for TransitClockPrivate {
fn default() -> Self {
Self {
info: Rc::new(RefCell::new(None)),
}
}
}
#[glib::object_subclass] #[glib::object_subclass]
impl ObjectSubclass for TransitClockPrivate { impl ObjectSubclass for TransitClockPrivate {
const NAME: &'static str = "TransitClock"; const NAME: &'static str = "TransitClock";
@ -34,8 +27,8 @@ glib::wrapper! {
pub struct TransitClock(ObjectSubclass<TransitClockPrivate>) @extends gtk::DrawingArea, gtk::Widget; pub struct TransitClock(ObjectSubclass<TransitClockPrivate>) @extends gtk::DrawingArea, gtk::Widget;
} }
impl TransitClock { impl Default for TransitClock {
pub fn new() -> Self { fn default() -> Self {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.set_width_request(500); s.set_width_request(500);
s.set_height_request(500); s.set_height_request(500);
@ -100,7 +93,9 @@ impl TransitClock {
s s
} }
}
impl TransitClock {
pub fn update_transit(&self, transit_info: SunMoon) { pub fn update_transit(&self, transit_info: SunMoon) {
*self.imp().info.borrow_mut() = Some(transit_info); *self.imp().info.borrow_mut() = Some(transit_info);
self.queue_draw(); self.queue_draw();

View File

@ -90,7 +90,7 @@ pub fn main() {
tx: Arc::new(RwLock::new(None)), tx: Arc::new(RwLock::new(None)),
}; };
let _ = runtime.spawn({ runtime.spawn({
let core = core.clone(); let core = core.clone();
async move { async move {
let soluna_client = SolunaClient::new(); let soluna_client = SolunaClient::new();

View File

@ -1,11 +1,10 @@
use chrono;
use chrono::prelude::*; use chrono::prelude::*;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
// http://astropixels.com/ephemeris/soleq2001.html // 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 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 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 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>( fn parse_time<'a>(
jaro: &str, year: &str,
iter: impl Iterator<Item = &'a str>, iter: impl Iterator<Item = &'a str>,
) -> chrono::DateTime<chrono::Utc> { ) -> chrono::DateTime<chrono::Utc> {
let partoj = iter.collect::<Vec<&str>>(); let parts = iter.collect::<Vec<&str>>();
let p = format!("{} {} {} {}", jaro, partoj[0], partoj[1], partoj[2]); let p = format!("{} {} {} {}", year, parts[0], parts[1], parts[2]);
chrono::Utc.datetime_from_str(&p, "%Y %b %d %H:%M").unwrap() NaiveDateTime::parse_from_str(&p, "%Y %b %d %H:%M")
.unwrap()
.and_utc()
} }
fn parse_line(year: &str, rest: &[&str]) -> YearlyEvents { fn parse_line(year: &str, rest: &[&str]) -> YearlyEvents {
@ -118,7 +119,7 @@ fn parse_events() -> Vec<Option<YearlyEvents>> {
.lines() .lines()
.map(|line| { .map(|line| {
match line match line
.split(" ") .split(' ')
.filter(|elem| !elem.is_empty()) .filter(|elem| !elem.is_empty())
.collect::<Vec<&str>>() .collect::<Vec<&str>>()
.as_slice() .as_slice()
@ -134,7 +135,7 @@ pub struct Solstices(HashMap<i32, YearlyEvents>);
impl Solstices { impl Solstices {
pub fn yearly_events(&self, year: i32) -> Option<YearlyEvents> { pub fn yearly_events(&self, year: i32) -> Option<YearlyEvents> {
self.0.get(&year).map(|c| c.clone()) self.0.get(&year).copied()
} }
pub fn next_event(&self, date: chrono::DateTime<chrono::Utc>) -> Option<Event> { pub fn next_event(&self, date: chrono::DateTime<chrono::Utc>) -> Option<Event> {
@ -142,17 +143,17 @@ impl Solstices {
match year_events { match year_events {
Some(year_events) => { Some(year_events) => {
if date <= year_events.spring_equinox { 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 { } 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 { } 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 { } else if date <= year_events.winter_solstice {
Some(Event::WinterSolstice(year_events.winter_solstice.clone())) Some(Event::WinterSolstice(year_events.winter_solstice))
} else { } else {
self.0 self.0
.get(&(date.year() + 1)) .get(&(date.year() + 1))
.map(|_| Event::SpringEquinox(year_events.spring_equinox.clone())) .map(|_| Event::SpringEquinox(year_events.spring_equinox))
} }
} }
None => None, None => None,
@ -165,7 +166,7 @@ impl From<Vec<Option<YearlyEvents>>> for Solstices {
Solstices(event_list.iter().fold(HashMap::new(), |mut m, record| { Solstices(event_list.iter().fold(HashMap::new(), |mut m, record| {
match record { match record {
Some(record) => { Some(record) => {
m.insert(record.year, record.clone()); m.insert(record.year, *record);
} }
None => (), None => (),
} }
@ -177,3 +178,24 @@ impl From<Vec<Option<YearlyEvents>>> for Solstices {
lazy_static! { lazy_static! {
pub static ref EVENTS: Solstices = Solstices::from(parse_events()); 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()
);
}
}

View File

@ -4,7 +4,6 @@
use chrono::{DateTime, Duration, Local, NaiveTime, Offset, TimeZone, Timelike, Utc}; use chrono::{DateTime, Duration, Local, NaiveTime, Offset, TimeZone, Timelike, Utc};
use geo_types::{Latitude, Longitude}; use geo_types::{Latitude, Longitude};
use memorycache::MemoryCache; use memorycache::MemoryCache;
use reqwest;
use serde::Deserialize; use serde::Deserialize;
const ENDPOINT: &str = "https://api.solunar.org/solunar"; const ENDPOINT: &str = "https://api.solunar.org/solunar";
@ -26,8 +25,8 @@ impl SunMoon {
let sunrise = parse_time(val.sunrise).unwrap(); let sunrise = parse_time(val.sunrise).unwrap();
let sunset = parse_time(val.sunset).unwrap(); let sunset = parse_time(val.sunset).unwrap();
let moonrise = val.moonrise.and_then(|v| parse_time(v)); let moonrise = val.moonrise.and_then(parse_time);
let moonset = val.moonset.and_then(|v| parse_time(v)); let moonset = val.moonset.and_then(parse_time);
Self { Self {
sunrise, sunrise,
@ -82,7 +81,7 @@ impl SolunaClient {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
client: reqwest::Client::new(), client: reqwest::Client::new(),
memory_cache: MemoryCache::new(), memory_cache: MemoryCache::default(),
} }
} }
@ -110,7 +109,7 @@ impl SolunaClient {
.get(reqwest::header::EXPIRES) .get(reqwest::header::EXPIRES)
.and_then(|header| header.to_str().ok()) .and_then(|header| header.to_str().ok())
.and_then(|expiration| DateTime::parse_from_rfc2822(expiration).ok()) .and_then(|expiration| DateTime::parse_from_rfc2822(expiration).ok())
.map(|dt_local| DateTime::<Utc>::from(dt_local)) .map(DateTime::<Utc>::from)
.unwrap_or( .unwrap_or(
Local::now() Local::now()
.with_hour(0) .with_hour(0)

View File

@ -13,11 +13,13 @@ pub struct MemoryCacheRecord<T> {
pub struct MemoryCache<T>(Arc<RwLock<HashMap<String, MemoryCacheRecord<T>>>>); pub struct MemoryCache<T>(Arc<RwLock<HashMap<String, MemoryCacheRecord<T>>>>);
impl<T: Clone> MemoryCache<T> { impl<T: Clone> Default for MemoryCache<T> {
pub fn new() -> MemoryCache<T> { fn default() -> Self {
MemoryCache(Arc::new(RwLock::new(HashMap::new()))) Self(Arc::new(RwLock::new(HashMap::new())))
} }
}
impl<T: Clone> MemoryCache<T> {
pub async fn find(&self, key: &str, f: impl Future<Output = (DateTime<Utc>, T)>) -> T { pub async fn find(&self, key: &str, f: impl Future<Output = (DateTime<Utc>, T)>) -> T {
let val = { let val = {
let cache = self.0.read().unwrap(); let cache = self.0.read().unwrap();
@ -53,7 +55,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn it_runs_the_requestor_when_the_value_does_not_exist() { async fn it_runs_the_requestor_when_the_value_does_not_exist() {
let cache = MemoryCache::new(); let cache = MemoryCache::default();
let value = cache let value = cache
.find("my_key", async { (Utc::now(), Value(15)) }) .find("my_key", async { (Utc::now(), Value(15)) })
.await; .await;
@ -63,7 +65,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn it_runs_the_requestor_when_the_value_is_old() { async fn it_runs_the_requestor_when_the_value_is_old() {
let run = Arc::new(RwLock::new(false)); let run = Arc::new(RwLock::new(false));
let cache = MemoryCache::new(); let cache = MemoryCache::default();
let _ = cache let _ = cache
.find("my_key", async { .find("my_key", async {
(Utc::now() - Duration::seconds(10), Value(15)) (Utc::now() - Duration::seconds(10), Value(15))
@ -82,7 +84,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn it_returns_the_cached_value_when_the_value_is_new() { async fn it_returns_the_cached_value_when_the_value_is_new() {
let run = Arc::new(RwLock::new(false)); let run = Arc::new(RwLock::new(false));
let cache = MemoryCache::new(); let cache = MemoryCache::default();
let _ = cache let _ = cache
.find("my_key", async { .find("my_key", async {
(Utc::now() + Duration::seconds(10), Value(15)) (Utc::now() + Duration::seconds(10), Value(15))