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)
.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));

View File

@ -35,8 +35,8 @@ glib::wrapper! {
pub struct Date(ObjectSubclass<DatePrivate>) @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();

View File

@ -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<EventsPrivate>) @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

View File

@ -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 {

View File

@ -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<TransitCardPrivate>) @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

View File

@ -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<RefCell<Option<SunMoon>>>,
}
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<TransitClockPrivate>) @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();

View File

@ -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();

View File

@ -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<Item = &'a str>,
) -> chrono::DateTime<chrono::Utc> {
let partoj = iter.collect::<Vec<&str>>();
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::<Vec<&str>>();
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<Option<YearlyEvents>> {
.lines()
.map(|line| {
match line
.split(" ")
.split(' ')
.filter(|elem| !elem.is_empty())
.collect::<Vec<&str>>()
.as_slice()
@ -134,7 +135,7 @@ pub struct Solstices(HashMap<i32, YearlyEvents>);
impl Solstices {
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> {
@ -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<Vec<Option<YearlyEvents>>> 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<Vec<Option<YearlyEvents>>> 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()
);
}
}

View File

@ -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::<Utc>::from(dt_local))
.map(DateTime::<Utc>::from)
.unwrap_or(
Local::now()
.with_hour(0)

View File

@ -13,11 +13,13 @@ pub struct MemoryCacheRecord<T> {
pub struct MemoryCache<T>(Arc<RwLock<HashMap<String, MemoryCacheRecord<T>>>>);
impl<T: Clone> MemoryCache<T> {
pub fn new() -> MemoryCache<T> {
MemoryCache(Arc::new(RwLock::new(HashMap::new())))
impl<T: Clone> Default for MemoryCache<T> {
fn default() -> Self {
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 {
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))