locode 0.2.0

Standardized country and city codes
Documentation
macro_rules! mapping {
    ($($city_code:ident $country:ident => $city:expr, $time_zone:expr;)+) => {
        #[derive(Debug, Clone, Copy)]
        /// IATA city codes mapping.
        pub enum City {
            $(
                $city_code,
            )+
        }

        impl City {
            /// Returns a slice of all IATA city codes.
            pub const fn all() -> &'static [Self] {
                &[
                    $(
                        Self::$city_code,
                    )+
                ]
            }

            #[cfg(feature = "feat-iata-city-from-code")]
            /// Returns the IATA code from the city code.
            pub fn from_code(code: &str) -> Option<Self> {
                use std::sync::OnceLock;
                use std::collections::HashMap;

                static MAPPING: OnceLock<HashMap<&'static str, City, foldhash::fast::RandomState>> = OnceLock::new();

                MAPPING.get_or_init(|| {
                        City::all()
                            .iter()
                            .map(|city| (city.code(), *city))
                            .collect()
                    })
                    .get(code)
                    .copied()
            }

            /// Returns the IATA code.
            pub const fn code(self) -> &'static str {
                use City::*;

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

            #[cfg(feature = "feat-iata-city-time-zone")]
            /// Returns the time zone.
            pub const fn time_zone(self) -> &'static str {
                use City::*;

                match self {
                    $(
                        $city_code => $time_zone,
                    )+
                }
            }

            #[cfg(feature = "feat-iata-city-country")]
            /// Returns the country.
            pub const fn country(self) -> &'static str {
                use City::*;

                match self {
                    $(
                        $city_code => stringify!($country),
                    )+
                }
            }

            #[cfg(feature = "feat-iata-city-name")]
            /// Returns the city name.
            pub const fn name(self) -> Option<&'static str> {
                use City::*;

                match self {
                    $(
                        $city_code => $city,
                    )+
                }
            }
        }
    }
}