use crate::error::MeterError;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Budget {
remaining: u64,
}
impl Budget {
pub fn new(micro_mata: u64) -> Self {
Self {
remaining: micro_mata,
}
}
pub fn remaining(&self) -> u64 {
self.remaining
}
pub fn can_afford(&self, cost: u64) -> bool {
cost <= self.remaining
}
pub fn charge(&mut self, cost: u64) -> Result<(), MeterError> {
if cost > self.remaining {
return Err(MeterError::OverBudget {
cost,
remaining: self.remaining,
});
}
self.remaining -= cost;
Ok(())
}
pub fn credit(&mut self, micro_mata: u64) {
self.remaining = self.remaining.saturating_add(micro_mata);
}
}