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
#[cfg(feature = "netidx")]
use derive::FromValue;
#[cfg(feature = "netidx")]
use netidx_derive::Pack;
use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "netidx", derive(Pack, FromValue))]
pub struct Amount<T: 'static, U: 'static> {
    amount: T,
    unit: U,
}

impl<T: 'static, U: 'static> Amount<T, U> {
    pub fn new(amount: T, unit: U) -> Self {
        Self { amount, unit }
    }

    pub fn as_scalar(&self) -> &T {
        &self.amount
    }

    pub fn unit(&self) -> &U {
        &self.unit
    }
}

impl<T: Ord + 'static, U: Eq + 'static> PartialOrd for Amount<T, U> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        if self.unit == other.unit {
            self.amount.partial_cmp(&other.amount)
        } else {
            None
        }
    }
}