Skip to main content

pedant_core/resolution/rust/
edition.rs

1//! Cargo package editions as resolved from direct and workspace fields.
2
3use std::fmt;
4
5/// A Rust edition Cargo can assign to a package.
6#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub enum CargoEdition {
8    /// The original edition, and Cargo's default when `edition` is omitted.
9    #[default]
10    Rust2015,
11    /// The Rust 2018 edition.
12    Rust2018,
13    /// The Rust 2021 edition.
14    Rust2021,
15    /// The Rust 2024 edition.
16    Rust2024,
17}
18
19impl CargoEdition {
20    pub(super) fn parse(text: &str) -> Option<Self> {
21        match text {
22            "2015" => Some(Self::Rust2015),
23            "2018" => Some(Self::Rust2018),
24            "2021" => Some(Self::Rust2021),
25            "2024" => Some(Self::Rust2024),
26            _ => None,
27        }
28    }
29
30    pub(in crate::resolution::rust) fn permits_bare_callable_traits(self) -> bool {
31        matches!(self, Self::Rust2015 | Self::Rust2018)
32    }
33}
34
35impl fmt::Display for CargoEdition {
36    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
37        let text = match self {
38            Self::Rust2015 => "2015",
39            Self::Rust2018 => "2018",
40            Self::Rust2021 => "2021",
41            Self::Rust2024 => "2024",
42        };
43        formatter.write_str(text)
44    }
45}