architect_api/utils/
amount.rs

1#[cfg(feature = "netidx")]
2use derive::FromValue;
3#[cfg(feature = "netidx")]
4use netidx_derive::Pack;
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8#[derive(
9    Default, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema,
10)]
11#[cfg_attr(feature = "netidx", derive(Pack, FromValue))]
12pub struct Amount<T: 'static, U: 'static> {
13    amount: T,
14    unit: U,
15}
16
17impl<T: 'static, U: 'static> Amount<T, U> {
18    pub fn new(amount: T, unit: U) -> Self {
19        Self { amount, unit }
20    }
21
22    pub fn as_scalar(&self) -> &T {
23        &self.amount
24    }
25
26    pub fn unit(&self) -> &U {
27        &self.unit
28    }
29}
30
31impl<T: Ord + 'static, U: Eq + 'static> PartialOrd for Amount<T, U> {
32    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
33        if self.unit == other.unit {
34            self.amount.partial_cmp(&other.amount)
35        } else {
36            None
37        }
38    }
39}