codes_un_m49/kinds.rs
1/*!
2Provides a classifier for UN M49 regions.
3*/
4
5use codes_common::error::unknown_value;
6use std::{fmt::Display, str::FromStr};
7
8#[cfg(feature = "serde")]
9use serde::{Deserialize, Serialize};
10
11// ------------------------------------------------------------------------------------------------
12// Public Macros
13// ------------------------------------------------------------------------------------------------
14
15// ------------------------------------------------------------------------------------------------
16// Public Types
17// ------------------------------------------------------------------------------------------------
18
19///
20/// This enumeration is derived from the column headings in the M49 downloadable CSV file.
21///
22#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
23#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
24pub enum RegionKind {
25 Global,
26 Region,
27 SubRegion,
28 IntermediateRegion,
29 Country,
30 Area,
31}
32
33// ------------------------------------------------------------------------------------------------
34// Public Functions
35// ------------------------------------------------------------------------------------------------
36
37// ------------------------------------------------------------------------------------------------
38// Private Types
39// ------------------------------------------------------------------------------------------------
40
41// ------------------------------------------------------------------------------------------------
42// Implementations
43// ------------------------------------------------------------------------------------------------
44
45impl Display for RegionKind {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 write!(
48 f,
49 "{}",
50 match self {
51 Self::Global => "Global",
52 Self::Region => "Region",
53 Self::SubRegion => "SubRegion",
54 Self::IntermediateRegion => "IntermediateRegion",
55 Self::Country => "Country",
56 Self::Area => "Area",
57 }
58 )
59 }
60}
61
62impl FromStr for RegionKind {
63 type Err = super::RegionClassificationCodeError;
64
65 fn from_str(s: &str) -> Result<Self, Self::Err> {
66 match s {
67 "Global" => Ok(Self::Global),
68 "Region" => Ok(Self::Region),
69 "SubRegion" => Ok(Self::SubRegion),
70 "IntermediateRegion" => Ok(Self::IntermediateRegion),
71 "Country" => Ok(Self::Country),
72 "Area" => Ok(Self::Area),
73 _ => Err(unknown_value("RegionKind", s)),
74 }
75 }
76}
77
78// ------------------------------------------------------------------------------------------------
79// Private Functions
80// ------------------------------------------------------------------------------------------------
81
82// ------------------------------------------------------------------------------------------------
83// Modules
84// ------------------------------------------------------------------------------------------------