State Machine in Rust
#[derive(Clone)]
enum State {
Closed { failure_count: u32 },
Open { until: Instant },
HalfOpen,
}
impl State {
fn record_failure(&self, threshold: u32) -> State {
match self {
State::Closed { failure_count } if failure_count + 1 >= threshold =>
State::Open { until: Instant::now() + OPEN_DURATION },
State::Closed { failure_count } =>
State::Closed { failure_count: failure_count + 1 },
State::HalfOpen =>
State::Open { until: Instant::now() + OPEN_DURATION },
other => other.clone(),
}
}
}