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