From d210c44905edc4e5149aa78518a856584e9aed7e Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Tue, 21 Mar 2023 14:29:27 -0400 Subject: [PATCH] Fleshed out some data structures: - Package - Version - Dependency --- Cargo.toml | 5 +++++ src/lib.rs | 9 ++++++++- src/package/dependency.rs | 30 ++++++++++++++++++++++++++++ src/package/mod.rs | 16 +++++++++++++++ src/version/gitrev.rs | 21 ++++++++++++++++++++ src/version/mod.rs | 24 +++++++++++++++++++++++ src/version/semver.rs | 41 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 src/package/dependency.rs create mode 100644 src/package/mod.rs create mode 100644 src/version/gitrev.rs create mode 100644 src/version/mod.rs create mode 100644 src/version/semver.rs diff --git a/Cargo.toml b/Cargo.toml index 47c88d6..6fce19e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,8 @@ sha2 = "0.10" [dependencies.serde] version = "1.0" features = ["derive"] + +[profile.release] +codegen-units = 1 +lto = true +strip = true diff --git a/src/lib.rs b/src/lib.rs index 084d24f..cd6305a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,9 @@ +#![warn(clippy::all, clippy::pedantic)] +mod package; mod plist; -pub use plist::*; +mod version; +pub use { + package::{Dependency, Package}, + plist::*, + version::*, +}; diff --git a/src/package/dependency.rs b/src/package/dependency.rs new file mode 100644 index 0000000..0bcffde --- /dev/null +++ b/src/package/dependency.rs @@ -0,0 +1,30 @@ +use { + crate::Version, + serde::{Serialize, Deserialize}, + super::Package, +}; + +#[derive(Debug, Serialize, Deserialize, PartialEq)] +pub struct Dependency { + name: String, + version: (Option, Option), +} + +impl Dependency { + #[allow(clippy::must_use_candidate)] + pub fn satisfied(&self, package: &Package) -> bool { + if self.name.as_str() == package.name.as_str() { + match &self.version { + (Some(low), Some(high)) => { + &package.version >= low && &package.version < high + }, + (Some(low), None) => &package.version >= low, + (None, Some(high)) => &package.version < high, + // no version requirements + _ => true, + } + } else { + false + } + } +} diff --git a/src/package/mod.rs b/src/package/mod.rs new file mode 100644 index 0000000..266b215 --- /dev/null +++ b/src/package/mod.rs @@ -0,0 +1,16 @@ +mod dependency; +use { + crate::{Plist, Version}, + serde::{Serialize, Deserialize}, +}; +pub use dependency::Dependency; + +#[derive(Serialize, Deserialize)] +pub struct Package { + name: String, + version: Version, + release: u32, + plist: Plist, + size: usize, + dependencies: Vec, +} diff --git a/src/version/gitrev.rs b/src/version/gitrev.rs new file mode 100644 index 0000000..94f14b2 --- /dev/null +++ b/src/version/gitrev.rs @@ -0,0 +1,21 @@ +use std::cmp::Ordering; + +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] +pub struct GitRev { + hash: String, + datetime: u64, +} + +impl PartialOrd for GitRev { + fn partial_cmp(&self, other: &Self) -> Option { + self.datetime.partial_cmp(&other.datetime) + } +} + +impl Ord for GitRev { + fn cmp(&self, other: &Self) -> Ordering { + self.datetime.cmp(&other.datetime) + } +} diff --git a/src/version/mod.rs b/src/version/mod.rs new file mode 100644 index 0000000..56e53c9 --- /dev/null +++ b/src/version/mod.rs @@ -0,0 +1,24 @@ +use serde::{Deserialize, Serialize}; + +mod gitrev; +mod semver; + +pub use {gitrev::GitRev, semver::SemVer}; + +#[derive(Debug, Serialize, Deserialize, PartialEq)] +pub enum Version { + Number(u32), + SemVer(SemVer), + Git(GitRev), +} + +impl PartialOrd for Version { + fn partial_cmp(&self, other: &Self) -> Option { + match (self, other) { + (Self::Number(s), Self::Number(o)) => s.partial_cmp(o), + (Self::SemVer(s), Self::SemVer(o)) => s.partial_cmp(o), + (Self::Git(s), Self::Git(o)) => s.partial_cmp(o), + _ => None, + } + } +} diff --git a/src/version/semver.rs b/src/version/semver.rs new file mode 100644 index 0000000..115386a --- /dev/null +++ b/src/version/semver.rs @@ -0,0 +1,41 @@ +use { + serde::{Deserialize, Serialize}, + std::cmp::Ordering, +}; + +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] +pub struct SemVer { + pub major: u32, + pub minor: u32, + pub patch: u32, +} + +impl PartialOrd for SemVer { + fn partial_cmp(&self, other: &Self) -> Option { + match self.major.partial_cmp(&other.major) { + Some(Ordering::Greater) => Some(Ordering::Greater), + Some(Ordering::Less) => Some(Ordering::Less), + None => None, + Some(Ordering::Equal) => match self.minor.partial_cmp(&other.minor) { + Some(Ordering::Greater) => Some(Ordering::Greater), + Some(Ordering::Less) => Some(Ordering::Less), + None => None, + Some(Ordering::Equal) => self.patch.partial_cmp(&other.patch), + }, + } + } +} + +impl Ord for SemVer { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + match self.major.cmp(&other.major) { + Ordering::Greater => Ordering::Greater, + Ordering::Less => Ordering::Less, + Ordering::Equal => match self.minor.cmp(&other.minor) { + Ordering::Greater => Ordering::Greater, + Ordering::Less => Ordering::Less, + Ordering::Equal => self.patch.cmp(&other.patch), + }, + } + } +}