Version - use tuple types instead of structs for less verbosity

This commit is contained in:
Nathan Fisher 2023-04-01 11:02:29 -04:00
parent a1843cac33
commit 9126e1da93
8 changed files with 439 additions and 133 deletions

View File

@ -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::<Vec<_>>();
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<Message>) -> Result<(), Box<dyn Error>> {
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) {

View File

@ -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<Url, url::ParseError> {
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
);
}
}

View File

@ -19,7 +19,7 @@ impl Repository {
pub fn search_names(&self, query: &str) -> Result<Vec<&Package>, PoisonError<Vec<&Package>>> {
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<Vec<&Package>, PoisonError<Vec<&Package>>> {
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)| {

View File

@ -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<Version> for GitRev {
type Error = ParseGitRevError;
fn try_from(value: Version) -> Result<Self, Self::Error> {
match value {
Version::Git(g) => Ok(g),
_ => Err(ParseGitRevError),
}
}
}

View File

@ -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<Utc>,
},
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<SemVer> for Version {
fn from(value: SemVer) -> Self {
Self::SemVer {
major: value.major,
minor: value.minor,
patch: value.patch,
}
Self::SemVer(value)
}
}
impl From<Rapid> for Version {
fn from(value: Rapid) -> Self {
Self::Rapid {
major: value.major,
minor: value.minor,
}
Self::Rapid(value)
}
}
impl From<GitRev> 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<std::cmp::Ordering> {
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<Rapid> for SemVer {
fn eq(&self, other: &Rapid) -> bool {
self.major == other.major && self.minor == other.minor && self.patch == 0
}
}
impl PartialOrd<Rapid> for SemVer {
fn partial_cmp(&self, other: &Rapid) -> Option<std::cmp::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) => match self.patch {
0 => Some(Ordering::Equal),
_ => Some(Ordering::Greater),
},
},
}
}
}
#[cfg(test)]
mod test {
use super::*;

206
src/version/mod.rs.bak Normal file
View File

@ -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<Utc>,
},
}
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<SemVer> for Version {
fn from(value: SemVer) -> Self {
Self::SemVer {
major: value.major,
minor: value.minor,
patch: value.patch,
}
}
}
impl From<Rapid> for Version {
fn from(value: Rapid) -> Self {
Self::Rapid {
major: value.major,
minor: value.minor,
}
}
}
impl From<GitRev> for Version {
fn from(value: GitRev) -> Self {
Self::Git {
hash: value.hash,
datetime: value.datetime,
}
}
}
impl From<u32> 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<std::cmp::Ordering> {
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<Self, Self::Err> {
if let Ok(v) = s.parse::<SemVer>() {
Ok(v.into())
} else if let Ok(v) = s.parse::<Rapid>() {
Ok(v.into())
} else if let Ok(v) = s.parse::<GitRev>() {
Ok(v.into())
} else if let Ok(v) = s.parse::<u32>() {
Ok(v.into())
} else {
Err(ParseVersionError)
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn cmp_semver_rapid_gt() {
let sem = "1.42.1".parse::<SemVer>().unwrap();
let rpd = "1.42".parse::<Rapid>().unwrap();
assert!(sem > rpd);
}
#[test]
fn cmp_semver_rapid_eq() {
let sem = "1.42.0".parse::<SemVer>().unwrap();
let rpd = "1.42".parse::<Rapid>().unwrap();
assert!(sem == rpd);
}
}

View File

@ -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<u32> for Rapid {
fn eq(&self, other: &u32) -> bool {
self.major == *other && self.minor == 0
}
}
impl PartialOrd<u32> for Rapid {
fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
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<SemVer> for Rapid {
fn eq(&self, other: &SemVer) -> bool {
other.eq(self)
}
}
impl PartialOrd<SemVer> for Rapid {
fn partial_cmp(&self, other: &SemVer) -> Option<Ordering> {
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<Rapid> for u32 {
fn eq(&self, other: &Rapid) -> bool {
other.eq(self)
}
}
impl PartialOrd<Rapid> for u32 {
fn partial_cmp(&self, other: &Rapid) -> Option<Ordering> {
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<Version> for Rapid {
type Error = ParseRapidError;
fn try_from(value: Version) -> Result<Self, Self::Error> {
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::*;

View File

@ -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<Rapid> for SemVer {
fn eq(&self, other: &Rapid) -> bool {
self.major == other.major && self.minor == other.minor && self.patch == 0
}
}
impl PartialOrd<Rapid> for SemVer {
fn partial_cmp(&self, other: &Rapid) -> Option<std::cmp::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) => match self.patch {
0 => Some(Ordering::Equal),
_ => Some(Ordering::Greater),
},
},
}
}
}
impl PartialEq<u32> for SemVer {
fn eq(&self, other: &u32) -> bool {
self.major == *other && self.minor == 0 && self.patch == 0
}
}
impl PartialOrd<u32> for SemVer {
fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
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<SemVer> for u32 {
fn eq(&self, other: &SemVer) -> bool {
other.eq(self)
}
}
impl PartialOrd<SemVer> for u32 {
fn partial_cmp(&self, other: &SemVer) -> Option<Ordering> {
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<Version> for SemVer {
type Error = ParseSemVerError;
fn try_from(value: Version) -> Result<Self, Self::Error> {
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::*;