aboutsummaryrefslogtreecommitdiff
path: root/rust/space-age/src/lib.rs
blob: 9d2448162ea02823ee13e8db77b849b08908782a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#[derive(Debug)]
pub struct Duration(f64);

impl From<u64> for Duration {
    fn from(s: u64) -> Self {
        Self(s as f64)
    }
}

pub trait Planet {
    const EARTH_SECONDS: f64 = 31557600.;
    const SECONDS_IN_YEAR: f64;
    fn years_during(d: &Duration) -> f64 { d.0 / Self::SECONDS_IN_YEAR }
}


macro_rules! planets {
    ( $( $planet:ident -> $ratio:expr ),* ) => {
        $(
            pub struct $planet;
            impl Planet for $planet {
                const SECONDS_IN_YEAR: f64 = $ratio * Self::EARTH_SECONDS;
            }
        )*
    }
}

planets!(
    Mercury -> 0.2408467,
    Venus -> 0.61519726,
    Earth -> 1.0,
    Mars -> 1.8808158,
    Jupiter -> 11.862615,
    Saturn -> 29.447498,
    Uranus -> 84.016846,
    Neptune -> 164.79132
);