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