summaryrefslogtreecommitdiffstats
path: root/src/metric/frequency.rs
blob: c35f874fec4c6e041f93e425c6b3331718c0a1b9 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::fmt::{Display, Formatter, Result};
use std::ops::{Add, Sub, AddAssign, SubAssign, Div, Mul, DivAssign, MulAssign};

use crate::metric::Unit;
use crate::metric::Primitive;

use crate::declare_unit_basics;

use crate::metric::time::Time;

#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
pub enum Frequency {
    Base(f64, fn(f64) -> f64, &'static str),
    Canonical(f64),
    Hertz(f64),
    Becquerel(f64),
}

impl Unit<Frequency> for Frequency {
    fn to_si_unit(&self) -> Frequency {
        match self {
            Frequency::Base(_, _, _) => self.clone(),
            Frequency::Canonical(value) => Frequency::Base(value.clone(), |x| x, "Hz"),
            Frequency::Hertz(value) => Frequency::Base(value.clone(), |x| x, "Hz"),
            Frequency::Becquerel(value) => Frequency::Base(value.clone(), |x| x, "Bq"),
        }
    }
}

declare_unit_basics!(Frequency);

// 1/s = Hz
impl Div<Time> for f64 {
    type Output = Frequency;

    fn div(self, rhs: Time) -> Self::Output {
        Frequency::Canonical(self * rhs.to_si_primitive())
    }
}

// Hz * s = 1
impl Mul<Time> for Frequency {
    type Output = f64;

    fn mul(self, rhs: Time) -> Self::Output {
        self.to_si_primitive() * rhs.to_si_primitive()
    }
}

// s * Hz = 1
impl Mul<Frequency> for Time {
    type Output = f64;

    fn mul(self, rhs: Frequency) -> Self::Output {
        self.to_si_primitive() * rhs.to_si_primitive()
    }
}

pub type Activity = Frequency;