multiversx_sc_meta_lib/
version.rs

1use core::fmt;
2use std::cmp::Ordering;
3
4use semver::{BuildMetadata, Prerelease, Version};
5
6#[derive(Debug, Clone, Eq)]
7pub struct FrameworkVersion {
8    pub version: Version,
9}
10
11impl FrameworkVersion {
12    pub const fn new(major: u64, minor: u64, patch: u64) -> Self {
13        let version = Version {
14            major,
15            minor,
16            patch,
17            pre: Prerelease::EMPTY,
18            build: BuildMetadata::EMPTY,
19        };
20
21        FrameworkVersion { version }
22    }
23
24    pub const fn from_triple(triple: (u64, u64, u64)) -> Self {
25        let (major, minor, patch) = triple;
26        FrameworkVersion::new(major, minor, patch)
27    }
28
29    pub fn from_string_template(version_str: &str) -> Self {
30        let version_arr: Vec<&str> = version_str.split('.').collect();
31
32        let major: u64 = version_arr[0].parse().unwrap();
33        let minor: u64 = version_arr[1].parse().unwrap();
34        let patch: u64 = version_arr[2].parse().unwrap();
35
36        FrameworkVersion::new(major, minor, patch)
37    }
38}
39
40impl Ord for FrameworkVersion {
41    fn cmp(&self, other: &Self) -> Ordering {
42        self.version.cmp(&other.version)
43    }
44}
45
46impl PartialOrd for FrameworkVersion {
47    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
48        Some(self.cmp(other))
49    }
50}
51
52impl PartialEq for FrameworkVersion {
53    fn eq(&self, other: &Self) -> bool {
54        self.version == other.version
55    }
56}
57
58impl fmt::Display for FrameworkVersion {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        write!(f, "{}", self.version)
61    }
62}
63
64pub fn is_sorted(versions: &[FrameworkVersion]) -> bool {
65    versions.windows(2).all(|window| window[0] < window[1])
66}
67
68#[macro_export]
69macro_rules! framework_version {
70    ($arg:expr) => {
71        FrameworkVersion::from_triple(multiversx_sc::derive::semver_tuple!($arg))
72    };
73}
74
75#[macro_export]
76macro_rules! framework_versions {
77    ($($arg:expr),+ $(,)?) => {
78        &[$(framework_version!($arg)),+]
79    };
80}