use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Country {
#[serde(rename = "DE")]
Germany,
#[serde(rename = "AT")]
Austria,
#[serde(rename = "CH")]
Switzerland,
#[serde(rename = "NL")]
Netherlands,
#[serde(rename = "BE")]
Belgium,
#[serde(rename = "FR")]
France,
#[serde(rename = "LU")]
Luxembourg,
#[serde(rename = "PL")]
Poland,
#[serde(rename = "CZ")]
CzechRepublic,
#[serde(rename = "DK")]
Denmark,
#[serde(rename = "IT")]
Italy,
#[serde(rename = "ES")]
Spain,
#[serde(rename = "GB")]
UnitedKingdom,
#[serde(rename = "SE")]
Sweden,
#[serde(rename = "NO")]
Norway,
#[serde(rename = "FI")]
Finland,
#[serde(rename = "PT")]
Portugal,
#[serde(rename = "GR")]
Greece,
#[serde(rename = "IE")]
Ireland,
#[serde(rename = "HU")]
Hungary,
#[serde(rename = "SK")]
Slovakia,
#[serde(rename = "SI")]
Slovenia,
#[serde(rename = "HR")]
Croatia,
#[serde(rename = "RO")]
Romania,
#[serde(rename = "BG")]
Bulgaria,
#[serde(rename = "EE")]
Estonia,
#[serde(rename = "LV")]
Latvia,
#[serde(rename = "LT")]
Lithuania,
#[serde(rename = "CY")]
Cyprus,
#[serde(rename = "MT")]
Malta,
#[serde(rename = "LI")]
Liechtenstein,
#[serde(rename = "IS")]
Iceland,
}
impl Country {
pub fn german_name(&self) -> &'static str {
match self {
Self::Germany => "Deutschland",
Self::Austria => "Oesterreich",
Self::Switzerland => "Schweiz",
Self::Netherlands => "Niederlande",
Self::Belgium => "Belgien",
Self::France => "Frankreich",
Self::Luxembourg => "Luxemburg",
Self::Poland => "Polen",
Self::CzechRepublic => "Tschechien",
Self::Denmark => "Daenemark",
Self::Italy => "Italien",
Self::Spain => "Spanien",
Self::UnitedKingdom => "Vereinigtes Koenigreich",
Self::Sweden => "Schweden",
Self::Norway => "Norwegen",
Self::Finland => "Finnland",
Self::Portugal => "Portugal",
Self::Greece => "Griechenland",
Self::Ireland => "Irland",
Self::Hungary => "Ungarn",
Self::Slovakia => "Slowakei",
Self::Slovenia => "Slowenien",
Self::Croatia => "Kroatien",
Self::Romania => "Rumaenien",
Self::Bulgaria => "Bulgarien",
Self::Estonia => "Estland",
Self::Latvia => "Lettland",
Self::Lithuania => "Litauen",
Self::Cyprus => "Zypern",
Self::Malta => "Malta",
Self::Liechtenstein => "Liechtenstein",
Self::Iceland => "Island",
}
}
pub fn alpha2_code(&self) -> &'static str {
match self {
Self::Germany => "DE",
Self::Austria => "AT",
Self::Switzerland => "CH",
Self::Netherlands => "NL",
Self::Belgium => "BE",
Self::France => "FR",
Self::Luxembourg => "LU",
Self::Poland => "PL",
Self::CzechRepublic => "CZ",
Self::Denmark => "DK",
Self::Italy => "IT",
Self::Spain => "ES",
Self::UnitedKingdom => "GB",
Self::Sweden => "SE",
Self::Norway => "NO",
Self::Finland => "FI",
Self::Portugal => "PT",
Self::Greece => "GR",
Self::Ireland => "IE",
Self::Hungary => "HU",
Self::Slovakia => "SK",
Self::Slovenia => "SI",
Self::Croatia => "HR",
Self::Romania => "RO",
Self::Bulgaria => "BG",
Self::Estonia => "EE",
Self::Latvia => "LV",
Self::Lithuania => "LT",
Self::Cyprus => "CY",
Self::Malta => "MT",
Self::Liechtenstein => "LI",
Self::Iceland => "IS",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialize() {
assert_eq!(serde_json::to_string(&Country::Germany).unwrap(), r#""DE""#);
assert_eq!(serde_json::to_string(&Country::Austria).unwrap(), r#""AT""#);
}
#[test]
fn test_deserialize() {
assert_eq!(
serde_json::from_str::<Country>(r#""DE""#).unwrap(),
Country::Germany
);
assert_eq!(
serde_json::from_str::<Country>(r#""CH""#).unwrap(),
Country::Switzerland
);
}
#[test]
fn test_roundtrip() {
for country in [
Country::Germany,
Country::Austria,
Country::Switzerland,
Country::Netherlands,
Country::Belgium,
Country::France,
Country::Luxembourg,
Country::Poland,
] {
let json = serde_json::to_string(&country).unwrap();
let parsed: Country = serde_json::from_str(&json).unwrap();
assert_eq!(country, parsed);
}
}
#[test]
fn test_alpha2_code() {
assert_eq!(Country::Germany.alpha2_code(), "DE");
assert_eq!(Country::Austria.alpha2_code(), "AT");
}
}