use crate::ParseError;
use ascii::AsciiStr;
use core::fmt::{self, Display, Formatter};
use core::str::FromStr;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Generation(u32);
impl Default for Generation {
fn default() -> Generation {
Generation(1)
}
}
impl Generation {
pub fn new(val: u32) -> Result<Self, ParseError> {
if val == 0 {
Err(ParseError::InvalidGeneration)
} else {
Ok(Self(val))
}
}
pub fn from_ascii(s: &AsciiStr) -> Result<Self, ParseError> {
let val = u32::from_str(s.as_str())
.map_err(|_| ParseError::InvalidGeneration)?;
Self::new(val)
}
#[must_use]
pub fn to_u32(self) -> u32 {
self.0
}
}
impl Display for Generation {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generation() {
assert_eq!(Generation::default(), Generation::new(1).unwrap());
assert_eq!(Generation::new(0), Err(ParseError::InvalidGeneration));
assert_eq!(
Generation::from_ascii(AsciiStr::from_ascii("123").unwrap())
.unwrap()
.to_u32(),
123
);
assert_eq!(
Generation::from_ascii(AsciiStr::from_ascii("123a").unwrap()),
Err(ParseError::InvalidGeneration)
);
assert_eq!(Generation::default().to_string(), "1");
}
}