Version - use tuple types instead of structs for less verbosity
This commit is contained in:
parent
a1843cac33
commit
9126e1da93
@ -37,7 +37,7 @@ impl Creator {
|
|||||||
let path = path.to_path_buf();
|
let path = path.to_path_buf();
|
||||||
let entries = WalkDir::new(".")
|
let entries = WalkDir::new(".")
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|x| x.is_ok())
|
.filter(Result::is_ok)
|
||||||
.map(|x| x.unwrap().path().to_path_buf())
|
.map(|x| x.unwrap().path().to_path_buf())
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
env::set_current_dir(d)?;
|
env::set_current_dir(d)?;
|
||||||
@ -61,6 +61,10 @@ impl Creator {
|
|||||||
self.entries.len()
|
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>> {
|
pub fn create(self, outdir: &Path, sender: Sender<Message>) -> Result<(), Box<dyn Error>> {
|
||||||
let d = env::current_dir()?;
|
let d = env::current_dir()?;
|
||||||
let plist = Mutex::new(Plist::default());
|
let plist = Mutex::new(Plist::default());
|
||||||
@ -70,7 +74,7 @@ impl Creator {
|
|||||||
&self.specs.name, self.specs.version, self.specs.release
|
&self.specs.name, self.specs.version, self.specs.release
|
||||||
);
|
);
|
||||||
if !outdir.exists() {
|
if !outdir.exists() {
|
||||||
fs::create_dir_all(&outdir)?;
|
fs::create_dir_all(outdir)?;
|
||||||
}
|
}
|
||||||
let outdir = outdir.canonicalize()?;
|
let outdir = outdir.canonicalize()?;
|
||||||
let mut archive = outdir.clone();
|
let mut archive = outdir.clone();
|
||||||
@ -101,9 +105,9 @@ impl Creator {
|
|||||||
sha256sum: _,
|
sha256sum: _,
|
||||||
mode: _,
|
mode: _,
|
||||||
size: _,
|
size: _,
|
||||||
} => path.clone(),
|
}
|
||||||
Entry::Link { path, target: _ } => path.clone(),
|
| Entry::Link { path, target: _ }
|
||||||
Entry::Directory { path, mode: _ } => path.clone(),
|
| Entry::Directory { path, mode: _ } => path,
|
||||||
};
|
};
|
||||||
plist.lock().unwrap().borrow_mut().entries.push(item.entry);
|
plist.lock().unwrap().borrow_mut().entries.push(item.entry);
|
||||||
match writer.lock().unwrap().borrow_mut().write_all(&item.data) {
|
match writer.lock().unwrap().borrow_mut().write_all(&item.data) {
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
use ron::ser::PrettyConfig;
|
|
||||||
|
|
||||||
use {
|
use {
|
||||||
crate::{Package, Repository, Version},
|
crate::{Package, Repository, Version},
|
||||||
rayon::prelude::*,
|
rayon::prelude::*,
|
||||||
|
ron::ser::PrettyConfig,
|
||||||
serde::{Deserialize, Serialize},
|
serde::{Deserialize, Serialize},
|
||||||
std::{
|
std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
@ -32,7 +31,7 @@ impl fmt::Display for Update {
|
|||||||
|
|
||||||
impl Update {
|
impl Update {
|
||||||
pub fn full_url(&self) -> Result<Url, url::ParseError> {
|
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
|
// Check if the remote has a package by this name
|
||||||
if let Some(remote_package) = repo.packages.get(key) {
|
if let Some(remote_package) = repo.packages.get(key) {
|
||||||
// Check if the remote package is an update
|
// 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() {
|
if let Ok(mut updates) = updates.lock() {
|
||||||
// Check if we've already pulled in an update from another repo,
|
// Check if we've already pulled in an update from another repo,
|
||||||
// and if so compare versions
|
// and if so compare versions
|
||||||
@ -151,4 +150,19 @@ mod test {
|
|||||||
.to_string();
|
.to_string();
|
||||||
assert_eq!("hpk-42_1".to_string(), up);
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ impl Repository {
|
|||||||
pub fn search_names(&self, query: &str) -> Result<Vec<&Package>, PoisonError<Vec<&Package>>> {
|
pub fn search_names(&self, query: &str) -> Result<Vec<&Package>, PoisonError<Vec<&Package>>> {
|
||||||
let mut results = vec![];
|
let mut results = vec![];
|
||||||
if let Some(p) = self.packages.get(query) {
|
if let Some(p) = self.packages.get(query) {
|
||||||
results.push(p)
|
results.push(p);
|
||||||
}
|
}
|
||||||
let results = Mutex::new(results);
|
let results = Mutex::new(results);
|
||||||
self.packages.par_iter().for_each(|(k, v)| {
|
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>>> {
|
pub fn search_all(&self, query: &str) -> Result<Vec<&Package>, PoisonError<Vec<&Package>>> {
|
||||||
let mut results = vec![];
|
let mut results = vec![];
|
||||||
if let Some(p) = self.packages.get(query) {
|
if let Some(p) = self.packages.get(query) {
|
||||||
results.push(p)
|
results.push(p);
|
||||||
}
|
}
|
||||||
let results = Mutex::new(results);
|
let results = Mutex::new(results);
|
||||||
self.packages.par_iter().for_each(|(k, v)| {
|
self.packages.par_iter().for_each(|(k, v)| {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use {
|
use {
|
||||||
|
crate::Version,
|
||||||
chrono::{offset::Utc, DateTime},
|
chrono::{offset::Utc, DateTime},
|
||||||
serde::{Deserialize, Serialize},
|
serde::{Deserialize, Serialize},
|
||||||
std::{cmp::Ordering, error::Error, fmt, str::FromStr},
|
std::{cmp::Ordering, error::Error, fmt, str::FromStr},
|
||||||
@ -65,3 +66,14 @@ impl FromStr for GitRev {
|
|||||||
Err(ParseGitRevError)
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use {
|
use {
|
||||||
chrono::{offset::Utc, DateTime},
|
|
||||||
serde::{Deserialize, Serialize},
|
serde::{Deserialize, Serialize},
|
||||||
std::{cmp::Ordering, error::Error, fmt, str::FromStr},
|
std::{error::Error, fmt, str::FromStr},
|
||||||
};
|
};
|
||||||
|
|
||||||
mod gitrev;
|
mod gitrev;
|
||||||
@ -13,28 +12,18 @@ pub use {gitrev::GitRev, rapid::Rapid, semver::SemVer};
|
|||||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
pub enum Version {
|
pub enum Version {
|
||||||
Number(u32),
|
Number(u32),
|
||||||
Rapid {
|
Rapid(Rapid),
|
||||||
major: u32,
|
SemVer(SemVer),
|
||||||
minor: u32,
|
Git(GitRev),
|
||||||
},
|
|
||||||
SemVer {
|
|
||||||
major: u32,
|
|
||||||
minor: u32,
|
|
||||||
patch: u32,
|
|
||||||
},
|
|
||||||
Git {
|
|
||||||
hash: String,
|
|
||||||
datetime: DateTime<Utc>,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Version {
|
impl Default for Version {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::SemVer {
|
Self::SemVer(SemVer {
|
||||||
major: 0,
|
major: 0,
|
||||||
minor: 1,
|
minor: 1,
|
||||||
patch: 0,
|
patch: 0,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,61 +31,28 @@ impl fmt::Display for Version {
|
|||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Number(n) => write!(f, "{n}"),
|
Self::Number(n) => write!(f, "{n}"),
|
||||||
Self::SemVer {
|
Self::SemVer(s) => write!(f, "{s}"),
|
||||||
major,
|
Self::Rapid(r) => write!(f, "{r}"),
|
||||||
minor,
|
Self::Git(g) => write!(f, "{g}"),
|
||||||
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 {
|
impl From<SemVer> for Version {
|
||||||
fn from(value: SemVer) -> Self {
|
fn from(value: SemVer) -> Self {
|
||||||
Self::SemVer {
|
Self::SemVer(value)
|
||||||
major: value.major,
|
|
||||||
minor: value.minor,
|
|
||||||
patch: value.patch,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Rapid> for Version {
|
impl From<Rapid> for Version {
|
||||||
fn from(value: Rapid) -> Self {
|
fn from(value: Rapid) -> Self {
|
||||||
Self::Rapid {
|
Self::Rapid(value)
|
||||||
major: value.major,
|
|
||||||
minor: value.minor,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<GitRev> for Version {
|
impl From<GitRev> for Version {
|
||||||
fn from(value: GitRev) -> Self {
|
fn from(value: GitRev) -> Self {
|
||||||
Self::Git {
|
Self::Git(value)
|
||||||
hash: value.hash,
|
|
||||||
datetime: value.datetime,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,47 +67,15 @@ impl PartialOrd for Version {
|
|||||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Self::Number(s), Self::Number(o)) => s.partial_cmp(o),
|
(Self::Number(s), Self::Number(o)) => s.partial_cmp(o),
|
||||||
(
|
(Self::Number(s), Self::Rapid(o)) => s.partial_cmp(o),
|
||||||
Self::SemVer {
|
(Self::Number(s), Self::SemVer(o)) => s.partial_cmp(o),
|
||||||
major,
|
(Self::Rapid(s), Self::Number(o)) => s.partial_cmp(o),
|
||||||
minor,
|
(Self::Rapid(s), Self::Rapid(o)) => s.partial_cmp(o),
|
||||||
patch,
|
(Self::Rapid(s), Self::SemVer(o)) => s.partial_cmp(o),
|
||||||
},
|
(Self::SemVer(s), Self::Number(o)) => s.partial_cmp(o),
|
||||||
Self::SemVer {
|
(Self::SemVer(s), Self::Rapid(o)) => s.partial_cmp(o),
|
||||||
major: a,
|
(Self::SemVer(s), Self::SemVer(o)) => s.partial_cmp(o),
|
||||||
minor: b,
|
(Self::Git(s), Self::Git(o)) => s.partial_cmp(o),
|
||||||
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,
|
_ => 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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
206
src/version/mod.rs.bak
Normal file
206
src/version/mod.rs.bak
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,7 @@
|
|||||||
|
use crate::SemVer;
|
||||||
|
|
||||||
use {
|
use {
|
||||||
|
crate::Version,
|
||||||
serde::{Deserialize, Serialize},
|
serde::{Deserialize, Serialize},
|
||||||
std::{cmp::Ordering, error::Error, fmt, num::ParseIntError, str::FromStr},
|
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)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct ParseRapidError;
|
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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use {
|
use {
|
||||||
|
crate::{Rapid, Version},
|
||||||
serde::{Deserialize, Serialize},
|
serde::{Deserialize, Serialize},
|
||||||
std::{cmp::Ordering, error::Error, fmt, num::ParseIntError, str::FromStr},
|
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)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct ParseSemVerError;
|
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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
Loading…
Reference in New Issue
Block a user