common: adding a version-checker crate

This commit is contained in:
Dave Hrycyszyn
2020-01-22 16:17:35 +00:00
parent d34907eccf
commit 44116e4907
4 changed files with 72 additions and 0 deletions
Generated
+8
View File
@@ -2422,6 +2422,7 @@ dependencies = [
"rand 0.7.2",
"serde",
"sphinx",
"version-checker",
]
[[package]]
@@ -2557,6 +2558,13 @@ version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
[[package]]
name = "version-checker"
version = "0.1.0"
dependencies = [
"semver",
]
[[package]]
name = "version_check"
version = "0.1.5"
+3
View File
@@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock
+10
View File
@@ -0,0 +1,10 @@
[package]
name = "version-checker"
version = "0.1.0"
authors = ["Dave Hrycyszyn <futurechimp@users.noreply.github.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
semver = "0.9.0"
+51
View File
@@ -0,0 +1,51 @@
use semver::Version;
use semver::VersionReq;
/// Checks whether given `version` is compatible with a given semantic
/// version requirement `req` according to major-minor semver rules.
/// The semantic version requirement can be passed as a full, concrete version
/// number, because that's what we'll have in our Cargo.toml files (e.g. 0.3.2).
/// The patch number in the requirement will be dropped and replaced with a wildcard (0.3.*) as all minor versions should be compatible with each other.
pub fn is_compatible(version: &str, req: &str) -> bool {
let version = Version::parse(version).unwrap();
let tmp = Version::parse(req).unwrap();
let wildcard = format!("{}.{}.*", tmp.major, tmp.minor).to_string();
let semver_requirement = VersionReq::parse(&wildcard).unwrap();
semver_requirement.matches(&version)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn version_0_3_0_is_compatible_with_requirement_0_3_x() {
assert!(is_compatible("0.3.0", "0.3.2"));
}
#[test]
fn version_0_3_1_is_compatible_with_minimum_requirement_0_3_x() {
assert!(is_compatible("0.3.1", "0.3.2"));
}
#[test]
fn version_0_3_2_is_compatible_with_minimum_requirement_0_3_x() {
assert!(is_compatible("0.3.2", "0.3.0"));
}
#[test]
fn version_0_2_0_is_not_compatible_with_requirement_0_3_x() {
assert!(!is_compatible("0.2.0", "0.3.2"));
}
#[test]
fn version_0_4_0_is_not_compatible_with_requirement_0_3_x() {
assert!(!is_compatible("0.4.0", "0.3.2"));
}
#[test]
fn version_1_3_2_is_not_compatible_with_requirement_0_3_x() {
assert!(!is_compatible("1.3.2", "0.3.2"));
}
}