Fleshed out some data structures:

- Package
- Version
- Dependency
This commit is contained in:
Nathan Fisher 2023-03-21 14:29:27 -04:00
parent 06bb42ecb9
commit d210c44905
7 changed files with 145 additions and 1 deletions

View File

@ -13,3 +13,8 @@ sha2 = "0.10"
[dependencies.serde] [dependencies.serde]
version = "1.0" version = "1.0"
features = ["derive"] features = ["derive"]
[profile.release]
codegen-units = 1
lto = true
strip = true

View File

@ -1,2 +1,9 @@
#![warn(clippy::all, clippy::pedantic)]
mod package;
mod plist; mod plist;
pub use plist::*; mod version;
pub use {
package::{Dependency, Package},
plist::*,
version::*,
};

30
src/package/dependency.rs Normal file
View File

@ -0,0 +1,30 @@
use {
crate::Version,
serde::{Serialize, Deserialize},
super::Package,
};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct Dependency {
name: String,
version: (Option<Version>, Option<Version>),
}
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
}
}
}

16
src/package/mod.rs Normal file
View File

@ -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<Dependency>,
}

21
src/version/gitrev.rs Normal file
View File

@ -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<Ordering> {
self.datetime.partial_cmp(&other.datetime)
}
}
impl Ord for GitRev {
fn cmp(&self, other: &Self) -> Ordering {
self.datetime.cmp(&other.datetime)
}
}

24
src/version/mod.rs Normal file
View File

@ -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<std::cmp::Ordering> {
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,
}
}
}

41
src/version/semver.rs Normal file
View File

@ -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<Ordering> {
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),
},
}
}
}