edition/
lib.rs

1//! The edition of the Rust language used in a crate.
2// This should live in a separate crate because we use it in both actual code and codegen.
3use std::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6#[repr(u8)]
7pub enum Edition {
8    // The syntax context stuff needs the discriminants to start from 0 and be consecutive.
9    Edition2015 = 0,
10    Edition2018,
11    Edition2021,
12    Edition2024,
13}
14
15impl Edition {
16    pub const DEFAULT: Edition = Edition::Edition2015;
17    pub const LATEST: Edition = Edition::Edition2024;
18    pub const CURRENT: Edition = Edition::Edition2024;
19    /// The current latest stable edition, note this is usually not the right choice in code.
20    pub const CURRENT_FIXME: Edition = Edition::Edition2024;
21
22    pub fn from_u32(u32: u32) -> Edition {
23        match u32 {
24            0 => Edition::Edition2015,
25            1 => Edition::Edition2018,
26            2 => Edition::Edition2021,
27            3 => Edition::Edition2024,
28            _ => panic!("invalid edition"),
29        }
30    }
31
32    pub fn at_least_2024(self) -> bool {
33        self >= Edition::Edition2024
34    }
35
36    pub fn at_least_2021(self) -> bool {
37        self >= Edition::Edition2021
38    }
39
40    pub fn at_least_2018(self) -> bool {
41        self >= Edition::Edition2018
42    }
43
44    pub fn number(&self) -> usize {
45        match self {
46            Edition::Edition2015 => 2015,
47            Edition::Edition2018 => 2018,
48            Edition::Edition2021 => 2021,
49            Edition::Edition2024 => 2024,
50        }
51    }
52
53    pub fn iter() -> impl Iterator<Item = Edition> {
54        [Edition::Edition2015, Edition::Edition2018, Edition::Edition2021, Edition::Edition2024]
55            .iter()
56            .copied()
57    }
58}
59
60#[derive(Debug)]
61pub struct ParseEditionError {
62    invalid_input: String,
63}
64
65impl std::error::Error for ParseEditionError {}
66impl fmt::Display for ParseEditionError {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        write!(f, "invalid edition: {:?}", self.invalid_input)
69    }
70}
71
72impl std::str::FromStr for Edition {
73    type Err = ParseEditionError;
74
75    fn from_str(s: &str) -> Result<Self, Self::Err> {
76        let res = match s {
77            "2015" => Edition::Edition2015,
78            "2018" => Edition::Edition2018,
79            "2021" => Edition::Edition2021,
80            "2024" => Edition::Edition2024,
81            _ => return Err(ParseEditionError { invalid_input: s.to_owned() }),
82        };
83        Ok(res)
84    }
85}
86
87impl fmt::Display for Edition {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        f.write_str(match self {
90            Edition::Edition2015 => "2015",
91            Edition::Edition2018 => "2018",
92            Edition::Edition2021 => "2021",
93            Edition::Edition2024 => "2024",
94        })
95    }
96}