architect_api/utils/
amount.rs

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