abstract_os/objects/
dependency.rs

1//! Dependency definitions for Abstract Modules
2use cw_semver::{Comparator, Version};
3use serde::{Deserialize, Serialize};
4
5use super::module::ModuleId;
6
7/// Statically defined dependency used in-contract
8#[derive(Debug, Clone, PartialEq)]
9pub struct StaticDependency {
10    pub id: ModuleId<'static>,
11    pub version_req: &'static [&'static str],
12}
13
14impl StaticDependency {
15    pub const fn new(
16        module_id: ModuleId<'static>,
17        version_requirement: &'static [&'static str],
18    ) -> Self {
19        Self {
20            id: module_id,
21            version_req: version_requirement,
22        }
23    }
24
25    /// Iterate through the (statically provided) version requirements and ensure that they are valid.
26    pub fn check(&self) -> Result<(), cw_semver::Error> {
27        for req in self.version_req {
28            Comparator::parse(req)?;
29        }
30        Ok(())
31    }
32
33    /// Iterates through the version requirements and checks that the provided **version** is compatible.
34    pub fn matches(&self, version: &Version) -> bool {
35        self.version_req
36            .iter()
37            .all(|req| Comparator::parse(req).unwrap().matches(version))
38    }
39}
40
41/// On-chain stored version of the module dependencies. Retrievable though raw-queries.
42#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
43pub struct Dependency {
44    pub id: String,
45    pub version_req: Vec<Comparator>,
46}
47
48impl From<&StaticDependency> for Dependency {
49    fn from(dep: &StaticDependency) -> Self {
50        Self {
51            id: dep.id.to_string(),
52            version_req: dep.version_req.iter().map(|s| s.parse().unwrap()).collect(),
53        }
54    }
55}
56
57#[cfg(test)]
58mod test {
59    use super::*;
60    use speculoos::prelude::*;
61    use std::borrow::Borrow;
62
63    #[test]
64    fn test_static_constructor() {
65        const VERSION_CONSTRAINT: [&str; 1] = ["^1.0.0"];
66
67        let dep = StaticDependency::new("test", &VERSION_CONSTRAINT);
68
69        assert_that!(dep.id).is_equal_to("test");
70        assert_that!(&dep.version_req.to_vec()).is_equal_to(VERSION_CONSTRAINT.to_vec());
71    }
72
73    #[test]
74    fn static_check_passes() {
75        const VERSION_CONSTRAINT: [&str; 1] = ["^1.0.0"];
76
77        let dep = StaticDependency::new("test", &VERSION_CONSTRAINT);
78
79        assert_that!(dep.check()).is_ok();
80    }
81
82    #[test]
83    fn static_check_fails() {
84        const VERSION_CONSTRAINT: [&str; 1] = ["^1e.0"];
85
86        let dep = StaticDependency::new("test", &VERSION_CONSTRAINT);
87
88        assert_that!(dep.check()).is_err();
89    }
90
91    #[test]
92    fn matches_should_match_matching_versions() {
93        const VERSION_CONSTRAINT: [&str; 1] = ["^1.0.0"];
94
95        let dep = StaticDependency::new("test", &VERSION_CONSTRAINT);
96
97        assert_that!(dep.matches(&Version::parse("1.0.0").unwrap())).is_true();
98        assert_that!(dep.matches(&Version::parse("1.1.0").unwrap())).is_true();
99        assert_that!(dep.matches(&Version::parse("1.1.1").unwrap())).is_true();
100    }
101
102    #[test]
103    fn matches_should_not_match_non_matching_versions() {
104        const VERSION_CONSTRAINT: [&str; 1] = ["^1.0.0"];
105
106        let dep = StaticDependency::new("test", &VERSION_CONSTRAINT);
107
108        assert_that!(dep.matches(&Version::parse("2.0.0").unwrap())).is_false();
109        assert_that!(dep.matches(&Version::parse("0.1.0").unwrap())).is_false();
110        assert_that!(dep.matches(&Version::parse("0.1.1").unwrap())).is_false();
111    }
112
113    #[test]
114    fn test_dependency_from_static() {
115        const VERSION_CONSTRAINT: [&str; 1] = ["^1.0.0"];
116
117        let dep = StaticDependency::new("test", &VERSION_CONSTRAINT);
118
119        let dep: Dependency = dep.borrow().into();
120
121        assert_that!(dep.id).is_equal_to("test".to_string());
122        assert_that!(dep.version_req).is_equal_to(vec![Comparator::parse("^1.0.0").unwrap()]);
123    }
124}