cephas 0.1.0

Prudent terminal counters and progress helpers
Documentation
#![forbid(unsafe_code)]

//! # cephas
//!
//! Lightweight counter utilities inspired by the command-loop helpers in this
//! repository.

/// Small rolling counter with optional reset support.
///
/// The counter is intentionally tiny so it can be inlined by command-loop
/// logic without pulling in heavier dependencies.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct CephasCounter {
    value: u64,
}

impl CephasCounter {
    /// Create a new zero-valued counter.
    pub const fn new() -> Self {
        Self { value: 0 }
    }

    /// Read the current value.
    pub const fn value(&self) -> u64 {
        self.value
    }

    /// Increment and return the new value.
    pub fn bump(&mut self) -> u64 {
        self.value = self.value.saturating_add(1);
        self.value
    }

    /// Reset the counter to zero.
    pub fn reset(&mut self) {
        self.value = 0;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn counter_starts_at_zero() {
        let c = CephasCounter::new();
        assert_eq!(c.value(), 0);
    }

    #[test]
    fn counter_bumps() {
        let mut c = CephasCounter::new();
        assert_eq!(c.bump(), 1);
        assert_eq!(c.bump(), 2);
        assert_eq!(c.value(), 2);
    }

    #[test]
    fn counter_resets() {
        let mut c = CephasCounter::new();
        c.bump();
        c.reset();
        assert_eq!(c.value(), 0);
    }
}