Skip to main content

vs_core/service/
version.rs

1//! Services for reporting version and build metadata.
2
3use crate::{App, CoreError, VersionInfo};
4
5const BUILD_TARGET_TRIPLE: &str = env!("VS_BUILD_TARGET");
6const BUILD_RELEASE_VARIANT: &str = env!("VS_BUILD_VARIANT");
7const BUILD_RELEASE_ARCHIVE_EXT: &str = env!("VS_BUILD_ARCHIVE_EXT");
8
9impl App {
10    /// Returns version and build metadata for the current binary.
11    pub fn version_info(&self) -> Result<VersionInfo, CoreError> {
12        Ok(VersionInfo {
13            current_version: format!("v{}", env!("CARGO_PKG_VERSION")),
14            build_target: release_target_triple().to_string(),
15            build_variant: release_feature_label()?.to_string(),
16            archive_extension: release_archive_extension().to_string(),
17        })
18    }
19}
20
21pub(crate) fn release_target_triple() -> &'static str {
22    BUILD_TARGET_TRIPLE
23}
24
25pub(crate) fn release_archive_extension() -> &'static str {
26    BUILD_RELEASE_ARCHIVE_EXT
27}
28
29pub(crate) fn release_feature_label() -> Result<&'static str, CoreError> {
30    match BUILD_RELEASE_VARIANT {
31        "full" => Ok("full"),
32        "lua" => Ok("lua"),
33        "wasi" => Ok("wasi"),
34        "bare" => Err(CoreError::Unsupported(String::from(
35            "self-upgrade is unavailable for bare builds because releases only publish lua, wasi, and full binaries",
36        ))),
37        other => Err(CoreError::Unsupported(format!(
38            "self-upgrade was built with an unknown release variant: {other}"
39        ))),
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::{
46        BUILD_RELEASE_VARIANT, BUILD_TARGET_TRIPLE, release_feature_label, release_target_triple,
47    };
48
49    #[test]
50    fn build_metadata_should_be_injected_at_compile_time() {
51        assert_eq!(release_target_triple(), BUILD_TARGET_TRIPLE);
52        match BUILD_RELEASE_VARIANT {
53            "full" | "lua" | "wasi" | "bare" => {}
54            other => panic!("unexpected build variant {other}"),
55        }
56    }
57
58    #[test]
59    fn build_variant_should_map_to_supported_labels() {
60        let label = release_feature_label();
61        match BUILD_RELEASE_VARIANT {
62            "bare" => assert!(label.is_err()),
63            _ => assert!(label.is_ok()),
64        }
65    }
66}