From 3b576d2c39e63f05c1c919579f61866a7798dc1e Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 2 May 2020 15:42:29 +0530 Subject: typetag dosen't support generics, use macro hack until then --- src/habit.rs | 69 ++++++++++++++++++++++++++++++------------------------------ src/main.rs | 30 +++++++++++++------------- 2 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/habit.rs b/src/habit.rs index 45eeefa..13a2bd2 100644 --- a/src/habit.rs +++ b/src/habit.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use chrono::NaiveDate; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use cursive::direction::Direction; use cursive::event::{Event, EventResult}; @@ -15,7 +15,7 @@ pub enum TrackEvent { Decrement, } -#[derive(Copy, Clone, Debug, Serialize)] +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] pub struct CustomBool(bool); use std::fmt; @@ -53,6 +53,7 @@ pub trait Habit { fn modify(&mut self, date: NaiveDate, event: TrackEvent); } +#[typetag::serde(tag = "type")] pub trait HabitWrapper: erased_serde::Serialize { fn remaining(&self, date: NaiveDate) -> u32; fn total(&self) -> u32; @@ -63,39 +64,39 @@ pub trait HabitWrapper: erased_serde::Serialize { fn take_focus(&mut self, _: Direction) -> bool; } -use erased_serde::serialize_trait_object; -serialize_trait_object!(HabitWrapper); - -impl HabitWrapper for T -where - T: Habit + ShadowView, - T: Serialize, - T::HabitType: std::fmt::Display, -{ - fn remaining(&self, date: NaiveDate) -> u32 { - Habit::remaining(self, date) - } - fn total(&self) -> u32 { - Habit::total(self) - } - fn modify(&mut self, date: NaiveDate, event: TrackEvent) { - Habit::modify(self, date, event); - } - fn draw(&self, printer: &Printer) { - ShadowView::draw(self, printer) - } - fn on_event(&mut self, event: Event) -> EventResult { - ShadowView::on_event(self, event) - } - fn required_size(&mut self, x: Vec2) -> Vec2 { - ShadowView::required_size(self, x) - } - fn take_focus(&mut self, d: Direction) -> bool { - ShadowView::take_focus(self, d) - } +macro_rules! auto_habit_impl { + ($struct_name:ident) => { + #[typetag::serde] + impl HabitWrapper for $struct_name { + fn remaining(&self, date: NaiveDate) -> u32 { + Habit::remaining(self, date) + } + fn total(&self) -> u32 { + Habit::total(self) + } + fn modify(&mut self, date: NaiveDate, event: TrackEvent) { + Habit::modify(self, date, event); + } + fn draw(&self, printer: &Printer) { + ShadowView::draw(self, printer) + } + fn on_event(&mut self, event: Event) -> EventResult { + ShadowView::on_event(self, event) + } + fn required_size(&mut self, x: Vec2) -> Vec2 { + ShadowView::required_size(self, x) + } + fn take_focus(&mut self, d: Direction) -> bool { + ShadowView::take_focus(self, d) + } + } + }; } -#[derive(Debug, Serialize)] +auto_habit_impl!(Count); +auto_habit_impl!(Bit); + +#[derive(Debug, Serialize, Deserialize)] pub struct Count { name: String, stats: HashMap, @@ -170,7 +171,7 @@ impl Habit for Count { } } -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct Bit { name: String, stats: HashMap, diff --git a/src/main.rs b/src/main.rs index cb3393b..2037a51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,25 +57,25 @@ fn main() { let mut s = Cursive::default(); let mut gymming = Count::new("gym", 5); - gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 11), 7); - gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 12), 8); - gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 13), 9); - gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 14), 10); - gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 15), 11); + gymming.insert_entry(NaiveDate::from_ymd(2020, 4, 11), 7); + gymming.insert_entry(NaiveDate::from_ymd(2020, 4, 12), 8); + gymming.insert_entry(NaiveDate::from_ymd(2020, 4, 13), 9); + gymming.insert_entry(NaiveDate::from_ymd(2020, 4, 14), 10); + gymming.insert_entry(NaiveDate::from_ymd(2020, 4, 15), 11); let mut reading = Bit::new("read"); - reading.insert_entry(NaiveDate::from_ymd(2020, 3, 11), true.into()); - reading.insert_entry(NaiveDate::from_ymd(2020, 3, 12), false.into()); - reading.insert_entry(NaiveDate::from_ymd(2020, 3, 13), true.into()); - reading.insert_entry(NaiveDate::from_ymd(2020, 3, 14), false.into()); - reading.insert_entry(NaiveDate::from_ymd(2020, 3, 15), true.into()); + reading.insert_entry(NaiveDate::from_ymd(2020, 4, 11), true.into()); + reading.insert_entry(NaiveDate::from_ymd(2020, 4, 12), false.into()); + reading.insert_entry(NaiveDate::from_ymd(2020, 4, 13), true.into()); + reading.insert_entry(NaiveDate::from_ymd(2020, 4, 14), false.into()); + reading.insert_entry(NaiveDate::from_ymd(2020, 4, 15), true.into()); let mut walking = Bit::new("walk"); - walking.insert_entry(NaiveDate::from_ymd(2020, 3, 11), true.into()); - walking.insert_entry(NaiveDate::from_ymd(2020, 3, 12), false.into()); - walking.insert_entry(NaiveDate::from_ymd(2020, 3, 13), true.into()); - walking.insert_entry(NaiveDate::from_ymd(2020, 3, 14), false.into()); - walking.insert_entry(NaiveDate::from_ymd(2020, 3, 15), true.into()); + walking.insert_entry(NaiveDate::from_ymd(2020, 4, 11), true.into()); + walking.insert_entry(NaiveDate::from_ymd(2020, 4, 12), false.into()); + walking.insert_entry(NaiveDate::from_ymd(2020, 4, 13), true.into()); + walking.insert_entry(NaiveDate::from_ymd(2020, 4, 14), false.into()); + walking.insert_entry(NaiveDate::from_ymd(2020, 4, 15), true.into()); let mut app = App::new(); app.add_habit(Box::new(gymming)); -- cgit v1.2.3