1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use crate::error::*; use std::str::FromStr; #[derive(Copy, Clone, Eq, Debug)] pub struct Version { pub major: u64, pub minor: u64 } impl Version { #[allow(dead_code)] pub fn new(major: u64, minor: u64) -> Self { Self { major, minor } } } impl FromStr for Version { type Err = Error; fn from_str(s: &str) -> Result<Version> { let vv = s.split('.').collect::<Vec<&str>>(); match (vv.get(0), vv.get(1)) { (Some(maj), Some(min)) => Ok(Version { major: maj.parse()?, minor: min.parse()? }), _ => Err(ErrorKind::SpecVersion(s.into()).into()) } } } use std::cmp; impl PartialOrd for Version { fn partial_cmp(&self, other: &Version) -> Option<cmp::Ordering> { Some(self.cmp(other)) } } impl PartialEq for Version { fn eq(&self, other: &Version) -> bool { self.major == other.major && self.minor == other.minor } } impl Ord for Version { fn cmp(&self, other: &Version) -> cmp::Ordering { match self.major.cmp(&other.major) { cmp::Ordering::Equal => {} r => return r } match self.minor.cmp(&other.minor) { cmp::Ordering::Equal => {} r => return r } cmp::Ordering::Equal } } #[cfg(test)] mod tests { use super::*; #[test] fn version_parsing() { assert_eq!("1.3".parse::<Version>().unwrap(), Version::new(1, 3)) } #[test] fn version_comparison() { assert!(Version::new(1, 3) >= Version::new(1, 2)); assert!(Version::new(1, 2) >= Version::new(1, 2)); assert!(Version::new(1, 2) == Version::new(1, 2)); assert!(Version::new(1, 1) <= Version::new(1, 2)); } }