ra_ap_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::Edition2021;
19    /// The current latest stable edition, note this is usually not the right choice in code.
20    pub const CURRENT_FIXME: Edition = Edition::Edition2021;
21
22    pub fn at_least_2024(self) -> bool {
23        self >= Edition::Edition2024
24    }
25
26    pub fn at_least_2021(self) -> bool {
27        self >= Edition::Edition2021
28    }
29
30    pub fn at_least_2018(self) -> bool {
31        self >= Edition::Edition2018
32    }
33
34    pub fn iter() -> impl Iterator<Item = Edition> {
35        [Edition::Edition2015, Edition::Edition2018, Edition::Edition2021, Edition::Edition2024]
36            .iter()
37            .copied()
38    }
39}
40
41#[derive(Debug)]
42pub struct ParseEditionError {
43    invalid_input: String,
44}
45
46impl std::error::Error for ParseEditionError {}
47impl fmt::Display for ParseEditionError {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        write!(f, "invalid edition: {:?}", self.invalid_input)
50    }
51}
52
53impl std::str::FromStr for Edition {
54    type Err = ParseEditionError;
55
56    fn from_str(s: &str) -> Result<Self, Self::Err> {
57        let res = match s {
58            "2015" => Edition::Edition2015,
59            "2018" => Edition::Edition2018,
60            "2021" => Edition::Edition2021,
61            "2024" => Edition::Edition2024,
62            _ => return Err(ParseEditionError { invalid_input: s.to_owned() }),
63        };
64        Ok(res)
65    }
66}
67
68impl fmt::Display for Edition {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.write_str(match self {
71            Edition::Edition2015 => "2015",
72            Edition::Edition2018 => "2018",
73            Edition::Edition2021 => "2021",
74            Edition::Edition2024 => "2024",
75        })
76    }
77}