#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub(crate) struct NumericVersion {
pub(crate) major: u64,
pub(crate) minor: u64,
pub(crate) patch: u64,
}
impl NumericVersion {
pub(crate) const fn new(major: u64, minor: u64, patch: u64) -> Self {
Self {
major,
minor,
patch,
}
}
pub(crate) fn parse(text: &str) -> Option<Self> {
let start = text.find(|character: char| character.is_ascii_digit())?;
let token = text[start..]
.split_whitespace()
.next()?
.trim_start_matches('v');
let mut numbers = token.split(['.', '-']);
Some(Self {
major: numbers.next()?.parse().ok()?,
minor: numbers.next().unwrap_or("0").parse().ok()?,
patch: numbers.next().unwrap_or("0").parse().ok()?,
})
}
}
impl std::fmt::Display for NumericVersion {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{}.{}.{}", self.major, self.minor, self.patch)
}
}