intendednull / dm-toolkit

Native management tool for tabletop rpgs
0 stars 0 forks source link

Macros for Count trait and Unit enums #7

Closed intendednull closed 5 years ago

intendednull commented 5 years ago

Right now the required derive macro is a bit verbose.

#[derive(FromPrimitive, ToPrimitive, Debug, Copy, Clone, PartialEq, Eq)]
pub enum UnitTime {
    Second = 1,
    Minute = 60,
    Hour = 3600,
    Day = 86400,
    Week = 604800,
    Month = 2629800,
    Year = 31557600,
}

We should compress it down to something like

#[derive(Unit)]
...

Implementing the Count trait is also trivial.

pub struct Time {
    pub value: i64,
}

impl Count for Time {
    type Unit = UnitTime;

    fn value(&self) -> i64 {
        self.value
    }
    fn mut_value(&mut self) -> &mut i64 {
        &mut self.value
    }
}

All we'd need for macro input is the Unit and member to use.