Added some testing for version comparison and implemented PartialEq for
Version manually
This commit is contained in:
parent
407d12a711
commit
89d36281e3
@ -5,13 +5,20 @@ use {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
/// Specifies a dependency requirement
|
||||||
pub struct Dependency {
|
pub struct Dependency {
|
||||||
|
/// The name of the dependency. All packages must have a unique name.
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
/// The version requirements for this dependency. If the low
|
||||||
|
/// version is `Some`, then the version must be equal to or
|
||||||
|
/// greater than this version. If the high version is `Some`,
|
||||||
|
/// then the version must be less than this version.
|
||||||
pub version: (Option<Version>, Option<Version>),
|
pub version: (Option<Version>, Option<Version>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Dependency {
|
impl Dependency {
|
||||||
#[allow(clippy::must_use_candidate)]
|
#[allow(clippy::must_use_candidate)]
|
||||||
|
/// Checks whether a package satisfies a given dependency
|
||||||
pub fn satisfied(&self, package: &Package) -> bool {
|
pub fn satisfied(&self, package: &Package) -> bool {
|
||||||
if self.name.as_str() == package.name.as_str() {
|
if self.name.as_str() == package.name.as_str() {
|
||||||
match &self.version {
|
match &self.version {
|
||||||
|
@ -6,6 +6,7 @@ use {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
/// Represents a Git revision
|
||||||
pub struct GitRev {
|
pub struct GitRev {
|
||||||
/// the short revision hash
|
/// the short revision hash
|
||||||
pub hash: String,
|
pub hash: String,
|
||||||
@ -77,3 +78,24 @@ impl TryFrom<Version> for GitRev {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use std::{thread, time::Duration};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ord() {
|
||||||
|
let a = GitRev {
|
||||||
|
hash: "aaab".to_string(),
|
||||||
|
datetime: Utc::now(),
|
||||||
|
};
|
||||||
|
thread::sleep(Duration::from_millis(10));
|
||||||
|
let b = GitRev {
|
||||||
|
hash: "aaaa".to_string(),
|
||||||
|
datetime: Utc::now(),
|
||||||
|
};
|
||||||
|
assert!(a < b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -9,11 +9,25 @@ mod semver;
|
|||||||
|
|
||||||
pub use {gitrev::GitRev, rapid::Rapid, semver::SemVer};
|
pub use {gitrev::GitRev, rapid::Rapid, semver::SemVer};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
/// An enum representing the most common versioning schemes.
|
||||||
|
/// Each scheme must implement Eq and Ord with itself, and
|
||||||
|
/// may optionally implement PartialEq and PartialOrd with
|
||||||
|
/// other schemes. Number, Rapid, and SemVer do this, while
|
||||||
|
/// Git can only compare with itself.
|
||||||
pub enum Version {
|
pub enum Version {
|
||||||
|
/// A single replease number, as in Firefox 102
|
||||||
Number(u32),
|
Number(u32),
|
||||||
|
/// Rapid versioning consists of two numbers separated by
|
||||||
|
/// a dot (.) character, and is used notably by Gnome
|
||||||
Rapid(Rapid),
|
Rapid(Rapid),
|
||||||
|
/// SemVer is the most common versioning scheme and consists
|
||||||
|
/// of three dot (.) separated numbers. The numbers are major,
|
||||||
|
/// minor, and patch respctively.
|
||||||
SemVer(SemVer),
|
SemVer(SemVer),
|
||||||
|
/// A git revision. Use of this scheme is to be avoided in
|
||||||
|
/// official packaging as it cannot be readily compared with
|
||||||
|
/// other versioning schemes for dependency resolution.
|
||||||
Git(GitRev),
|
Git(GitRev),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,6 +95,25 @@ impl PartialOrd for Version {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Version {
|
||||||
|
#[allow(clippy::many_single_char_names)]
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
match (self, other) {
|
||||||
|
(Self::Number(s), Self::Number(o)) => s.eq(o),
|
||||||
|
(Self::Number(s), Self::Rapid(o)) => s.eq(o),
|
||||||
|
(Self::Number(s), Self::SemVer(o)) => s.eq(o),
|
||||||
|
(Self::Rapid(s), Self::Number(o)) => s.eq(o),
|
||||||
|
(Self::Rapid(s), Self::Rapid(o)) => s.eq(o),
|
||||||
|
(Self::Rapid(s), Self::SemVer(o)) => s.eq(o),
|
||||||
|
(Self::SemVer(s), Self::Number(o)) => s.eq(o),
|
||||||
|
(Self::SemVer(s), Self::Rapid(o)) => s.eq(o),
|
||||||
|
(Self::SemVer(s), Self::SemVer(o)) => s.eq(o),
|
||||||
|
(Self::Git(s), Self::Git(o)) => s.eq(o),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ParseVersionError;
|
pub struct ParseVersionError;
|
||||||
|
|
||||||
@ -127,4 +160,18 @@ mod test {
|
|||||||
let rpd = "1.42".parse::<Rapid>().unwrap();
|
let rpd = "1.42".parse::<Rapid>().unwrap();
|
||||||
assert!(sem == rpd);
|
assert!(sem == rpd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cmp_semver_num_gt() {
|
||||||
|
let sem = Version::SemVer("42.69.0".parse().unwrap());
|
||||||
|
let num = Version::Number(42);
|
||||||
|
assert!(sem > num);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cmp_semver_num_eq() {
|
||||||
|
let sem = Version::SemVer("42.0.0".parse().unwrap());
|
||||||
|
let num = Version::Number(42);
|
||||||
|
assert_eq!(sem, num);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,4 +188,18 @@ mod test {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cmp_semver_num_gt() {
|
||||||
|
let sem = "42.69.0".parse::<SemVer>().unwrap();
|
||||||
|
let num = 42;
|
||||||
|
assert!(sem > num);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cmp_semver_num_eq() {
|
||||||
|
let sem = "42.0.0".parse::<SemVer>().unwrap();
|
||||||
|
let num = 42;
|
||||||
|
assert_eq!(sem, num);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
BIN
test/2.tar
BIN
test/2.tar
Binary file not shown.
Loading…
Reference in New Issue
Block a user