From 9126e1da9327655392fdf8ab81fd7379f06b4e37 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sat, 1 Apr 2023 11:02:29 -0400 Subject: [PATCH] Version - use tuple types instead of structs for less verbosity --- src/creator/mod.rs | 14 ++- src/db/mod.rs | 22 ++++- src/repository/mod.rs | 4 +- src/version/gitrev.rs | 12 +++ src/version/mod.rs | 143 +++++----------------------- src/version/mod.rs.bak | 206 +++++++++++++++++++++++++++++++++++++++++ src/version/rapid.rs | 82 ++++++++++++++++ src/version/semver.rs | 89 ++++++++++++++++++ 8 files changed, 439 insertions(+), 133 deletions(-) create mode 100644 src/version/mod.rs.bak diff --git a/src/creator/mod.rs b/src/creator/mod.rs index 2558bfe..e143601 100644 --- a/src/creator/mod.rs +++ b/src/creator/mod.rs @@ -37,7 +37,7 @@ impl Creator { let path = path.to_path_buf(); let entries = WalkDir::new(".") .into_iter() - .filter(|x| x.is_ok()) + .filter(Result::is_ok) .map(|x| x.unwrap().path().to_path_buf()) .collect::>(); env::set_current_dir(d)?; @@ -61,6 +61,10 @@ impl Creator { self.entries.len() } + pub fn is_empty(&self) -> bool { + self.entries.is_empty() + } + pub fn create(self, outdir: &Path, sender: Sender) -> Result<(), Box> { let d = env::current_dir()?; let plist = Mutex::new(Plist::default()); @@ -70,7 +74,7 @@ impl Creator { &self.specs.name, self.specs.version, self.specs.release ); if !outdir.exists() { - fs::create_dir_all(&outdir)?; + fs::create_dir_all(outdir)?; } let outdir = outdir.canonicalize()?; let mut archive = outdir.clone(); @@ -101,9 +105,9 @@ impl Creator { sha256sum: _, mode: _, size: _, - } => path.clone(), - Entry::Link { path, target: _ } => path.clone(), - Entry::Directory { path, mode: _ } => path.clone(), + } + | Entry::Link { path, target: _ } + | Entry::Directory { path, mode: _ } => path, }; plist.lock().unwrap().borrow_mut().entries.push(item.entry); match writer.lock().unwrap().borrow_mut().write_all(&item.data) { diff --git a/src/db/mod.rs b/src/db/mod.rs index 73c20b2..0dad30c 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,8 +1,7 @@ -use ron::ser::PrettyConfig; - use { crate::{Package, Repository, Version}, rayon::prelude::*, + ron::ser::PrettyConfig, serde::{Deserialize, Serialize}, std::{ collections::HashMap, @@ -32,7 +31,7 @@ impl fmt::Display for Update { impl Update { pub fn full_url(&self) -> Result { - self.url.join(&format!("{}.tar.zstd", self)) + self.url.join(&format!("{self}.tar.zstd")) } } @@ -64,7 +63,7 @@ impl Database { // Check if the remote has a package by this name if let Some(remote_package) = repo.packages.get(key) { // Check if the remote package is an update - if remote_package.is_upgrade(&local_package) { + if remote_package.is_upgrade(local_package) { if let Ok(mut updates) = updates.lock() { // Check if we've already pulled in an update from another repo, // and if so compare versions @@ -151,4 +150,19 @@ mod test { .to_string(); assert_eq!("hpk-42_1".to_string(), up); } + + #[test] + fn upate_full_url() { + let up = Update { + name: "hpk".to_string(), + version: Version::Number(42), + release: 1, + url: Url::parse("https://hitchhiker-linux.org/pub/packages/").unwrap(), + }; + let full_url = up.full_url().unwrap().to_string(); + assert_eq!( + "https://hitchhiker-linux.org/pub/packages/hpk-42_1.tar.zstd".to_string(), + full_url + ); + } } diff --git a/src/repository/mod.rs b/src/repository/mod.rs index 1c1872e..2a88db1 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -19,7 +19,7 @@ impl Repository { pub fn search_names(&self, query: &str) -> Result, PoisonError>> { let mut results = vec![]; if let Some(p) = self.packages.get(query) { - results.push(p) + results.push(p); } let results = Mutex::new(results); self.packages.par_iter().for_each(|(k, v)| { @@ -36,7 +36,7 @@ impl Repository { pub fn search_all(&self, query: &str) -> Result, PoisonError>> { let mut results = vec![]; if let Some(p) = self.packages.get(query) { - results.push(p) + results.push(p); } let results = Mutex::new(results); self.packages.par_iter().for_each(|(k, v)| { diff --git a/src/version/gitrev.rs b/src/version/gitrev.rs index c2e6052..4b6ef38 100644 --- a/src/version/gitrev.rs +++ b/src/version/gitrev.rs @@ -1,4 +1,5 @@ use { + crate::Version, chrono::{offset::Utc, DateTime}, serde::{Deserialize, Serialize}, std::{cmp::Ordering, error::Error, fmt, str::FromStr}, @@ -65,3 +66,14 @@ impl FromStr for GitRev { Err(ParseGitRevError) } } + +impl TryFrom for GitRev { + type Error = ParseGitRevError; + + fn try_from(value: Version) -> Result { + match value { + Version::Git(g) => Ok(g), + _ => Err(ParseGitRevError), + } + } +} diff --git a/src/version/mod.rs b/src/version/mod.rs index 3ee2d71..06b8057 100644 --- a/src/version/mod.rs +++ b/src/version/mod.rs @@ -1,7 +1,6 @@ use { - chrono::{offset::Utc, DateTime}, serde::{Deserialize, Serialize}, - std::{cmp::Ordering, error::Error, fmt, str::FromStr}, + std::{error::Error, fmt, str::FromStr}, }; mod gitrev; @@ -13,28 +12,18 @@ pub use {gitrev::GitRev, rapid::Rapid, semver::SemVer}; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub enum Version { Number(u32), - Rapid { - major: u32, - minor: u32, - }, - SemVer { - major: u32, - minor: u32, - patch: u32, - }, - Git { - hash: String, - datetime: DateTime, - }, + Rapid(Rapid), + SemVer(SemVer), + Git(GitRev), } impl Default for Version { fn default() -> Self { - Self::SemVer { + Self::SemVer(SemVer { major: 0, minor: 1, patch: 0, - } + }) } } @@ -42,61 +31,28 @@ impl fmt::Display for Version { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Number(n) => write!(f, "{n}"), - Self::SemVer { - major, - minor, - patch, - } => { - let v = SemVer { - major: *major, - minor: *minor, - patch: *patch, - }; - write!(f, "{v}") - } - Self::Rapid { major, minor } => { - let v = Rapid { - major: *major, - minor: *minor, - }; - write!(f, "{v}") - } - Self::Git { hash, datetime } => { - let v = GitRev { - hash: hash.clone(), - datetime: *datetime, - }; - write!(f, "{v}") - } + Self::SemVer(s) => write!(f, "{s}"), + Self::Rapid(r) => write!(f, "{r}"), + Self::Git(g) => write!(f, "{g}"), } } } impl From for Version { fn from(value: SemVer) -> Self { - Self::SemVer { - major: value.major, - minor: value.minor, - patch: value.patch, - } + Self::SemVer(value) } } impl From for Version { fn from(value: Rapid) -> Self { - Self::Rapid { - major: value.major, - minor: value.minor, - } + Self::Rapid(value) } } impl From for Version { fn from(value: GitRev) -> Self { - Self::Git { - hash: value.hash, - datetime: value.datetime, - } + Self::Git(value) } } @@ -111,47 +67,15 @@ 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 { - major, - minor, - patch, - }, - Self::SemVer { - major: a, - minor: b, - patch: c, - }, - ) => (major, minor, patch).partial_cmp(&(a, b, c)), - (Self::Rapid { major, minor }, Self::Rapid { major: a, minor: b }) => { - (major, minor).partial_cmp(&(a, b)) - } - ( - Self::Git { - hash: _a, - datetime: b, - }, - Self::Git { - hash: _c, - datetime: d, - }, - ) => b.partial_cmp(&d), - ( - Self::SemVer { - major, - minor, - patch, - }, - Self::Rapid { major: a, minor: b }, - ) => SemVer { - major: *major, - minor: *minor, - patch: *patch, - } - .partial_cmp(&Rapid { - major: *a, - minor: *b, - }), + (Self::Number(s), Self::Rapid(o)) => s.partial_cmp(o), + (Self::Number(s), Self::SemVer(o)) => s.partial_cmp(o), + (Self::Rapid(s), Self::Number(o)) => s.partial_cmp(o), + (Self::Rapid(s), Self::Rapid(o)) => s.partial_cmp(o), + (Self::Rapid(s), Self::SemVer(o)) => s.partial_cmp(o), + (Self::SemVer(s), Self::Number(o)) => s.partial_cmp(o), + (Self::SemVer(s), Self::Rapid(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, } } @@ -186,31 +110,6 @@ impl FromStr for Version { } } -impl PartialEq for SemVer { - fn eq(&self, other: &Rapid) -> bool { - self.major == other.major && self.minor == other.minor && self.patch == 0 - } -} - -impl PartialOrd for SemVer { - fn partial_cmp(&self, other: &Rapid) -> 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) => match self.patch { - 0 => Some(Ordering::Equal), - _ => Some(Ordering::Greater), - }, - }, - } - } -} - #[cfg(test)] mod test { use super::*; diff --git a/src/version/mod.rs.bak b/src/version/mod.rs.bak new file mode 100644 index 0000000..9208624 --- /dev/null +++ b/src/version/mod.rs.bak @@ -0,0 +1,206 @@ +use { + chrono::{offset::Utc, DateTime}, + serde::{Deserialize, Serialize}, + std::{error::Error, fmt, str::FromStr}, +}; + +mod gitrev; +mod rapid; +mod semver; + +pub use {gitrev::GitRev, rapid::Rapid, semver::SemVer}; + +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub enum Version { + Number(u32), + Rapid { + major: u32, + minor: u32, + }, + SemVer { + major: u32, + minor: u32, + patch: u32, + }, + Git { + hash: String, + datetime: DateTime, + }, +} + +impl Default for Version { + fn default() -> Self { + Self::SemVer { + major: 0, + minor: 1, + patch: 0, + } + } +} + +impl fmt::Display for Version { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Number(n) => write!(f, "{n}"), + Self::SemVer { + major, + minor, + patch, + } => { + let v = SemVer { + major: *major, + minor: *minor, + patch: *patch, + }; + write!(f, "{v}") + } + Self::Rapid { major, minor } => { + let v = Rapid { + major: *major, + minor: *minor, + }; + write!(f, "{v}") + } + Self::Git { hash, datetime } => { + let v = GitRev { + hash: hash.clone(), + datetime: *datetime, + }; + write!(f, "{v}") + } + } + } +} + +impl From for Version { + fn from(value: SemVer) -> Self { + Self::SemVer { + major: value.major, + minor: value.minor, + patch: value.patch, + } + } +} + +impl From for Version { + fn from(value: Rapid) -> Self { + Self::Rapid { + major: value.major, + minor: value.minor, + } + } +} + +impl From for Version { + fn from(value: GitRev) -> Self { + Self::Git { + hash: value.hash, + datetime: value.datetime, + } + } +} + +impl From for Version { + fn from(value: u32) -> Self { + Self::Number(value) + } +} + +impl PartialOrd for Version { + #[allow(clippy::many_single_char_names)] + fn partial_cmp(&self, other: &Self) -> Option { + match (self, other) { + (Self::Number(s), Self::Number(o)) => s.partial_cmp(o), + ( + Self::SemVer { + major, + minor, + patch, + }, + Self::SemVer { + major: a, + minor: b, + patch: c, + }, + ) => (major, minor, patch).partial_cmp(&(a, b, c)), + (Self::Rapid { major, minor }, Self::Rapid { major: a, minor: b }) => { + (major, minor).partial_cmp(&(a, b)) + } + ( + Self::Git { + hash: _a, + datetime: b, + }, + Self::Git { + hash: _c, + datetime: d, + }, + ) => b.partial_cmp(&d), + ( + Self::SemVer { + major, + minor, + patch, + }, + Self::Rapid { major: a, minor: b }, + ) => SemVer { + major: *major, + minor: *minor, + patch: *patch, + } + .partial_cmp(&Rapid { + major: *a, + minor: *b, + }), + _ => None, + } + } +} + +#[derive(Debug)] +pub struct ParseVersionError; + +impl fmt::Display for ParseVersionError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "error parsing version") + } +} + +impl Error for ParseVersionError {} + +impl FromStr for Version { + type Err = ParseVersionError; + + fn from_str(s: &str) -> Result { + if let Ok(v) = s.parse::() { + Ok(v.into()) + } else if let Ok(v) = s.parse::() { + Ok(v.into()) + } else if let Ok(v) = s.parse::() { + Ok(v.into()) + } else if let Ok(v) = s.parse::() { + Ok(v.into()) + } else { + Err(ParseVersionError) + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn cmp_semver_rapid_gt() { + let sem = "1.42.1".parse::().unwrap(); + let rpd = "1.42".parse::().unwrap(); + assert!(sem > rpd); + } + + #[test] + fn cmp_semver_rapid_eq() { + let sem = "1.42.0".parse::().unwrap(); + let rpd = "1.42".parse::().unwrap(); + assert!(sem == rpd); + } +} diff --git a/src/version/rapid.rs b/src/version/rapid.rs index 53a667e..0be2946 100644 --- a/src/version/rapid.rs +++ b/src/version/rapid.rs @@ -1,4 +1,7 @@ +use crate::SemVer; + use { + crate::Version, serde::{Deserialize, Serialize}, std::{cmp::Ordering, error::Error, fmt, num::ParseIntError, str::FromStr}, }; @@ -36,6 +39,63 @@ impl Ord for Rapid { } } +impl PartialEq for Rapid { + fn eq(&self, other: &u32) -> bool { + self.major == *other && self.minor == 0 + } +} + +impl PartialOrd for Rapid { + fn partial_cmp(&self, other: &u32) -> Option { + match self.major.partial_cmp(other) { + Some(Ordering::Greater) => Some(Ordering::Greater), + Some(Ordering::Less) => Some(Ordering::Less), + None => None, + Some(Ordering::Equal) => { + if self.minor == 0 { + Some(Ordering::Equal) + } else { + Some(Ordering::Greater) + } + } + } + } +} + +impl PartialEq for Rapid { + fn eq(&self, other: &SemVer) -> bool { + other.eq(self) + } +} + +impl PartialOrd for Rapid { + fn partial_cmp(&self, other: &SemVer) -> Option { + match other.partial_cmp(self) { + Some(Ordering::Less) => Some(Ordering::Greater), + Some(Ordering::Greater) => Some(Ordering::Less), + Some(Ordering::Equal) => Some(Ordering::Equal), + None => None, + } + } +} + +impl PartialEq for u32 { + fn eq(&self, other: &Rapid) -> bool { + other.eq(self) + } +} + +impl PartialOrd for u32 { + fn partial_cmp(&self, other: &Rapid) -> Option { + match other.partial_cmp(self) { + Some(Ordering::Equal) => Some(Ordering::Equal), + Some(Ordering::Less) => Some(Ordering::Greater), + Some(Ordering::Greater) => Some(Ordering::Less), + None => None, + } + } +} + #[derive(Debug, PartialEq)] pub struct ParseRapidError; @@ -69,6 +129,28 @@ impl FromStr for Rapid { } } +impl TryFrom for Rapid { + type Error = ParseRapidError; + + fn try_from(value: Version) -> Result { + match value { + Version::SemVer(s) => { + if s.patch == 0 { + Ok(Self { + major: s.major, + minor: s.minor, + }) + } else { + Err(ParseRapidError) + } + } + Version::Rapid(s) => Ok(s), + Version::Number(major) => Ok(Self { major, minor: 0 }), + Version::Git(_) => Err(ParseRapidError), + } + } +} + #[cfg(test)] mod test { use super::*; diff --git a/src/version/semver.rs b/src/version/semver.rs index e4dbc35..cc9d3da 100644 --- a/src/version/semver.rs +++ b/src/version/semver.rs @@ -1,4 +1,5 @@ use { + crate::{Rapid, Version}, serde::{Deserialize, Serialize}, std::{cmp::Ordering, error::Error, fmt, num::ParseIntError, str::FromStr}, }; @@ -46,6 +47,70 @@ impl Ord for SemVer { } } +impl PartialEq for SemVer { + fn eq(&self, other: &Rapid) -> bool { + self.major == other.major && self.minor == other.minor && self.patch == 0 + } +} + +impl PartialOrd for SemVer { + fn partial_cmp(&self, other: &Rapid) -> 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) => match self.patch { + 0 => Some(Ordering::Equal), + _ => Some(Ordering::Greater), + }, + }, + } + } +} + +impl PartialEq for SemVer { + fn eq(&self, other: &u32) -> bool { + self.major == *other && self.minor == 0 && self.patch == 0 + } +} + +impl PartialOrd for SemVer { + fn partial_cmp(&self, other: &u32) -> Option { + match self.major.cmp(other) { + Ordering::Greater => Some(Ordering::Greater), + Ordering::Less => Some(Ordering::Less), + Ordering::Equal => { + if self.minor == 0 && self.patch == 0 { + Some(Ordering::Equal) + } else { + Some(Ordering::Greater) + } + } + } + } +} + +impl PartialEq for u32 { + fn eq(&self, other: &SemVer) -> bool { + other.eq(self) + } +} + +impl PartialOrd for u32 { + fn partial_cmp(&self, other: &SemVer) -> Option { + match other.partial_cmp(self) { + Some(Ordering::Less) => Some(Ordering::Greater), + Some(Ordering::Greater) => Some(Ordering::Less), + Some(Ordering::Equal) => Some(Ordering::Equal), + None => None, + } + } +} + #[derive(Debug, PartialEq)] pub struct ParseSemVerError; @@ -84,6 +149,30 @@ impl FromStr for SemVer { } } +impl TryFrom for SemVer { + type Error = ParseSemVerError; + fn try_from(value: Version) -> Result { + match value { + Version::SemVer(s) => Ok(SemVer { + major: s.major, + minor: s.minor, + patch: s.patch, + }), + Version::Rapid(r) => Ok(SemVer { + major: r.major, + minor: r.minor, + patch: 0, + }), + Version::Number(n) => Ok(Self { + major: n, + minor: 0, + patch: 0, + }), + Version::Git(_) => Err(ParseSemVerError), + } + } +} + #[cfg(test)] mod test { use super::*;