Skip to main content

llm/usage/
usd.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3use std::fmt;
4use std::ops::{Add, AddAssign};
5
6/// A monetary amount denominated in US dollars.
7#[repr(transparent)]
8#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd, Serialize, Deserialize, JsonSchema)]
9#[serde(transparent)]
10#[schemars(transparent)]
11pub struct Usd(f64);
12
13impl Usd {
14    pub const ZERO: Self = Self(0.0);
15
16    pub const fn new(value: f64) -> Self {
17        Self(value)
18    }
19
20    pub const fn get(self) -> f64 {
21        self.0
22    }
23}
24
25impl Add for Usd {
26    type Output = Self;
27    fn add(self, rhs: Self) -> Self::Output {
28        Self(self.0 + rhs.0)
29    }
30}
31
32impl AddAssign for Usd {
33    fn add_assign(&mut self, rhs: Self) {
34        self.0 += rhs.0;
35    }
36}
37
38impl fmt::Display for Usd {
39    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
40        self.0.fmt(formatter)
41    }
42}