Skip to main content

cargo_msrv/
msrv.rs

1use crate::rust::RustRelease;
2use crate::rust::Toolchain;
3
4/// An enum to represent the minimal compatibility
5#[derive(Clone, Debug, Eq, PartialEq)]
6pub enum MinimumSupportedRustVersion {
7    /// A toolchain is compatible, if the outcome of a toolchain check results in a success
8    Toolchain {
9        // toolchain
10        toolchain: Toolchain,
11    },
12    /// Compatibility is none, if the check on the last available toolchain fails
13    NoCompatibleToolchain,
14}
15
16impl MinimumSupportedRustVersion {
17    pub fn toolchain(msrv: &RustRelease) -> Self {
18        let toolchain = msrv.to_toolchain_spec().to_owned();
19
20        Self::Toolchain { toolchain }
21    }
22
23    pub fn from_option(msrv: Option<&RustRelease>) -> Self {
24        msrv.map_or(
25            MinimumSupportedRustVersion::NoCompatibleToolchain,
26            MinimumSupportedRustVersion::toolchain,
27        )
28    }
29}
30
31impl MinimumSupportedRustVersion {
32    #[cfg(test)]
33    pub fn unwrap_version(&self) -> rust_releases::semver::Version {
34        if let Self::Toolchain { toolchain, .. } = self {
35            return toolchain.version().clone();
36        }
37
38        panic!("Unable to unwrap MinimalCompatibility (CapableToolchain::version)")
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use crate::msrv::MinimumSupportedRustVersion;
45    use crate::rust::RustRelease;
46    use cargo_metadata::semver;
47
48    #[test]
49    fn accept() {
50        let version = semver::Version::new(1, 2, 3);
51        let rust_release = RustRelease::new(
52            rust_releases::Release::new_stable(version.clone()),
53            "x",
54            &[],
55        );
56        let msrv = MinimumSupportedRustVersion::toolchain(&rust_release);
57
58        assert!(matches!(
59            msrv,
60            MinimumSupportedRustVersion::Toolchain { toolchain } if toolchain.version() == &version && toolchain.target() == "x"));
61    }
62
63    #[test]
64    fn accept_from_option() {
65        let version = semver::Version::new(1, 2, 3);
66        let rust_release = RustRelease::new(
67            rust_releases::Release::new_stable(version.clone()),
68            "x",
69            &[],
70        );
71        let msrv = MinimumSupportedRustVersion::from_option(Some(&rust_release));
72
73        assert!(matches!(
74            msrv,
75            MinimumSupportedRustVersion::Toolchain { toolchain } if toolchain.version() == &version && toolchain.target() == "x"));
76    }
77
78    #[test]
79    fn reject_from_option() {
80        let msrv = MinimumSupportedRustVersion::from_option(None);
81
82        assert!(matches!(
83            msrv,
84            MinimumSupportedRustVersion::NoCompatibleToolchain
85        ));
86    }
87}