// ! A unified versioning system for Rust. #![no_std] use core::{fmt::Display, prelude::rust_2021::derive}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub struct Version { pub major: u8, pub minor: u8, pub patch: u8, } impl Version { pub fn new(major: u8, minor: u8, patch: u8) -> Self { Self { major, minor, patch, } } pub fn new_from_env(vstring: &'static str) -> Self { let mut spl = vstring.split("."); if spl.clone().count() > 3 { panic!("Improper version string"); } // TODO: handle failing any of these let major: u8 = spl.nth(0).unwrap().parse().unwrap(); let minor: u8 = spl.nth(1).unwrap().parse().unwrap(); let patch: u8 = spl.nth(2).unwrap().parse().unwrap(); Self { major, minor, patch, } } } impl Display for Version { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "v{}.{}.{}", self.major, self.minor, self.patch)?; Ok(()) } }