wallet_adapter_common/
version.rs

1use std::borrow::Cow;
2
3/// The Version of the Wallet Standard currently implemented.
4/// This may be used by the app to determine compatibility and feature detect.
5pub const WALLET_STANDARD_VERSION: &str = "1.0.0";
6
7/// Semver Versioning struct
8#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub struct SemverVersion {
10    major: u8,
11    minor: u8,
12    patch: u8,
13}
14
15impl SemverVersion {
16    /// Instantiate a new [SemverVersion]
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Set the major version
22    pub fn set_major(mut self, major: u8) -> Self {
23        self.major = major;
24
25        self
26    }
27    /// Set the minor version
28    pub fn set_minor(mut self, minor: u8) -> Self {
29        self.minor = minor;
30
31        self
32    }
33
34    /// Set the patch version
35    pub fn set_patch(mut self, patch: u8) -> Self {
36        self.patch = patch;
37
38        self
39    }
40
41    /// The major version
42    pub fn major(&self) -> u8 {
43        self.major
44    }
45
46    /// The minor version
47    pub fn minor(&self) -> u8 {
48        self.minor
49    }
50
51    /// The patch version
52    pub fn patch(&self) -> u8 {
53        self.patch
54    }
55
56    /// Get the string version of [Self] in the format `major.minor.patch`
57    pub fn stringify_version<'a>(&'a self) -> Cow<'a, str> {
58        Cow::Borrowed("")
59            + Cow::Owned(self.major.to_string())
60            + "."
61            + Cow::Owned(self.minor.to_string())
62            + "."
63            + Cow::Owned(self.minor.to_string())
64    }
65}
66
67impl core::fmt::Debug for SemverVersion {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        write!(
70            f,
71            "SemverVersion({}.{}.{})",
72            self.major, self.minor, self.patch
73        )
74    }
75}
76
77impl core::fmt::Display for SemverVersion {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
80    }
81}