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
use std::{
    fmt::{self, Display, Formatter},
    str::FromStr,
};

#[derive(Debug, Error)]
pub enum CodenameParseError {
    #[error("unknown codename string")]
    NotFound,
}

/// The codename associated with an Ubuntu version.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Codename {
    Bionic,
    Cosmic,
    Disco,
    Eoan,
    Focal,
}

impl Codename {
    /// The date when this release is to be, or was, EOL'd.
    pub fn eol_date(self) -> (u32, u32, u32) {
        let (y, m, d) = self.release_date();
        
        if y % 2 == 0 && m == 4 {
            (y + 10, m, d)
        } else {
            (y + 1, if m == 4 { 1 } else { 7 }, d)
        }
    }

    /// Returns the release date in a `(year, month, date)` format
    pub fn release_date(self) -> (u32, u32, u32) {
        match self {
            Codename::Bionic => (2018, 4, 26),
            Codename::Cosmic => (2018, 10, 18),
            Codename::Disco => (2019, 4, 18),
            Codename::Eoan => (2019, 10, 17),
            // Approximate time for future release
            Codename::Focal => (2020, 4, 0),
        }
    }

    /// When this was released, as the time in seconds since the Unix Epoch
    pub fn release_timestamp(self) -> u64 {
        match self {
            Codename::Bionic => 1_524_700_800,
            Codename::Cosmic => 1_539_820_800,
            Codename::Disco => 1_555_545_600,
            Codename::Eoan => 1_571_270_400,
            // Approximate time for future release
            Codename::Focal => 1_585_699_200,
        }
    }
}

impl Display for Codename {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { fmt.write_str(<&'static str>::from(*self)) }
}

impl FromStr for Codename {
    type Err = CodenameParseError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        let release = match input {
            "bionic" => Codename::Bionic,
            "cosmic" => Codename::Cosmic,
            "disco" => Codename::Disco,
            "eoan" => Codename::Eoan,
            "focal" => Codename::Focal,
            _ => return Err(CodenameParseError::NotFound),
        };

        Ok(release)
    }
}

impl From<Codename> for &'static str {
    fn from(codename: Codename) -> Self {
        match codename {
            Codename::Bionic => "bionic",
            Codename::Cosmic => "cosmic",
            Codename::Disco => "disco",
            Codename::Eoan => "eoan",
            Codename::Focal => "focal",
        }
    }
}