use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default,
)]
pub struct ViewNumber(pub u64);

impl ViewNumber {
    pub const GENESIS: Self = Self(0);

    pub fn next(self) -> Self {
        Self(self.0.checked_add(1).expect("view number overflow"))
    }

    pub fn as_u64(self) -> u64 {
        self.0
    }
}

impl fmt::Display for ViewNumber {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "v{}", self.0)
    }
}

impl From<u64> for ViewNumber {
    fn from(v: u64) -> Self {
        Self(v)
    }
}
