1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use std::fmt;
use std::fmt::Display;

use serde_untagged::UntaggedEnumVisitor;

use crate::core::PartialVersion;
use crate::core::PartialVersionError;

#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, serde::Serialize)]
#[serde(transparent)]
pub struct RustVersion(PartialVersion);

impl RustVersion {
    pub fn is_compatible_with(&self, rustc: &PartialVersion) -> bool {
        let msrv = self.0.to_caret_req();
        // Remove any pre-release identifiers for easier comparison
        let rustc = semver::Version {
            major: rustc.major,
            minor: rustc.minor.unwrap_or_default(),
            patch: rustc.patch.unwrap_or_default(),
            pre: Default::default(),
            build: Default::default(),
        };
        msrv.matches(&rustc)
    }

    pub fn into_partial(self) -> PartialVersion {
        self.0
    }

    pub fn as_partial(&self) -> &PartialVersion {
        &self.0
    }
}

impl std::str::FromStr for RustVersion {
    type Err = RustVersionError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let partial = value.parse::<PartialVersion>();
        let partial = partial.map_err(RustVersionErrorKind::PartialVersion)?;
        partial.try_into()
    }
}

impl TryFrom<semver::Version> for RustVersion {
    type Error = RustVersionError;

    fn try_from(version: semver::Version) -> Result<Self, Self::Error> {
        let version = PartialVersion::from(version);
        Self::try_from(version)
    }
}

impl TryFrom<PartialVersion> for RustVersion {
    type Error = RustVersionError;

    fn try_from(partial: PartialVersion) -> Result<Self, Self::Error> {
        if partial.pre.is_some() {
            return Err(RustVersionErrorKind::Prerelease.into());
        }
        if partial.build.is_some() {
            return Err(RustVersionErrorKind::BuildMetadata.into());
        }
        Ok(Self(partial))
    }
}

impl<'de> serde::Deserialize<'de> for RustVersion {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        UntaggedEnumVisitor::new()
            .expecting("SemVer version")
            .string(|value| value.parse().map_err(serde::de::Error::custom))
            .deserialize(deserializer)
    }
}

impl Display for RustVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// Error parsing a [`RustVersion`].
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct RustVersionError(#[from] RustVersionErrorKind);

/// Non-public error kind for [`RustVersionError`].
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
enum RustVersionErrorKind {
    #[error("unexpected prerelease field, expected a version like \"1.32\"")]
    Prerelease,

    #[error("unexpected build field, expected a version like \"1.32\"")]
    BuildMetadata,

    #[error(transparent)]
    PartialVersion(#[from] PartialVersionError),
}

#[cfg(test)]
mod test {
    use super::*;
    use snapbox::prelude::*;
    use snapbox::str;

    #[test]
    fn is_compatible_with_rustc() {
        let cases = &[
            ("1", "1.70.0", true),
            ("1.30", "1.70.0", true),
            ("1.30.10", "1.70.0", true),
            ("1.70", "1.70.0", true),
            ("1.70.0", "1.70.0", true),
            ("1.70.1", "1.70.0", false),
            ("1.70", "1.70.0-nightly", true),
            ("1.70.0", "1.70.0-nightly", true),
            ("1.71", "1.70.0", false),
            ("2", "1.70.0", false),
        ];
        let mut passed = true;
        for (msrv, rustc, expected) in cases {
            let msrv: RustVersion = msrv.parse().unwrap();
            let rustc = PartialVersion::from(semver::Version::parse(rustc).unwrap());
            if msrv.is_compatible_with(&rustc) != *expected {
                println!("failed: {msrv} is_compatible_with {rustc} == {expected}");
                passed = false;
            }
        }
        assert!(passed);
    }

    #[test]
    fn is_compatible_with_workspace_msrv() {
        let cases = &[
            ("1", "1", true),
            ("1", "1.70", true),
            ("1", "1.70.0", true),
            ("1.30", "1", false),
            ("1.30", "1.70", true),
            ("1.30", "1.70.0", true),
            ("1.30.10", "1", false),
            ("1.30.10", "1.70", true),
            ("1.30.10", "1.70.0", true),
            ("1.70", "1", false),
            ("1.70", "1.70", true),
            ("1.70", "1.70.0", true),
            ("1.70.0", "1", false),
            ("1.70.0", "1.70", true),
            ("1.70.0", "1.70.0", true),
            ("1.70.1", "1", false),
            ("1.70.1", "1.70", false),
            ("1.70.1", "1.70.0", false),
            ("1.71", "1", false),
            ("1.71", "1.70", false),
            ("1.71", "1.70.0", false),
            ("2", "1.70.0", false),
        ];
        let mut passed = true;
        for (dep_msrv, ws_msrv, expected) in cases {
            let dep_msrv: RustVersion = dep_msrv.parse().unwrap();
            let ws_msrv = ws_msrv.parse::<RustVersion>().unwrap().into_partial();
            if dep_msrv.is_compatible_with(&ws_msrv) != *expected {
                println!("failed: {dep_msrv} is_compatible_with {ws_msrv} == {expected}");
                passed = false;
            }
        }
        assert!(passed);
    }

    #[test]
    fn parse_errors() {
        let cases = &[
            // Disallow caret
            (
                "^1.43",
                str![[r#"unexpected version requirement, expected a version like "1.32""#]],
            ),
            // Valid pre-release
            (
                "1.43.0-beta.1",
                str![[r#"unexpected prerelease field, expected a version like "1.32""#]],
            ),
            // Bad pre-release
            (
                "1.43-beta.1",
                str![[r#"unexpected prerelease field, expected a version like "1.32""#]],
            ),
            // Weird wildcard
            (
                "x",
                str![[r#"unexpected version requirement, expected a version like "1.32""#]],
            ),
            (
                "1.x",
                str![[r#"unexpected version requirement, expected a version like "1.32""#]],
            ),
            (
                "1.1.x",
                str![[r#"unexpected version requirement, expected a version like "1.32""#]],
            ),
            // Non-sense
            ("foodaddle", str![[r#"expected a version like "1.32""#]]),
        ];
        for (input, expected) in cases {
            let actual: Result<RustVersion, _> = input.parse();
            let actual = match actual {
                Ok(result) => format!("didn't fail: {result:?}"),
                Err(err) => err.to_string(),
            };
            snapbox::assert_data_eq!(actual, expected.clone().raw());
        }
    }
}