2020-05-11 15:02:01 -07:00
|
|
|
extern crate serde_derive;
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
use solana_sdk::sanitize::Sanitize;
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
|
|
|
pub struct Version {
|
|
|
|
major: u16,
|
|
|
|
minor: u16,
|
|
|
|
patch: u16,
|
|
|
|
commit: Option<u32>, // first 4 bytes of the sha1 commit hash
|
|
|
|
}
|
|
|
|
|
2020-05-11 22:49:29 -07:00
|
|
|
fn compute_commit(sha1: Option<&'static str>) -> Option<u32> {
|
|
|
|
let sha1 = sha1?;
|
|
|
|
if sha1.len() < 8 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
u32::from_str_radix(&sha1[..8], 16).ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 15:02:01 -07:00
|
|
|
impl Default for Version {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
major: env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(),
|
|
|
|
minor: env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(),
|
|
|
|
patch: env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(),
|
2020-05-11 22:49:29 -07:00
|
|
|
commit: compute_commit(option_env!("CI_COMMIT")),
|
2020-05-11 15:02:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Version {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}.{}.{} {}",
|
|
|
|
self.major,
|
|
|
|
self.minor,
|
|
|
|
self.patch,
|
|
|
|
match self.commit {
|
|
|
|
None => "devbuild".to_string(),
|
|
|
|
Some(commit) => format!("{:08x}", commit),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sanitize for Version {}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! version {
|
|
|
|
() => {
|
|
|
|
&*format!("{}", $crate::Version::default())
|
|
|
|
};
|
|
|
|
}
|
2020-05-11 22:49:29 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_compute_commit() {
|
|
|
|
assert_eq!(compute_commit(None), None);
|
2020-05-15 09:35:43 -07:00
|
|
|
assert_eq!(compute_commit(Some("1234567890")), Some(0x1234_5678));
|
2020-05-11 22:49:29 -07:00
|
|
|
assert_eq!(compute_commit(Some("HEAD")), None);
|
|
|
|
assert_eq!(compute_commit(Some("garbagein")), None);
|
|
|
|
}
|
|
|
|
}
|