solana_version/
legacy.rs

1use {
2    crate::compute_commit,
3    serde_derive::{Deserialize, Serialize},
4    solana_sanitize::Sanitize,
5    std::{convert::TryInto, fmt},
6};
7
8// Older version structure used earlier 1.3.x releases
9#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
10#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
11pub struct LegacyVersion1 {
12    major: u16,
13    minor: u16,
14    patch: u16,
15    commit: Option<u32>, // first 4 bytes of the sha1 commit hash
16}
17
18impl Sanitize for LegacyVersion1 {}
19
20#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
21#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
22pub struct LegacyVersion2 {
23    pub major: u16,
24    pub minor: u16,
25    pub patch: u16,
26    pub commit: Option<u32>, // first 4 bytes of the sha1 commit hash
27    pub feature_set: u32,    // first 4 bytes of the FeatureSet identifier
28}
29
30impl From<LegacyVersion1> for LegacyVersion2 {
31    fn from(legacy_version: LegacyVersion1) -> Self {
32        Self {
33            major: legacy_version.major,
34            minor: legacy_version.minor,
35            patch: legacy_version.patch,
36            commit: legacy_version.commit,
37            feature_set: 0,
38        }
39    }
40}
41
42impl Default for LegacyVersion2 {
43    fn default() -> Self {
44        let feature_set =
45            u32::from_le_bytes(solana_feature_set::ID.as_ref()[..4].try_into().unwrap());
46        Self {
47            major: env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(),
48            minor: env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(),
49            patch: env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(),
50            commit: compute_commit(option_env!("CI_COMMIT")),
51            feature_set,
52        }
53    }
54}
55
56impl fmt::Display for LegacyVersion2 {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        write!(f, "{}.{}.{}", self.major, self.minor, self.patch,)
59    }
60}
61
62impl fmt::Debug for LegacyVersion2 {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        write!(
65            f,
66            "{}.{}.{} (src:{}; feat:{})",
67            self.major,
68            self.minor,
69            self.patch,
70            match self.commit {
71                None => "devbuild".to_string(),
72                Some(commit) => format!("{commit:08x}"),
73            },
74            self.feature_set,
75        )
76    }
77}
78
79impl Sanitize for LegacyVersion2 {}