multiversx_sc_meta_lib/cargo_toml/
version_req.rs

1use crate::{
2    version::FrameworkVersion,
3    version_history::{find_version_by_str, LAST_VERSION},
4};
5
6/// Crate version requirements, as expressed in Cargo.toml. A very crude version.
7///
8/// TODO: replace with semver::VersionReq at some point.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct VersionReq {
11    pub semver: FrameworkVersion,
12    pub is_strict: bool,
13}
14impl VersionReq {
15    pub fn from_version_str(raw: &str) -> Option<Self> {
16        if let Some(stripped_version) = raw.strip_prefix('=') {
17            Some(VersionReq {
18                semver: find_version_by_str(stripped_version)?.clone(),
19                is_strict: true,
20            })
21        } else {
22            Some(VersionReq {
23                semver: find_version_by_str(raw)?.clone(),
24                is_strict: false,
25            })
26        }
27    }
28
29    pub fn from_version_str_or_latest(raw: &str) -> Self {
30        if let Some(stripped_version) = raw.strip_prefix('=') {
31            VersionReq {
32                semver: find_version_by_str(stripped_version)
33                    .unwrap_or(&LAST_VERSION)
34                    .clone(),
35                is_strict: true,
36            }
37        } else {
38            VersionReq {
39                semver: find_version_by_str(raw).unwrap_or(&LAST_VERSION).clone(),
40                is_strict: false,
41            }
42        }
43    }
44
45    pub fn strict(self) -> Self {
46        Self {
47            semver: self.semver,
48            is_strict: true,
49        }
50    }
51
52    pub fn into_string(self) -> String {
53        if self.is_strict {
54            format!("={}", self.semver)
55        } else {
56            self.semver.to_string()
57        }
58    }
59}