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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
use std::fmt; use std::process::Command; pub const CURRENT_STABLE: Version = Version { major: 1, minor: 20, patch: 0, }; pub const CURRENT_BETA: Version = Version { major: 1, minor: 21, patch: 0, }; pub const CURRENT_NIGHTLY: Version = Version { major: 1, minor: 22, patch: 0, }; #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub struct Version { pub major: u32, pub minor: u32, pub patch: u32, } impl fmt::Display for Version { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "{}.{}.{}", self.major, self.minor, self.patch) } } impl Version { pub fn new(major: u32, minor: u32, patch: u32) -> Version { Version { major, minor, patch, } } pub fn from_env_var() -> Option<Version> { option_env!("CFG_VERSION").and_then(|s| Self::from_string(&s)) } fn from_string(version_str: &str) -> Option<Version> { use std::str::FromStr; macro_rules! try_opt { ($x: expr) => { match $x { Some(Ok(x)) => x, _ => return None, } } } let mut nums = version_str.split('.').map(u32::from_str); Some(Version::new(try_opt!(nums.next()), try_opt!(nums.next()), try_opt!(nums.next()))) } pub fn to_unstable_tool_string(&self) -> String { format!("0.{}{}.{}", self.major, self.minor, self.patch) } pub fn to_stable_tool_string(&self) -> String { format!("{}{}.{}.0", self.major, self.minor, self.patch) } } pub fn channel() -> Option<&'static str> { option_env!("CFG_RELEASE_CHANNEL") } pub fn commit_hash() -> Option<String> { Command::new("git") .args(&["rev-parse", "--short", "HEAD"]) .output() .ok() .and_then(|r| String::from_utf8(r.stdout).ok()) } pub fn commit_date() -> Option<String> { Command::new("git") .args(&["log", "-1", "--date=short", "--pretty=format:%cd"]) .output() .ok() .and_then(|r| String::from_utf8(r.stdout).ok()) } #[cfg(test)] mod tests { use super::*; #[test] fn test_formatting() { let v = Version::new(1, 2, 3); assert_eq!(&v.to_string(), "1.2.3"); assert_eq!(&v.to_unstable_tool_string(), "0.12.3"); assert_eq!(&v.to_stable_tool_string(), "12.3.0"); } #[test] fn test_from_str() { assert_eq!(Version::from_string("0.0"), None); assert_eq!(Version::from_string("0.0.a"), None); assert_eq!(Version::from_string("0.."), None); assert_eq!(Version::from_string("0.0.0"), Some(Version::new(0, 0, 0))); assert_eq!(Version::from_string("3.2.1"), Some(Version::new(3, 2, 1))); } }