Skip to content

Commit

Permalink
feat: Add ordering to Stable, Beta, Nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
foresterre committed Apr 25, 2024
1 parent b9ab5ad commit 3413bdb
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 22 deletions.
44 changes: 22 additions & 22 deletions crates/rust-toolchain/src/channel.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
mod beta;
mod nightly;
mod stable;

use crate::{ReleaseDate, RustVersion};

/// A Rust release channel
pub use beta::Beta;
pub use nightly::Nightly;
pub use stable::Stable;

/// A Rust [`release channel`].
///
/// Does not include the once used `Alpha` release channel, which has not been used post `1.0.0`.
///
/// # Variants
///
/// Alpha releases, which are no longer used, are unsupported.
/// See also: [`Stable`], [`Beta`] and [`Nightly`].
///
/// [`release channel`]: https://forge.rust-lang.org/#current-release-versions
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Channel {
/// The stable release channel
Expand All @@ -14,51 +28,37 @@ pub enum Channel {
}

impl Channel {
/// Create a new stable channel instance.
/// Create a new [`Stable`] channel instance.
pub fn stable(version: RustVersion) -> Self {
Channel::Stable(Stable { version })
}

/// Create a new beta channel instance.
/// Create a new [`Beta`] channel instance.
pub fn beta(version: RustVersion) -> Self {
Channel::Beta(Beta { version })
}

/// Create a new nightly channel instance.
/// Create a new [`Nightly`] channel instance.
pub fn nightly(date: ReleaseDate) -> Self {
Channel::Nightly(Nightly { date })
}
}

impl Channel {
/// Whether the given [`Channel`] is of the [`Stable`] variant.
pub fn is_stable(&self) -> bool {
matches!(self, Self::Stable(_))
}

/// Whether the given [`Channel`] is of the [`Beta`] variant.
pub fn is_beta(&self) -> bool {
matches!(self, Self::Beta(_))
}

/// Whether the given [`Channel`] is of the [`Nightly`] variant.
pub fn is_nightly(&self) -> bool {
matches!(self, Self::Nightly(_))
}
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Stable {
pub version: RustVersion,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Beta {
pub version: RustVersion,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Nightly {
pub date: ReleaseDate,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
26 changes: 26 additions & 0 deletions crates/rust-toolchain/src/channel/beta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::RustVersion;

#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Beta {
pub version: RustVersion,
}

#[cfg(test)]
mod tests {
use crate::{Beta, RustVersion};

#[yare::parameterized(
day1 = { RustVersion::new(0, 0, 0), RustVersion::new(0, 0, 1) },
month1 = { RustVersion::new(0, 0, 0), RustVersion::new(0, 1, 0) },
year1 = { RustVersion::new(0, 0, 0), RustVersion::new(1, 0, 0) },
month_trumps_day = { RustVersion::new(0, 0, 999), RustVersion::new(0, 1, 0) },
year_trumps_day = { RustVersion::new(0, 0, 999), RustVersion::new(1, 0, 0) },
year_trumps_month = { RustVersion::new(0, 999, 0), RustVersion::new(1, 0, 0) },
)]
fn ord(left: RustVersion, right: RustVersion) {
let left = Beta { version: left };
let right = Beta { version: right };

assert!(left < right);
}
}
26 changes: 26 additions & 0 deletions crates/rust-toolchain/src/channel/nightly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::ReleaseDate;

#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Nightly {
pub date: ReleaseDate,
}

#[cfg(test)]
mod tests {
use crate::{Nightly, ReleaseDate};

#[yare::parameterized(
patch1 = { ReleaseDate::new(0, 0, 0), ReleaseDate::new(0, 0, 1) },
minor1 = { ReleaseDate::new(0, 0, 0), ReleaseDate::new(0, 1, 0) },
major1 = { ReleaseDate::new(0, 0, 0), ReleaseDate::new(1, 0, 0) },
minor_trumps_patch = { ReleaseDate::new(0, 0, 99), ReleaseDate::new(0, 1, 0) },
major_trumps_patch = { ReleaseDate::new(0, 0, 99), ReleaseDate::new(1, 0, 0) },
major_trumps_minor = { ReleaseDate::new(0, 99, 0), ReleaseDate::new(1, 0, 0) },
)]
fn ord(left: ReleaseDate, right: ReleaseDate) {
let left = Nightly { date: left };
let right = Nightly { date: right };

assert!(left < right);
}
}
26 changes: 26 additions & 0 deletions crates/rust-toolchain/src/channel/stable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::RustVersion;

#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Stable {
pub version: RustVersion,
}

#[cfg(test)]
mod tests {
use crate::{RustVersion, Stable};

#[yare::parameterized(
patch1 = { RustVersion::new(0, 0, 0), RustVersion::new(0, 0, 1) },
minor1 = { RustVersion::new(0, 0, 0), RustVersion::new(0, 1, 0) },
major1 = { RustVersion::new(0, 0, 0), RustVersion::new(1, 0, 0) },
minor_trumps_patch = { RustVersion::new(0, 0, 999), RustVersion::new(0, 1, 0) },
major_trumps_patch = { RustVersion::new(0, 0, 999), RustVersion::new(1, 0, 0) },
major_trumps_minor = { RustVersion::new(0, 999, 0), RustVersion::new(1, 0, 0) },
)]
fn ord(left: RustVersion, right: RustVersion) {
let left = Stable { version: left };
let right = Stable { version: right };

assert!(left < right);
}
}

0 comments on commit 3413bdb

Please sign in to comment.