diff options
Diffstat (limited to 'src/build_info.rs')
| -rw-r--r-- | src/build_info.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/build_info.rs b/src/build_info.rs new file mode 100644 index 00000000..1fdedfbb --- /dev/null +++ b/src/build_info.rs @@ -0,0 +1,44 @@ +//! Build identity helpers. + +pub const BASE_VERSION: &str = env!("CARGO_PKG_VERSION"); + +pub fn channel() -> &'static str { + non_empty(option_env!("HERDR_BUILD_CHANNEL")).unwrap_or("stable") +} + +pub fn build_id() -> Option<&'static str> { + non_empty(option_env!("HERDR_BUILD_ID")) +} + +pub fn version() -> String { + match channel() { + "stable" => BASE_VERSION.to_string(), + channel => match build_id() { + Some(build_id) => format!("{BASE_VERSION}-{channel}.{build_id}"), + None => format!("{BASE_VERSION}-{channel}"), + }, + } +} + +pub fn is_preview() -> bool { + channel() == "preview" +} + +fn non_empty(value: Option<&'static str>) -> Option<&'static str> { + value.and_then(|value| { + let trimmed = value.trim(); + if trimmed.is_empty() { + None + } else { + Some(trimmed) + } + }) +} + +#[cfg(test)] +mod tests { + #[test] + fn stable_version_defaults_to_cargo_version() { + assert!(!super::version().is_empty()); + } +} |
