use std::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Version {
pub major: u64,
pub minor: u64,
pub patch: u64,
}
impl Version {
pub const fn new(major: u64, minor: u64, patch: u64) -> Self {
Self {
major,
minor,
patch,
}
}
pub fn parse(version: &str) -> Option<Self> {
let core = version.trim().split(['-', '+']).next()?;
let mut parts = core.split('.').map(|x| x.parse::<u64>().ok());
let version = Self::new(parts.next()??, parts.next()??, parts.next()??);
parts.next().is_none().then_some(version)
}
pub(crate) fn crate_version() -> Self {
Self::parse(env!("CARGO_PKG_VERSION")).expect("CARGO_PKG_VERSION")
}
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}