saluki_metadata/
lib.rs

1#[allow(dead_code)]
2mod details {
3    include!(concat!(env!("OUT_DIR"), "/details.rs"));
4}
5
6static VERSION_DETAILS: AppDetails = AppDetails {
7    full_name: details::DETECTED_APP_FULL_NAME,
8    short_name: details::DETECTED_APP_SHORT_NAME,
9    identifier: details::DETECTED_APP_IDENTIFIER,
10    git_hash: details::DETECTED_GIT_HASH,
11    version: Version::new(
12        details::DETECTED_APP_VERSION,
13        details::DETECTED_APP_VERSION_MAJOR,
14        details::DETECTED_APP_VERSION_MINOR,
15        details::DETECTED_APP_VERSION_PATCH,
16    ),
17    build_time: details::DETECTED_APP_BUILD_TIME,
18    dev_build: details::DETECTED_APP_DEV_BUILD,
19    target_arch: details::DETECTED_TARGET_ARCH,
20};
21
22/// Gets the detected details for this application.
23///
24/// This includes basic information like the application name and semantic version, and information that might otherwise
25/// fall under the general umbrella of "build metadata."
26pub fn get_app_details() -> &'static AppDetails {
27    &VERSION_DETAILS
28}
29
30/// Application details.
31///
32/// # Configuration
33///
34/// This struct is generated at build time and contains information about the detected application name and version
35/// based on detected environment variables. The following environment variables are used:
36///
37/// - `APP_FULL_NAME`: Application's full name. If this is not set, the default value is "unknown".
38/// - `APP_SHORT_NAME`: Application's short name. If this is not set, the default value is "unknown".
39/// - `APP_IDENTIFIER`: Application's identifier. If this is not set, the default value is "unknown".
40/// - `APP_VERSION`: Version of the application. If this is not set, the default value is "0.0.0".
41/// - `APP_GIT_HASH`: Git hash of the application. If this is not set, the default value is "unknown".
42/// - `APP_BUILD_TIME`: Build time of the application. If this is not set, the default value is "0000-00-00 00:00:00".
43/// - `APP_DEV_BUILD`: Whether the application is a development build. If this is not set, the default value is `true`.
44/// - `TARGET`: Target architecture of the application. If this is not set, the default value is "unknown-arch".
45///
46/// Environment variables prefixed with `APP_` are expected to be set by the build script/tooling, while others are
47/// provided automatically by Cargo.
48///
49/// The version string will be treated as a semantic version, and will be split on periods to extract the major, minor,
50/// and patch numbers. Additionally, the patch number will be split on hyphens to remove any pre-release or build
51/// metadata.
52pub struct AppDetails {
53    full_name: &'static str,
54    short_name: &'static str,
55    identifier: &'static str,
56    git_hash: &'static str,
57    version: Version,
58    build_time: &'static str,
59    dev_build: bool,
60    target_arch: &'static str,
61}
62
63impl AppDetails {
64    /// Returns the application's full name.
65    ///
66    /// This is typically a human-friendly/"pretty" name of the binary/executable, such as "Agent Data Plane".
67    ///
68    /// If the full name could not be detected, this will return "unknown".
69    pub fn full_name(&self) -> &'static str {
70        self.full_name
71    }
72
73    /// Returns the application's short name.
74    ///
75    /// This is typically a shorter version of the name of the binary/executable, such as "Data Plane" or
76    /// "DATAPLANE".
77    ///
78    /// If the short name could not be detected, this will return "unknown".
79    pub fn short_name(&self) -> &'static str {
80        self.short_name
81    }
82
83    /// Returns the application's identifier.
84    ///
85    /// This is typically a very condensed form of the name of the binary/executable, like an acronym, such as "adp" or
86    /// "ADP".
87    ///
88    /// If the identifier could not be detected, this will return "unknown".
89    pub fn identifier(&self) -> &'static str {
90        self.identifier
91    }
92
93    /// Returns the Git hash used to build the application.
94    ///
95    /// If the Git hash could not be detected, this will return "unknown".
96    pub fn git_hash(&self) -> &'static str {
97        self.git_hash
98    }
99
100    /// Returns the application's version.
101    ///
102    /// If the version could not be detected, this will return a version equivalent to `0.0.0`.
103    pub fn version(&self) -> &Version {
104        &self.version
105    }
106
107    /// Returns the build time of the application.
108    ///
109    /// If the build time could not be detected, this will return "0000-00-00 00:00:00".
110    pub fn build_time(&self) -> &'static str {
111        self.build_time
112    }
113
114    /// Returns `true` if this application is a development build.
115    ///
116    /// Development builds generally encompass all local builds, and any CI builds which are not related to versioned
117    /// artifacts intended for public release.
118    ///
119    /// If the development build flag could not be detected, this will return `true`.
120    pub fn is_dev_build(&self) -> bool {
121        self.dev_build
122    }
123
124    /// Returns the target architecture of the application.
125    ///
126    /// This returns a "target triple", which is a string that generally has _four_ components: the processor
127    /// architecture (x86-64, ARM64, etc), the "vendor" ("apple", "pc", etc), the operating system ("linux", "windows",
128    /// "darwin", etc) and environment/ABI ("gnu", "musl", etc).
129    ///
130    /// The environment/ABI component can sometimes be omitted in scenarios where there are no meaningful distinctions
131    /// for the given operating system.
132    ///
133    /// If the target architecture could not be detected, this will return "unknown-arch".
134    pub fn target_arch(&self) -> &'static str {
135        self.target_arch
136    }
137}
138
139/// A simple representation of a semantic version.
140pub struct Version {
141    raw: &'static str,
142    major: u32,
143    minor: u32,
144    patch: u32,
145}
146
147impl Version {
148    const fn new(raw: &'static str, major: u32, minor: u32, patch: u32) -> Self {
149        Self {
150            raw,
151            major,
152            minor,
153            patch,
154        }
155    }
156
157    /// Returns the raw version string.
158    pub fn raw(&self) -> &'static str {
159        self.raw
160    }
161
162    /// Returns the major version number.
163    ///
164    /// If the major version number is not present in the version string, this will return `0`.
165    pub fn major(&self) -> u32 {
166        self.major
167    }
168
169    /// Returns the minor version number.
170    ///
171    /// If the minor version number is not present in the version string, this will return `0`.
172    pub fn minor(&self) -> u32 {
173        self.minor
174    }
175
176    /// Returns the patch version number.
177    ///
178    /// If the patch version number is not present in the version string, this will return `0`.
179    pub fn patch(&self) -> u32 {
180        self.patch
181    }
182}