locode 0.2.0

Standardized country and city codes
Documentation
macro_rules! mapping {
    ($($alpha_2_code:ident $alpha_3_code:ident => $numeric_code:expr, $short_name_en:expr, $short_name_uppercase_en:expr, $full_name_en:expr, $short_name_zh:expr;)+) => {
        #[derive(Debug, Clone, Copy)]
        #[repr(u16)]
        /// ISO 3166-1 country codes mapping.
        ///
        /// The Chinese names of countries and regions are determined according to GB/T 2659-2000 (equivalent to ISO 3166-1:1997).
        /// For those not included in GB/T 2659-2000 (i.e., countries and regional entries newly added in ISO 3166.1-2006),
        /// Chinese names are given according to conventional usage.
        pub enum Iso3166 {
            $(
                $alpha_3_code = $numeric_code,
            )+
        }

        impl Iso3166 {
            /// Returns a slice of all ISO 3166-1 country codes.
            pub const fn all() -> &'static [Self] {
                &[
                    $(
                        Self::$alpha_3_code,
                    )+
                ]
            }

            /// Returns the ISO 3166-1 country code from the alpha-2 code.
            pub fn from_alpha_2_code(code: &str) -> Option<Self> {
                match code {
                    $(
                        _ if stringify!($alpha_2_code).eq_ignore_ascii_case(code) => Some(Self::$alpha_3_code),
                    )+
                    _ => None,
                }
            }

            /// Returns the ISO 3166-1 country code from the alpha-3 code.
            pub fn from_alpha_3_code(code: &str) -> Option<Self> {
                match code {
                    $(
                        _ if stringify!($alpha_3_code).eq_ignore_ascii_case(code) => Some(Self::$alpha_3_code),
                    )+
                    _ => None,
                }
            }

            #[inline]
            /// Returns the ISO 3166-1 country code from the numeric code.
            pub const fn from_numeric_code(code: u16) -> Option<Self> {
                match code {
                    $(
                        $numeric_code => Some(Self::$alpha_3_code),
                    )+
                    _ => None,
                }
            }

            /// Returns the ISO 3166-1 alpha-2 code.
            pub const fn alpha_2_code(self) -> &'static str {
                use Iso3166::*;

                match self {
                    $(
                        $alpha_3_code => stringify!($alpha_2_code),
                    )+
                }
            }

            /// Returns the ISO 3166-1 alpha-3 code.
            pub const fn alpha_3_code(self) -> &'static str {
                use Iso3166::*;

                match self {
                    $(
                        $alpha_3_code => stringify!($alpha_2_code),
                    )+
                }
            }

            #[inline]
            /// Returns the ISO 3166-1 numeric code.
            pub const fn numeric_code(self) -> u16 {
                self as u16
            }

            #[cfg(feature = "feat-iso3166-short-name-en")]
            /// Returns the short name in English.
            pub const fn short_name_en(self) -> Option<&'static str> {
                use Iso3166::*;

                match self {
                    $(
                        $alpha_3_code => $short_name_en,
                    )+
                }
            }

            #[cfg(feature = "feat-iso3166-short-name-uppercase-en")]
            /// Returns the short name in uppercase English.
            pub const fn short_name_uppercase_en(self) -> Option<&'static str> {
                use Iso3166::*;

                match self {
                    $(
                        $alpha_3_code => $short_name_uppercase_en,
                    )+
                }
            }

            #[cfg(feature = "feat-iso3166-full-name-en")]
            /// Returns the full name in English.
            pub const fn full_name_en(self) -> Option<&'static str> {
                use Iso3166::*;

                match self {
                    $(
                        $alpha_3_code => $full_name_en,
                    )+
                }
            }

            #[cfg(feature = "feat-iso3166-short-name-zh")]
            /// Returns the short name in Chinese.
            pub const fn short_name_zh(self) -> Option<&'static str> {
                use Iso3166::*;

                match self {
                    $(
                        $alpha_3_code => $short_name_zh,
                    )+
                }
            }
        }
    }
}