Skip to main content

compiletest/
edition.rs

1use std::str::FromStr;
2
3use crate::fatal;
4
5#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
6pub(crate) enum Edition {
7    // Note that the ordering here is load-bearing, as we want the future edition to be greater than
8    // any year-based edition.
9    Year(u32),
10    Future,
11}
12
13impl std::fmt::Display for Edition {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        match self {
16            Edition::Year(year) => write!(f, "{year}"),
17            Edition::Future => f.write_str("future"),
18        }
19    }
20}
21
22impl From<u32> for Edition {
23    fn from(value: u32) -> Self {
24        Edition::Year(value)
25    }
26}
27
28impl FromStr for Edition {
29    type Err = String;
30
31    fn from_str(input: &str) -> Result<Self, Self::Err> {
32        let input = input.trim();
33        if input == "future" {
34            Ok(Edition::Future)
35        } else {
36            let year: u32 =
37                input.parse().map_err(|v| format!("{input} is not a valid edition year: {v}"))?;
38            Ok(Edition::Year(year))
39        }
40    }
41}
42
43pub(crate) fn parse_edition(input: &str) -> Edition {
44    input.parse().unwrap_or_else(|_| fatal!("`{input}` doesn't look like an edition"))
45}