Refactor and fix Clippy lints

This commit is contained in:
Nathan Fisher 2024-01-15 19:02:53 -05:00
parent 32dd349c70
commit 9255c3e1f2
10 changed files with 198 additions and 182 deletions

View File

@ -1,10 +1,11 @@
use { use {
crate::Error, crate::error::Error,
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{fmt, str}, std::{fmt, str},
}; };
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)] #[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
/// Represents the processor architecture the software is compiled for
pub enum Arch { pub enum Arch {
Any, Any,
Arm, Arm,

View File

@ -1,5 +1,8 @@
use { use {
crate::{Error, GitRev, PreRelease, Rapid, SemVer, Simple, MAX_U12}, crate::{
error::Error, gitrev::GitRev, prerelease::PreRelease, rapid::Rapid, semver::SemVer,
simple::Simple, MAX_U12,
},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{cmp::Ordering, fmt, str::FromStr}, std::{cmp::Ordering, fmt, str::FromStr},
}; };

View File

@ -1,12 +1,21 @@
use { use {
crate::{Error, Extended, Kind, Rapid, SemVer, Simple, Version}, crate::{
error::Error, extended::Extended, rapid::Rapid, semver::SemVer, simple::Simple,
version::Kind, version::Version,
},
chrono::{offset::Utc, DateTime}, chrono::{offset::Utc, DateTime},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{cmp::Ordering, fmt, str::FromStr}, std::{cmp::Ordering, fmt, str::FromStr},
}; };
/// Represents a Git revisionrather than a regular release.
/// A `GitRev` release contains the revision's hash and the Date/Time of the release.
/// Git revisions are not the preferred method of distribution for obvious reasons, as
/// they generally do not represent a stable release of the code. Another drawback is
/// that a Git revision can only be compared against another Git revision, making it
/// impossible to properly order updates between a proper SemVer type release and a\
/// Git revision.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
/// Represents a Git revision
pub struct GitRev { pub struct GitRev {
/// the short revision hash /// the short revision hash
pub hash: String, pub hash: String,
@ -22,7 +31,7 @@ impl fmt::Display for GitRev {
impl PartialOrd for GitRev { impl PartialOrd for GitRev {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.datetime.partial_cmp(&other.datetime) Some(self.cmp(other))
} }
} }

View File

@ -1,166 +1,14 @@
use {
serde::{Deserialize, Serialize},
std::{fmt, str::FromStr},
};
mod arch; mod arch;
mod error; mod error;
mod extended; mod extended;
mod gitrev; mod gitrev;
pub mod prelude;
mod prerelease; mod prerelease;
mod rapid; mod rapid;
mod semver; mod semver;
mod simple; mod simple;
mod version;
pub use { pub use version::Version;
arch::Arch, error::Error, extended::Extended, gitrev::GitRev, prerelease::PreRelease,
rapid::Rapid, semver::SemVer, simple::Simple,
};
pub static MAX_U12: u16 = 4096; pub static MAX_U12: u16 = 4096;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Kind {
Simple(Simple),
Rapid(Rapid),
SemVer(SemVer),
Extended(Extended),
GitRev(GitRev),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Version(Kind, Arch);
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
Kind::Simple(s) => write!(f, "{s}-{}", self.1),
Kind::Rapid(r) => write!(f, "{r}-{}", self.1),
Kind::SemVer(s) => write!(f, "{s}-{}", self.1),
Kind::Extended(x) => write!(f, "{x}-{}", self.1),
Kind::GitRev(g) => write!(f, "{g}-{}", self.1),
}
}
}
impl FromStr for Version {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let Some((s, Some(arch))) = s.split_once('-').map(|(s, a)| (s, a.parse::<Arch>().ok()))
else {
return Err(Error::ParseArch);
};
if let Ok(v) = Simple::from_str(s) {
Ok(Self(Kind::Simple(v), arch))
} else if let Ok(v) = Rapid::from_str(s) {
Ok(Self(Kind::Rapid(v), arch))
} else if let Ok(v) = SemVer::from_str(s) {
Ok(Self(Kind::SemVer(v), arch))
} else if let Ok(v) = Extended::from_str(s) {
Ok(Self(Kind::Extended(v), arch))
} else if let Ok(v) = GitRev::from_str(s) {
Ok(Self(Kind::GitRev(v), arch))
} else {
Err(Error::ParseVersion)
}
}
}
impl From<Simple> for Kind {
fn from(value: Simple) -> Self {
Self::Simple(value)
}
}
impl From<Rapid> for Kind {
fn from(value: Rapid) -> Self {
Self::Rapid(value)
}
}
impl From<SemVer> for Kind {
fn from(value: SemVer) -> Self {
Self::SemVer(value)
}
}
impl From<Extended> for Kind {
fn from(value: Extended) -> Self {
Self::Extended(value)
}
}
impl From<GitRev> for Kind {
fn from(value: GitRev) -> Self {
Self::GitRev(value)
}
}
impl PartialEq for Kind {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Simple(a), Self::Simple(b)) => a == b,
(Self::Simple(a), Self::Rapid(b)) => a == b,
(Self::Simple(a), Self::SemVer(b)) => a == b,
(Self::Simple(a), Self::Extended(b)) => a == b,
(Self::Rapid(a), Self::Simple(b)) => a == b,
(Self::Rapid(a), Self::Rapid(b)) => a == b,
(Self::Rapid(a), Self::SemVer(b)) => a == b,
(Self::Rapid(a), Self::Extended(b)) => a == b,
(Self::SemVer(a), Self::Simple(b)) => a == b,
(Self::SemVer(a), Self::Rapid(b)) => a == b,
(Self::SemVer(a), Self::SemVer(b)) => a == b,
(Self::SemVer(a), Self::Extended(b)) => a == b,
(Self::Extended(a), Self::Simple(b)) => a == b,
(Self::Extended(a), Self::Rapid(b)) => a == b,
(Self::Extended(a), Self::SemVer(b)) => a == b,
(Self::Extended(a), Self::Extended(b)) => a == b,
(Self::GitRev(a), Self::GitRev(b)) => a == b,
_ => false,
}
}
}
impl Eq for Kind {}
impl PartialOrd for Kind {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (self, other) {
(Self::Simple(a), Self::Simple(b)) => a.partial_cmp(b),
(Self::Simple(a), Self::Rapid(b)) => a.partial_cmp(b),
(Self::Simple(a), Self::SemVer(b)) => a.partial_cmp(b),
(Self::Simple(a), Self::Extended(b)) => a.partial_cmp(b),
(Self::Rapid(a), Self::Simple(b)) => a.partial_cmp(b),
(Self::Rapid(a), Self::Rapid(b)) => a.partial_cmp(b),
(Self::Rapid(a), Self::SemVer(b)) => a.partial_cmp(b),
(Self::Rapid(a), Self::Extended(b)) => a.partial_cmp(b),
(Self::SemVer(a), Self::Simple(b)) => a.partial_cmp(b),
(Self::SemVer(a), Self::Rapid(b)) => a.partial_cmp(b),
(Self::SemVer(a), Self::SemVer(b)) => a.partial_cmp(b),
(Self::SemVer(a), Self::Extended(b)) => a.partial_cmp(b),
(Self::Extended(a), Self::Simple(b)) => a.partial_cmp(b),
(Self::Extended(a), Self::Rapid(b)) => a.partial_cmp(b),
(Self::Extended(a), Self::SemVer(b)) => a.partial_cmp(b),
(Self::Extended(a), Self::Extended(b)) => a.partial_cmp(b),
(Self::GitRev(a), Self::GitRev(b)) => a.partial_cmp(b),
_ => None,
}
}
}
impl PartialEq for Version {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0 && self.1 == other.1
}
}
impl PartialOrd for Version {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
if self.1 == other.1 {
self.0.partial_cmp(&other.0)
} else {
None
}
}
}

4
src/prelude.rs Normal file
View File

@ -0,0 +1,4 @@
pub use crate::{
arch::Arch, error::Error as VersionError, extended::Extended, gitrev::GitRev, rapid::Rapid,
semver::SemVer, simple::Simple, version::Kind as VersionKind,
};

View File

@ -1,5 +1,5 @@
use { use {
crate::Error, crate::error::Error,
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{cmp, fmt, num::NonZeroU16, str}, std::{cmp, fmt, num::NonZeroU16, str},
}; };
@ -18,31 +18,19 @@ pub enum PreRelease {
impl PreRelease { impl PreRelease {
pub fn is_prerelease(&self) -> bool { pub fn is_prerelease(&self) -> bool {
match self { !matches!(self, Self::None)
Self::None => false,
_ => true,
}
} }
pub fn is_alpha(&self) -> bool { pub fn is_alpha(&self) -> bool {
match self { matches!(self, Self::Alpha(_))
Self::Alpha(_) => true,
_ => false,
}
} }
pub fn is_beta(&self) -> bool { pub fn is_beta(&self) -> bool {
match self { matches!(self, Self::Beta(_))
Self::Beta(_) => true,
_ => false,
}
} }
pub fn is_rc(&self) -> bool { pub fn is_rc(&self) -> bool {
match self { matches!(self, Self::RC(_))
Self::RC(_) => true,
_ => false,
}
} }
pub fn number(&self) -> Option<u16> { pub fn number(&self) -> Option<u16> {
@ -187,9 +175,9 @@ impl From<PreRelease> for u16 {
} }
impl TryFrom<u16> for PreRelease { impl TryFrom<u16> for PreRelease {
type Error = crate::Error; type Error = crate::error::Error;
fn try_from(value: u16) -> Result<Self, crate::Error> { fn try_from(value: u16) -> Result<Self, crate::error::Error> {
let mask = 0o1_777; let mask = 0o1_777;
let v = value & mask; let v = value & mask;
let flag = value & !mask; let flag = value & !mask;

View File

@ -1,5 +1,8 @@
use { use {
crate::{Error, Extended, GitRev, PreRelease, SemVer, Simple, MAX_U12}, crate::{
error::Error, extended::Extended, gitrev::GitRev, prerelease::PreRelease, semver::SemVer,
simple::Simple, MAX_U12,
},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{cmp::Ordering, fmt, str::FromStr}, std::{cmp::Ordering, fmt, str::FromStr},
}; };

View File

@ -1,5 +1,8 @@
use { use {
crate::{Error, Extended, GitRev, PreRelease, Rapid, Simple, MAX_U12}, crate::{
error::Error, extended::Extended, gitrev::GitRev, prerelease::PreRelease, rapid::Rapid,
simple::Simple, MAX_U12,
},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{cmp::Ordering, fmt, str::FromStr}, std::{cmp::Ordering, fmt, str::FromStr},
}; };

View File

@ -1,5 +1,8 @@
use { use {
crate::{Error, Extended, GitRev, PreRelease, Rapid, SemVer, MAX_U12}, crate::{
error::Error, extended::Extended, gitrev::GitRev, prerelease::PreRelease, rapid::Rapid,
semver::SemVer, MAX_U12,
},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{cmp::Ordering, fmt, str::FromStr}, std::{cmp::Ordering, fmt, str::FromStr},
}; };

154
src/version.rs Normal file
View File

@ -0,0 +1,154 @@
use {
crate::{
arch::Arch, error::Error, extended::Extended, gitrev::GitRev, rapid::Rapid, semver::SemVer,
simple::Simple,
},
serde::{Deserialize, Serialize},
std::{fmt, str::FromStr},
};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Kind {
Simple(Simple),
Rapid(Rapid),
SemVer(SemVer),
Extended(Extended),
GitRev(GitRev),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Version(pub Kind, pub Arch);
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
Kind::Simple(s) => write!(f, "{s}-{}", self.1),
Kind::Rapid(r) => write!(f, "{r}-{}", self.1),
Kind::SemVer(s) => write!(f, "{s}-{}", self.1),
Kind::Extended(x) => write!(f, "{x}-{}", self.1),
Kind::GitRev(g) => write!(f, "{g}-{}", self.1),
}
}
}
impl FromStr for Version {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let Some((s, Some(arch))) = s.split_once('-').map(|(s, a)| (s, a.parse::<Arch>().ok()))
else {
return Err(Error::ParseArch);
};
if let Ok(v) = Simple::from_str(s) {
Ok(Self(Kind::Simple(v), arch))
} else if let Ok(v) = Rapid::from_str(s) {
Ok(Self(Kind::Rapid(v), arch))
} else if let Ok(v) = SemVer::from_str(s) {
Ok(Self(Kind::SemVer(v), arch))
} else if let Ok(v) = Extended::from_str(s) {
Ok(Self(Kind::Extended(v), arch))
} else if let Ok(v) = GitRev::from_str(s) {
Ok(Self(Kind::GitRev(v), arch))
} else {
Err(Error::ParseVersion)
}
}
}
impl From<Simple> for Kind {
fn from(value: Simple) -> Self {
Self::Simple(value)
}
}
impl From<Rapid> for Kind {
fn from(value: Rapid) -> Self {
Self::Rapid(value)
}
}
impl From<SemVer> for Kind {
fn from(value: SemVer) -> Self {
Self::SemVer(value)
}
}
impl From<Extended> for Kind {
fn from(value: Extended) -> Self {
Self::Extended(value)
}
}
impl From<GitRev> for Kind {
fn from(value: GitRev) -> Self {
Self::GitRev(value)
}
}
impl PartialEq for Kind {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Simple(a), Self::Simple(b)) => a == b,
(Self::Simple(a), Self::Rapid(b)) => a == b,
(Self::Simple(a), Self::SemVer(b)) => a == b,
(Self::Simple(a), Self::Extended(b)) => a == b,
(Self::Rapid(a), Self::Simple(b)) => a == b,
(Self::Rapid(a), Self::Rapid(b)) => a == b,
(Self::Rapid(a), Self::SemVer(b)) => a == b,
(Self::Rapid(a), Self::Extended(b)) => a == b,
(Self::SemVer(a), Self::Simple(b)) => a == b,
(Self::SemVer(a), Self::Rapid(b)) => a == b,
(Self::SemVer(a), Self::SemVer(b)) => a == b,
(Self::SemVer(a), Self::Extended(b)) => a == b,
(Self::Extended(a), Self::Simple(b)) => a == b,
(Self::Extended(a), Self::Rapid(b)) => a == b,
(Self::Extended(a), Self::SemVer(b)) => a == b,
(Self::Extended(a), Self::Extended(b)) => a == b,
(Self::GitRev(a), Self::GitRev(b)) => a == b,
_ => false,
}
}
}
impl Eq for Kind {}
impl PartialOrd for Kind {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (self, other) {
(Self::Simple(a), Self::Simple(b)) => a.partial_cmp(b),
(Self::Simple(a), Self::Rapid(b)) => a.partial_cmp(b),
(Self::Simple(a), Self::SemVer(b)) => a.partial_cmp(b),
(Self::Simple(a), Self::Extended(b)) => a.partial_cmp(b),
(Self::Rapid(a), Self::Simple(b)) => a.partial_cmp(b),
(Self::Rapid(a), Self::Rapid(b)) => a.partial_cmp(b),
(Self::Rapid(a), Self::SemVer(b)) => a.partial_cmp(b),
(Self::Rapid(a), Self::Extended(b)) => a.partial_cmp(b),
(Self::SemVer(a), Self::Simple(b)) => a.partial_cmp(b),
(Self::SemVer(a), Self::Rapid(b)) => a.partial_cmp(b),
(Self::SemVer(a), Self::SemVer(b)) => a.partial_cmp(b),
(Self::SemVer(a), Self::Extended(b)) => a.partial_cmp(b),
(Self::Extended(a), Self::Simple(b)) => a.partial_cmp(b),
(Self::Extended(a), Self::Rapid(b)) => a.partial_cmp(b),
(Self::Extended(a), Self::SemVer(b)) => a.partial_cmp(b),
(Self::Extended(a), Self::Extended(b)) => a.partial_cmp(b),
(Self::GitRev(a), Self::GitRev(b)) => a.partial_cmp(b),
_ => None,
}
}
}
impl PartialEq for Version {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0 && self.1 == other.1
}
}
impl PartialOrd for Version {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
if self.1 == other.1 {
self.0.partial_cmp(&other.0)
} else {
None
}
}
}