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

/// All the possible countries in which the application can run.
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum Country {
    Common,
    Es,
    It,
    Uk,
}

impl FromStr for Country {
    type Err = CountryParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "common" => Ok(Self::Common),
            "it" => Ok(Self::It),
            "es" => Ok(Self::Es),
            "uk" => Ok(Self::Uk),
            _ => Err(CountryParseError(s.to_string())),
        }
    }
}

impl Display for Country {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let country = match self {
            Self::Common => "common",
            Self::Es => "es",
            Self::It => "it",
            Self::Uk => "uk",
        };
        f.write_str(country)
    }
}

#[derive(Debug)]
pub struct CountryParseError(String);

impl Display for CountryParseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!(
            "{} is not a valid country string. Allowed strings are 'common', 'it', 'es' and 'uk'.",
            &self.0
        ))
    }
}