Skip to content

Commit f377761

Browse files
committed
Update and document the revised API
1 parent c76aa15 commit f377761

File tree

10 files changed

+300
-35
lines changed

10 files changed

+300
-35
lines changed

crates/rust-toolchain/src/channel.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,31 @@ impl Channel {
7272
pub fn is_nightly(&self) -> bool {
7373
matches!(self, Self::Nightly(_))
7474
}
75+
76+
/// Returns the release version, or None, if it's a nightly release.
77+
pub fn version(&self) -> Option<RustVersion> {
78+
match self {
79+
Channel::Stable(v) => Some(v.version),
80+
Channel::Beta(v) => Some(v.version),
81+
Channel::Nightly(_) => None,
82+
}
83+
}
84+
85+
/// Returns the release date, or
86+
pub fn date(&self) -> Option<Date> {
87+
match self {
88+
Channel::Stable(_) => None,
89+
Channel::Beta(_) => None,
90+
Channel::Nightly(v) => Some(v.date.clone()),
91+
}
92+
}
7593
}
7694

7795
#[cfg(test)]
7896
mod tests {
7997
use super::*;
98+
use crate::{Date, RustVersion};
99+
use yare::parameterized;
80100

81101
#[test]
82102
fn create_channel_stable() {
@@ -104,4 +124,13 @@ mod tests {
104124
assert!(!stable.is_beta());
105125
assert!(stable.is_nightly());
106126
}
127+
128+
#[parameterized(
129+
stable = { Channel::stable("1.2.3".parse().unwrap()), Some(RustVersion::new(1,2,3)) },
130+
beta = { Channel::beta("1.2.3".parse().unwrap()), Some(RustVersion::new(1,2,3)) },
131+
nightly = { Channel::nightly(Date::new(2024, 1, 1)), None }
132+
)]
133+
fn version(c: Channel, expected: Option<RustVersion>) {
134+
assert_eq!(c.version(), expected)
135+
}
107136
}

crates/rust-toolchain/src/channel/beta.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@ use crate::RustVersion;
55
/// [`channel`]: https://rust-lang.github.io/rustup/concepts/channels.html
66
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
77
pub struct Beta {
8+
/// The three component Rust version
89
pub version: RustVersion,
10+
/// The nth pre-release beta version, if any
911
pub prerelease: Option<u32>,
1012
}
1113

1214
#[cfg(test)]
1315
mod tests {
14-
use crate::{Beta, RustVersion};
16+
use crate::{channel::Beta, RustVersion};
1517

1618
#[yare::parameterized(
1719
day1 = { RustVersion::new(0, 0, 0), RustVersion::new(0, 0, 1) },
1820
month1 = { RustVersion::new(0, 0, 0), RustVersion::new(0, 1, 0) },
1921
year1 = { RustVersion::new(0, 0, 0), RustVersion::new(1, 0, 0) },
20-
month_trumps_day = { RustVersion::new(0, 0, 999), RustVersion::new(0, 1, 0) },
21-
year_trumps_day = { RustVersion::new(0, 0, 999), RustVersion::new(1, 0, 0) },
22-
year_trumps_month = { RustVersion::new(0, 999, 0), RustVersion::new(1, 0, 0) },
22+
month_over_day = { RustVersion::new(0, 0, 999), RustVersion::new(0, 1, 0) },
23+
year_over_day = { RustVersion::new(0, 0, 999), RustVersion::new(1, 0, 0) },
24+
year_over_month = { RustVersion::new(0, 999, 0), RustVersion::new(1, 0, 0) },
2325
)]
2426
fn ord(left: RustVersion, right: RustVersion) {
2527
let left = Beta {

crates/rust-toolchain/src/channel/nightly.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ use crate::Date;
55
/// [`channel`]: https://rust-lang.github.io/rustup/concepts/channels.html
66
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
77
pub struct Nightly {
8+
/// A short YYYY-MM-DD associated date
89
pub date: Date,
910
}
1011

1112
#[cfg(test)]
1213
mod tests {
13-
use crate::{Date, Nightly};
14+
use crate::{channel::Nightly, Date};
1415

1516
#[yare::parameterized(
1617
patch1 = { Date::new(0, 0, 0), Date::new(0, 0, 1) },
1718
minor1 = { Date::new(0, 0, 0), Date::new(0, 1, 0) },
1819
major1 = { Date::new(0, 0, 0), Date::new(1, 0, 0) },
19-
minor_trumps_patch = { Date::new(0, 0, 99), Date::new(0, 1, 0) },
20-
major_trumps_patch = { Date::new(0, 0, 99), Date::new(1, 0, 0) },
21-
major_trumps_minor = { Date::new(0, 99, 0), Date::new(1, 0, 0) },
20+
minor_over_patch = { Date::new(0, 0, 99), Date::new(0, 1, 0) },
21+
major_over_patch = { Date::new(0, 0, 99), Date::new(1, 0, 0) },
22+
major_over_minor = { Date::new(0, 99, 0), Date::new(1, 0, 0) },
2223
)]
2324
fn ord(left: Date, right: Date) {
2425
let left = Nightly { date: left };

crates/rust-toolchain/src/channel/stable.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ use crate::RustVersion;
55
/// [`channel`]: https://rust-lang.github.io/rustup/concepts/channels.html
66
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
77
pub struct Stable {
8+
/// The three component Rust version
89
pub version: RustVersion,
910
}
1011

1112
#[cfg(test)]
1213
mod tests {
13-
use crate::{RustVersion, Stable};
14+
use crate::{channel::Stable, RustVersion};
1415

1516
#[yare::parameterized(
1617
patch1 = { RustVersion::new(0, 0, 0), RustVersion::new(0, 0, 1) },
1718
minor1 = { RustVersion::new(0, 0, 0), RustVersion::new(0, 1, 0) },
1819
major1 = { RustVersion::new(0, 0, 0), RustVersion::new(1, 0, 0) },
19-
minor_trumps_patch = { RustVersion::new(0, 0, 999), RustVersion::new(0, 1, 0) },
20-
major_trumps_patch = { RustVersion::new(0, 0, 999), RustVersion::new(1, 0, 0) },
21-
major_trumps_minor = { RustVersion::new(0, 999, 0), RustVersion::new(1, 0, 0) },
20+
minor_over_patch = { RustVersion::new(0, 0, 999), RustVersion::new(0, 1, 0) },
21+
major_over_patch = { RustVersion::new(0, 0, 999), RustVersion::new(1, 0, 0) },
22+
major_over_minor = { RustVersion::new(0, 999, 0), RustVersion::new(1, 0, 0) },
2223
)]
2324
fn ord(left: RustVersion, right: RustVersion) {
2425
let left = Stable { version: left };

crates/rust-toolchain/src/component.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::{Beta, Nightly, Stable};
21
use std::borrow::Cow;
32

43
/// A toolchain component
@@ -15,10 +14,12 @@ pub struct Component {
1514
}
1615

1716
impl Component {
17+
/// Create a new Component instance
1818
pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
1919
Self { name: name.into() }
2020
}
2121

22+
/// The name of the component
2223
pub fn name(&self) -> &str {
2324
self.name.as_ref()
2425
}

crates/rust-toolchain/src/date.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
use std::fmt;
22

3-
/// A release date for a Rust release.
3+
/// A release date for a Rust toolchain.
4+
///
5+
/// Nightly toolchains use a date as version instead of a semver number.
6+
/// This Date should be regarded as a form of a version number, just like semver.
7+
///
8+
/// For full-featured dates, it is recommended to use a dedicated library
9+
/// like [`time`], [`chrono`] or [`jiff`].
10+
///
11+
/// [`time`]: https://docs.rs/time/latest/time/
12+
/// [`chrono`]: https://docs.rs/chrono/latest/chrono/
13+
/// [`jiff`]:https://docs.rs/jiff/latest/jiff/
414
#[derive(Clone, Debug, Eq, Hash, PartialEq, Ord, PartialOrd)]
515
pub struct Date {
616
date: DateImpl,
@@ -24,6 +34,21 @@ impl Date {
2434
}
2535
}
2636

37+
/// The year
38+
pub fn year(&self) -> u16 {
39+
self.date.year
40+
}
41+
42+
/// The month
43+
pub fn month(&self) -> u8 {
44+
self.date.month
45+
}
46+
47+
/// The day
48+
pub fn day(&self) -> u8 {
49+
self.date.day
50+
}
51+
2752
/// Prints a yyyy-mm-dd representation of a release date.
2853
///
2954
/// This representation may, just like [`Date`], be not a valid date

crates/rust-toolchain/src/lib.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
//! # rust-toolchain
22
//!
3-
//! The `rust-toolchain` crate defines a set of types which describe a Rust toolchain.
3+
//! The [`rust-toolchain`] crate defines a set of types which model a Rust toolchain.
4+
//!
45
//! While there is no definitive spec which defines what a "Rust toolchain" is,
5-
//! we try to follow the official Rust release process as closely as possible.
6-
//! The [`rustup`] project has written down a rough specification for [`toolchains`] used
7-
//! by the Rust project. In the initial version, we will follow this spec, but disregard custom
8-
//! toolchains altogether, in the name of simplicity.
6+
//! it tries to follow official Rust sources as closely as possible.
7+
//!
8+
//! The project is currently primarily modelled around the rough [`toolchain`]
9+
//! specification written by the [`rustup`] developers. For now, we have disregarded
10+
//! custom toolchains altogether though, both in the name of simplicity, and because
11+
//! the current users didn't really need it yet. If you would like to see it added,
12+
//! please open an issue (preferably including your use case :)).
913
//!
1014
//! This project is part of the [`rust-releases`] and [`cargo-msrv`] projects.
11-
//! In case you have a feature request, question, bug, or have another reason to contact the developers,
12-
//! please, create a new issue at the `rust-releases` [`repository`].
1315
//!
16+
//! In case you have a feature request, question, bug, or have another reason to
17+
//! contact the developers, please create a new issue at the `rust-releases` [`repository`].
18+
//!
19+
//! [`rust-toolchain`]: https://docs.rs/rust-toolchain/latest/rust_toolchain/
1420
//! [`rustup`]: https://github.com/rust-lang/rustup
15-
//! [`toolchains`]: https://rust-lang.github.io/rustup/concepts/toolchains.html
21+
//! [`toolchain`]: https://rust-lang.github.io/rustup/concepts/toolchains.html
1622
//! [`rust-releases`]: https://github.com/foresterre/rust-releases
1723
//! [`cargo-msrv`]: https://github.com/foresterre/cargo-msrv
1824
//! [`repository`]: https://github.com/foresterre/rust-releases/issues
1925
// #![deny(missing_docs)]
2026
#![warn(clippy::all)]
2127
#![deny(unsafe_code)]
28+
#![deny(missing_docs)]
2229

23-
mod channel;
30+
/// Rust release channels, such as stable, beta and nightly.
31+
pub mod channel;
2432
mod component;
2533
mod date;
2634
mod target;
2735
mod toolchain;
2836
mod version;
2937

30-
pub use channel::{Channel, *};
38+
pub use channel::Channel;
3139
pub use component::Component;
3240
pub use date::Date;
3341
pub use target::Target;

crates/rust-toolchain/src/target.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use std::fmt;
12
use std::str::FromStr;
23

3-
/// A Rust toolchain
4+
/// A target platform
45
///
56
/// Commonly represented as a [`target triple`]. A target triple consists of three (or four) components: the
67
/// architecture component, the vendor component, the operating system component and optionally
@@ -11,22 +12,22 @@ use std::str::FromStr;
1112
/// - [`RFC 0131: target specification`]
1213
/// - [`rustup concepts: toolchains`]
1314
/// - [`rustup component history`]
15+
/// - [`rustc platform support`]
1416
///
1517
/// [`target triple`]: https://github.com/rust-lang/rfcs/blob/master/text/0131-target-specification.md#detailed-design
1618
/// [`RFC 0131: target specification`]: https://github.com/rust-lang/rfcs/blob/master/text/0131-target-specification.md#detailed-design
1719
/// [`rustup concepts: toolchains`]: https://rust-lang.github.io/rustup/concepts/toolchains.html
1820
/// [`rustup component history`]: https://rust-lang.github.io/rustup-components-history/
19-
// Extra information may be found here:
20-
// - https://doc.rust-lang.org/rustc/platform-support.html
21-
// - https://rust-lang.github.io/rustup/concepts/toolchains.html
21+
/// [`rustc platform support`]: https://doc.rust-lang.org/rustc/platform-support.html
2222
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
2323
pub struct Target {
2424
target: target_lexicon::Triple,
2525
}
2626

2727
impl Target {
28-
/// Create a new `Target` instance which represents the `host` platform on which the compiler
29-
/// runs.
28+
/// Create a new `Target` instance which represents the `host` platform.
29+
///
30+
/// The platform on which this library is compiled, will be the `host` platform.
3031
pub const fn host() -> Self {
3132
Self {
3233
target: target_lexicon::HOST,
@@ -58,6 +59,13 @@ impl Target {
5859
}
5960
}
6061

62+
impl fmt::Display for Target {
63+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64+
write!(f, "{}", self.target)
65+
}
66+
}
67+
68+
/// Errors which may occur while parsing a [`Target`].
6169
#[derive(Debug, thiserror::Error)]
6270
#[non_exhaustive]
6371
pub enum ParseError {
@@ -104,4 +112,11 @@ mod tests {
104112

105113
assert_eq!(this_platform, expected);
106114
}
115+
116+
#[test]
117+
fn to_string() {
118+
let target = Target::try_from_target_triple("x86_64-unknown-linux-gnu").unwrap();
119+
120+
assert_eq!(target.to_string(), "x86_64-unknown-linux-gnu");
121+
}
107122
}

0 commit comments

Comments
 (0)