From 89d36281e31691cf25905eeb6452eff41538cdee Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 9 Apr 2023 19:14:16 -0400 Subject: [PATCH 1/3] Added some testing for version comparison and implemented PartialEq for Version manually --- src/package/dependency.rs | 7 ++++++ src/version/gitrev.rs | 22 +++++++++++++++++ src/version/mod.rs | 49 +++++++++++++++++++++++++++++++++++++- src/version/semver.rs | 14 +++++++++++ test/2.tar | Bin 10240 -> 10240 bytes 5 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/package/dependency.rs b/src/package/dependency.rs index 64cbc29..a9e99a8 100644 --- a/src/package/dependency.rs +++ b/src/package/dependency.rs @@ -5,13 +5,20 @@ use { }; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +/// Specifies a dependency requirement pub struct Dependency { + /// The name of the dependency. All packages must have a unique name. 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, Option), } impl Dependency { #[allow(clippy::must_use_candidate)] + /// Checks whether a package satisfies a given dependency pub fn satisfied(&self, package: &Package) -> bool { if self.name.as_str() == package.name.as_str() { match &self.version { diff --git a/src/version/gitrev.rs b/src/version/gitrev.rs index 4b6ef38..b40331f 100644 --- a/src/version/gitrev.rs +++ b/src/version/gitrev.rs @@ -6,6 +6,7 @@ use { }; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +/// Represents a Git revision pub struct GitRev { /// the short revision hash pub hash: String, @@ -77,3 +78,24 @@ impl TryFrom 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); + } +} diff --git a/src/version/mod.rs b/src/version/mod.rs index 06b8057..903b117 100644 --- a/src/version/mod.rs +++ b/src/version/mod.rs @@ -9,11 +9,25 @@ mod 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 { + /// A single replease number, as in Firefox 102 Number(u32), + /// Rapid versioning consists of two numbers separated by + /// a dot (.) character, and is used notably by Gnome 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), + /// 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), } @@ -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)] pub struct ParseVersionError; @@ -127,4 +160,18 @@ mod test { let rpd = "1.42".parse::().unwrap(); 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); + } } diff --git a/src/version/semver.rs b/src/version/semver.rs index cc9d3da..89758ec 100644 --- a/src/version/semver.rs +++ b/src/version/semver.rs @@ -188,4 +188,18 @@ mod test { }) ); } + + #[test] + fn cmp_semver_num_gt() { + let sem = "42.69.0".parse::().unwrap(); + let num = 42; + assert!(sem > num); + } + + #[test] + fn cmp_semver_num_eq() { + let sem = "42.0.0".parse::().unwrap(); + let num = 42; + assert_eq!(sem, num); + } } diff --git a/test/2.tar b/test/2.tar index 7593697876d7836908460e72aaad1e8d22c31bbd..98902ce9c96775fa2ed4c0b8194684c56b3bd9ca 100644 GIT binary patch delta 51 zcmZn&Xb70lBWPq|U~FV)#$aG*U~Xp2pkOdLkuim@w74X(NI`)iFR>&eF>j)v*XBsZ GU}9p*pkOdLkuim*w74X(NI`)iFR>(bqM*;_NXF%A E08nNP_5c6? From 43d8c82c08512aef84014cb4e4a730667b5a6bfa Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Mon, 10 Apr 2023 18:41:23 -0400 Subject: [PATCH 2/3] Remove appstream from Specs and Package structs --- src/package/mod.rs | 5 ----- src/package/specs.rs | 4 ---- 2 files changed, 9 deletions(-) diff --git a/src/package/mod.rs b/src/package/mod.rs index 0020cd9..bcef93f 100644 --- a/src/package/mod.rs +++ b/src/package/mod.rs @@ -45,10 +45,6 @@ pub struct Package { pub description: String, /// a more verbose description of the package pub long_description: String, - /// an optional link to an - /// [AppStream](https://www.freedesktop.org/wiki/Distributions/AppStream/) - /// metadata file - pub appstream_data: Option, /// a listing of all files, directories and symlinks which are a part of /// this package pub plist: Plist, @@ -72,7 +68,6 @@ impl From for Package { release: value.release, description: value.description, long_description: value.long_description, - appstream_data: value.appstream_data, dependencies: value.dependencies, users: value.users, groups: value.groups, diff --git a/src/package/specs.rs b/src/package/specs.rs index 1cfcc83..abe182b 100644 --- a/src/package/specs.rs +++ b/src/package/specs.rs @@ -16,10 +16,6 @@ pub struct Specs { pub description: String, /// a more verbose description of the package pub long_description: String, - /// an optional link to an - /// [AppStream](https://www.freedesktop.org/wiki/Distributions/AppStream/) - /// metadata file - pub appstream_data: Option, /// all of this package's runtime dependencies pub dependencies: Vec, /// an optional list of users to be created upon installation From 00683722d64a9c2f619e6c5378ab7b571b32ecc8 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Mon, 10 Apr 2023 19:17:26 -0400 Subject: [PATCH 3/3] Make Archive::remove parallel; Add Archive::pop method; --- src/tar/mod.rs | 15 +++++++++++++-- test/2.tar | Bin 10240 -> 10240 bytes 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/tar/mod.rs b/src/tar/mod.rs index 004b94e..f94798d 100644 --- a/src/tar/mod.rs +++ b/src/tar/mod.rs @@ -4,7 +4,7 @@ use std::{ path::PathBuf, }; -use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; +use rayon::prelude::{IntoParallelRefIterator, ParallelIterator, IndexedParallelIterator}; mod error; mod header; @@ -143,7 +143,7 @@ impl Archive { pub fn remove(&mut self, filename: &str) -> Result { let mut name = [0u8; 100]; name[..filename.len()].copy_from_slice(filename.as_bytes()); - if let Some(i) = &self.nodes.iter().position(|x| x.header.fname == name) { + if let Some(i) = &self.nodes.par_iter().position_any(|x| x.header.fname == name) { self.nodes.remove(*i); return Ok(true); } @@ -166,6 +166,17 @@ impl Archive { x.header.file_path() == Ok(PathBuf::from(filename)) }).cloned() } + + pub fn pop(&mut self, filename: &str) -> Option { + if let Some(i) = self.nodes.par_iter().position_any(|x| { + x.header.file_path() == Ok(PathBuf::from(filename)) + }) { + let node = self.nodes.get(i).cloned(); + self.nodes.remove(i); + return node; + } + None + } } #[cfg(test)] diff --git a/test/2.tar b/test/2.tar index 98902ce9c96775fa2ed4c0b8194684c56b3bd9ca..d37f5f88b97f1e407fb36b86a6fbcba352351702 100644 GIT binary patch delta 33 ocmZn&Xb70lBWP-9XkcJs#$aG*U~Xo_pkOdLkuha6BjbNH0Ge+I;s5{u delta 33 ocmZn&Xb70lBWPq|U~FV)#$aG*U~Xp2pkOdLkuha6BjbNH0Gg8s