rust_analyzer_proc_macro_srv/
version.rs

1//! Code for representing rust-analyzer's release version number.
2#![expect(dead_code)]
3
4use std::fmt;
5
6/// Information about the git repository where rust-analyzer was built from.
7pub(crate) struct CommitInfo {
8    pub(crate) short_commit_hash: &'static str,
9    pub(crate) commit_hash: &'static str,
10    pub(crate) commit_date: &'static str,
11}
12
13/// Cargo's version.
14pub(crate) struct VersionInfo {
15    /// rust-analyzer's version, such as "1.57.0", "1.58.0-beta.1", "1.59.0-nightly", etc.
16    pub(crate) version: &'static str,
17    /// The release channel we were built for (stable/beta/nightly/dev).
18    ///
19    /// `None` if not built via bootstrap.
20    pub(crate) release_channel: Option<&'static str>,
21    /// Information about the Git repository we may have been built from.
22    ///
23    /// `None` if not built from a git repo.
24    pub(crate) commit_info: Option<CommitInfo>,
25}
26
27impl fmt::Display for VersionInfo {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(f, "{}", self.version)?;
30
31        if let Some(ci) = &self.commit_info {
32            write!(f, " ({} {})", ci.short_commit_hash, ci.commit_date)?;
33        };
34        Ok(())
35    }
36}
37
38/// Returns information about cargo's version.
39pub(crate) const fn version() -> VersionInfo {
40    let version = match option_env!("CFG_RELEASE") {
41        Some(x) => x,
42        None => "0.0.0",
43    };
44
45    let release_channel = option_env!("CFG_RELEASE_CHANNEL");
46    let commit_info = match (
47        option_env!("RA_COMMIT_SHORT_HASH"),
48        option_env!("RA_COMMIT_HASH"),
49        option_env!("RA_COMMIT_DATE"),
50    ) {
51        (Some(short_commit_hash), Some(commit_hash), Some(commit_date)) => {
52            Some(CommitInfo { short_commit_hash, commit_hash, commit_date })
53        }
54        _ => None,
55    };
56
57    VersionInfo { version, release_channel, commit_info }
58}