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
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std;
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Budget {
pub amount: u64,
pub consumption: u64,
}
impl Budget {
pub const fn new(amount: u64) -> Self {
Self {
consumption: 0,
amount,
}
}
pub fn remaining(&self) -> std::result::Result<u64, u64> {
if self.consumption <= self.amount {
Ok(self.amount - self.consumption)
} else {
Err(self.consumption - self.amount)
}
}
pub fn is_consumed(&self) -> bool {
self.consumption >= self.amount
}
}