use crate::domain_primitives::{Amount, Count};
use pipecircuit::{StateMachine, StepInputOf};
pub struct Counter {
count: Count,
}
impl Counter {
pub const fn new(count: Count) -> Self {
Self { count }
}
}
impl StateMachine for Counter {}
pub struct Add {
pub amount: Amount,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AddOutcome {
Added { before: Count, after: Count },
Overflow { current: Count, requested: Amount },
}
impl StepInputOf<Counter> for Add {
type Outcome = AddOutcome;
fn step(self, counter: &mut Counter) -> Self::Outcome {
let before = counter.count;
match before.get().checked_add(self.amount.get()) {
Some(value) => {
let after = Count::new(value);
counter.count = after;
AddOutcome::Added { before, after }
}
None => AddOutcome::Overflow {
current: before,
requested: self.amount,
},
}
}
}
pub struct Read;
impl StepInputOf<Counter> for Read {
type Outcome = Count;
fn step(self, counter: &mut Counter) -> Self::Outcome {
counter.count
}
}
#[cfg(kani)]
mod proofs;
#[cfg(test)]
mod tests;
#[cfg(test)]
mod architecture;