create version module

This commit is contained in:
Takayuki Maeda 2021-09-06 22:29:18 +09:00
parent 83b3e21997
commit 919d0753f0
2 changed files with 51 additions and 0 deletions

View File

@ -6,6 +6,7 @@ mod config;
mod database; mod database;
mod event; mod event;
mod ui; mod ui;
mod version;
#[macro_use] #[macro_use]
mod log; mod log;

50
src/version.rs Normal file
View File

@ -0,0 +1,50 @@
use std::{env, fmt};
#[derive(Default)]
pub struct Version {
major: u32,
minor: u32,
patch: u32,
pre: Option<String>,
}
impl Version {
/// read version at compile time from env variables
pub fn new() -> Self {
let mut res = Self::default();
let major_str = env!("CARGO_PKG_VERSION_MAJOR");
if let Ok(major) = major_str.parse::<u32>() {
res.major = major;
}
let minor_str = env!("CARGO_PKG_VERSION_MINOR");
if let Ok(minor) = minor_str.parse::<u32>() {
res.minor = minor;
}
let patch_str = env!("CARGO_PKG_VERSION_PATCH");
if let Ok(patch) = patch_str.parse::<u32>() {
res.patch = patch;
}
let pre_str = env!("CARGO_PKG_VERSION_PRE");
res.pre = if pre_str.is_empty() {
None
} else {
Some(pre_str.to_string())
};
res
}
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"v{}.{}.{}{}",
self.major,
self.minor,
self.patch,
self.pre
.as_ref()
.map_or(String::new(), |pre| format!("-{}", pre.to_string()))
)
}
}