#![warn(missing_docs)]
use crate::types::Place;
use chrono::{Offset, TimeZone, Utc};
use chrono_tz::Tz;
use rustc_hash::FxHashMap;
use std::str::FromStr;
use std::sync::LazyLock;
static COUNTRY_NAMES: LazyLock<FxHashMap<&'static str, &'static str>> = LazyLock::new(|| {
[
("AF", "Afghanistan"),
("AL", "Albania"),
("DZ", "Algeria"),
("AS", "American Samoa"),
("AD", "Andorra"),
("AO", "Angola"),
("AI", "Anguilla"),
("AQ", "Antarctica"),
("AG", "Antigua and Barbuda"),
("AR", "Argentina"),
("AM", "Armenia"),
("AW", "Aruba"),
("AU", "Australia"),
("AT", "Austria"),
("AZ", "Azerbaijan"),
("BS", "Bahamas"),
("BH", "Bahrain"),
("BD", "Bangladesh"),
("BB", "Barbados"),
("BY", "Belarus"),
("BE", "Belgium"),
("BZ", "Belize"),
("BJ", "Benin"),
("BM", "Bermuda"),
("BT", "Bhutan"),
("BO", "Bolivia"),
("BA", "Bosnia and Herzegovina"),
("BW", "Botswana"),
("BV", "Bouvet Island"),
("BR", "Brazil"),
("IO", "British Indian Ocean Territory"),
("VG", "British Virgin Islands"),
("BN", "Brunei"),
("BG", "Bulgaria"),
("BF", "Burkina Faso"),
("BI", "Burundi"),
("KH", "Cambodia"),
("CM", "Cameroon"),
("CA", "Canada"),
("CV", "Cape Verde"),
("KY", "Cayman Islands"),
("CF", "Central African Republic"),
("TD", "Chad"),
("CL", "Chile"),
("CN", "China"),
("CX", "Christmas Island"),
("CC", "Cocos (Keeling) Islands"),
("CO", "Colombia"),
("KM", "Comoros"),
("CK", "Cook Islands"),
("CR", "Costa Rica"),
("HR", "Croatia"),
("CU", "Cuba"),
("CY", "Cyprus"),
("CZ", "Czech Republic"),
("CD", "Democratic Republic of the Congo"),
("DK", "Denmark"),
("DJ", "Djibouti"),
("DM", "Dominica"),
("DO", "Dominican Republic"),
("TL", "East Timor"),
("EC", "Ecuador"),
("EG", "Egypt"),
("SV", "El Salvador"),
("GQ", "Equatorial Guinea"),
("ER", "Eritrea"),
("EE", "Estonia"),
("ET", "Ethiopia"),
("FK", "Falkland Islands"),
("FO", "Faroe Islands"),
("FJ", "Fiji"),
("FI", "Finland"),
("FR", "France"),
("GF", "French Guiana"),
("PF", "French Polynesia"),
("TF", "French Southern Territories"),
("GA", "Gabon"),
("GM", "Gambia"),
("GE", "Georgia"),
("DE", "Germany"),
("GH", "Ghana"),
("GI", "Gibraltar"),
("GR", "Greece"),
("GL", "Greenland"),
("GD", "Grenada"),
("GP", "Guadeloupe"),
("GU", "Guam"),
("GT", "Guatemala"),
("GN", "Guinea"),
("GW", "Guinea-Bissau"),
("GY", "Guyana"),
("HT", "Haiti"),
("HM", "Heard Island and McDonald Islands"),
("HN", "Honduras"),
("HK", "Hong Kong"),
("HU", "Hungary"),
("IS", "Iceland"),
("IN", "India"),
("ID", "Indonesia"),
("IR", "Iran"),
("IQ", "Iraq"),
("IE", "Ireland"),
("IL", "Israel"),
("IT", "Italy"),
("CI", "Ivory Coast"),
("JM", "Jamaica"),
("JP", "Japan"),
("JO", "Jordan"),
("KZ", "Kazakhstan"),
("KE", "Kenya"),
("KI", "Kiribati"),
("KW", "Kuwait"),
("KG", "Kyrgyzstan"),
("LA", "Laos"),
("LV", "Latvia"),
("LB", "Lebanon"),
("LS", "Lesotho"),
("LR", "Liberia"),
("LY", "Libya"),
("LI", "Liechtenstein"),
("LT", "Lithuania"),
("LU", "Luxembourg"),
("MO", "Macau"),
("MK", "Macedonia"),
("MG", "Madagascar"),
("MW", "Malawi"),
("MY", "Malaysia"),
("MV", "Maldives"),
("ML", "Mali"),
("MT", "Malta"),
("MH", "Marshall Islands"),
("MQ", "Martinique"),
("MR", "Mauritania"),
("MU", "Mauritius"),
("YT", "Mayotte"),
("MX", "Mexico"),
("FM", "Micronesia"),
("MD", "Moldova"),
("MC", "Monaco"),
("MN", "Mongolia"),
("ME", "Montenegro"),
("MS", "Montserrat"),
("MA", "Morocco"),
("MZ", "Mozambique"),
("MM", "Myanmar"),
("NA", "Namibia"),
("NR", "Nauru"),
("NP", "Nepal"),
("NL", "Netherlands"),
("AN", "Netherlands Antilles"),
("NC", "New Caledonia"),
("NZ", "New Zealand"),
("NI", "Nicaragua"),
("NE", "Niger"),
("NG", "Nigeria"),
("NU", "Niue"),
("NF", "Norfolk Island"),
("KP", "North Korea"),
("MP", "Northern Mariana Islands"),
("NO", "Norway"),
("OM", "Oman"),
("PK", "Pakistan"),
("PW", "Palau"),
("PS", "Palestinian Territory"),
("PA", "Panama"),
("PG", "Papua New Guinea"),
("PY", "Paraguay"),
("PE", "Peru"),
("PH", "Philippines"),
("PN", "Pitcairn"),
("PL", "Poland"),
("PT", "Portugal"),
("PR", "Puerto Rico"),
("QA", "Qatar"),
("CG", "Republic of the Congo"),
("RE", "Reunion"),
("RO", "Romania"),
("RU", "Russia"),
("RW", "Rwanda"),
("SH", "Saint Helena"),
("KN", "Saint Kitts and Nevis"),
("LC", "Saint Lucia"),
("PM", "Saint Pierre and Miquelon"),
("VC", "Saint Vincent and the Grenadines"),
("WS", "Samoa"),
("SM", "San Marino"),
("ST", "São Tomé and PrÃncipe"),
("SA", "Saudi Arabia"),
("SN", "Senegal"),
("RS", "Serbia"),
("CS", "Serbia and Montenegro"),
("SC", "Seychelles"),
("SL", "Sierra Leone"),
("SG", "Singapore"),
("SK", "Slovakia"),
("SI", "Slovenia"),
("SB", "Solomon Islands"),
("SO", "Somalia"),
("ZA", "South Africa"),
("GS", "South Georgia and the South Sandwich Islands"),
("KR", "South Korea"),
("ES", "Spain"),
("LK", "Sri Lanka"),
("SD", "Sudan"),
("SR", "Suriname"),
("SJ", "Svalbard and Jan Mayen"),
("SZ", "Swaziland"),
("SE", "Sweden"),
("CH", "Switzerland"),
("SY", "Syria"),
("TW", "Taiwan"),
("TJ", "Tajikistan"),
("TZ", "Tanzania"),
("TH", "Thailand"),
("TG", "Togo"),
("TK", "Tokelau"),
("TO", "Tonga"),
("TT", "Trinidad and Tobago"),
("TN", "Tunisia"),
("TR", "Turkey"),
("TM", "Turkmenistan"),
("TC", "Turks and Caicos Islands"),
("TV", "Tuvalu"),
("VI", "U.S. Virgin Islands"),
("UG", "Uganda"),
("UA", "Ukraine"),
("AE", "United Arab Emirates"),
("GB", "United Kingdom"),
("US", "United States"),
("UM", "United States Minor Outlying Islands"),
("UY", "Uruguay"),
("UZ", "Uzbekistan"),
("VU", "Vanuatu"),
("VA", "Vatican City"),
("VE", "Venezuela"),
("VN", "Vietnam"),
("WF", "Wallis and Futuna"),
("EH", "Western Sahara"),
("YE", "Yemen"),
("ZM", "Zambia"),
("ZW", "Zimbabwe"),
]
.into_iter()
.collect()
});
static COUNTRY_CURRENCIES: LazyLock<FxHashMap<&'static str, &'static str>> = LazyLock::new(|| {
[
("AF", "AFN"),
("AL", "ALL"),
("DZ", "DZD"),
("AS", "USD"),
("AD", "EUR"),
("AO", "AOA"),
("AI", "XCD"),
("AQ", "USD"),
("AG", "XCD"),
("AR", "ARS"),
("AM", "AMD"),
("AW", "AWG"),
("AU", "AUD"),
("AT", "EUR"),
("AZ", "AZN"),
("BS", "BSD"),
("BH", "BHD"),
("BD", "BDT"),
("BB", "BBD"),
("BY", "BYR"),
("BE", "EUR"),
("BZ", "BZD"),
("BJ", "XOF"),
("BM", "BMD"),
("BT", "BTN"),
("BO", "BOB"),
("BA", "BAM"),
("BW", "BWP"),
("BV", "NOK"),
("BR", "BRL"),
("IO", "USD"),
("VG", "USD"),
("BN", "BND"),
("BG", "BGN"),
("BF", "XOF"),
("BI", "BIF"),
("KH", "KHR"),
("CM", "XAF"),
("CA", "CAD"),
("CV", "CVE"),
("KY", "KYD"),
("CF", "XAF"),
("TD", "XAF"),
("CL", "CLP"),
("CN", "CNY"),
("CX", "AUD"),
("CC", "AUD"),
("CO", "COP"),
("KM", "KMF"),
("CK", "NZD"),
("CR", "CRC"),
("HR", "HRK"),
("CU", "CUP"),
("CY", "EUR"),
("CZ", "CZK"),
("CD", "CDF"),
("DK", "DKK"),
("DJ", "DJF"),
("DM", "XCD"),
("DO", "DOP"),
("TL", "USD"),
("EC", "USD"),
("EG", "EGP"),
("SV", "SVC"),
("GQ", "XAF"),
("ER", "ERN"),
("EE", "EUR"),
("ET", "ETB"),
("FK", "FKP"),
("FO", "DKK"),
("FJ", "FJD"),
("FI", "EUR"),
("FR", "EUR"),
("GF", "EUR"),
("PF", "XPF"),
("TF", "EUR"),
("GA", "XAF"),
("GM", "GMD"),
("GE", "GEL"),
("DE", "EUR"),
("GH", "GHS"),
("GI", "GIP"),
("GR", "EUR"),
("GL", "DKK"),
("GD", "XCD"),
("GP", "EUR"),
("GU", "USD"),
("GT", "GTQ"),
("GN", "GNF"),
("GW", "XOF"),
("GY", "GYD"),
("HT", "HTG"),
("HM", "AUD"),
("HN", "HNL"),
("HK", "HKD"),
("HU", "HUF"),
("IS", "ISK"),
("IN", "INR"),
("ID", "IDR"),
("IR", "IRR"),
("IQ", "IQD"),
("IE", "EUR"),
("IL", "ILS"),
("IT", "EUR"),
("CI", "XOF"),
("JM", "JMD"),
("JP", "JPY"),
("JO", "JOD"),
("KZ", "KZT"),
("KE", "KES"),
("KI", "AUD"),
("KW", "KWD"),
("KG", "KGS"),
("LA", "LAK"),
("LV", "EUR"),
("LB", "LBP"),
("LS", "LSL"),
("LR", "LRD"),
("LY", "LYD"),
("LI", "CHF"),
("LT", "EUR"),
("LU", "EUR"),
("MO", "MOP"),
("MK", "MKD"),
("MG", "MGA"),
("MW", "MWK"),
("MY", "MYR"),
("MV", "MVR"),
("ML", "XOF"),
("MT", "EUR"),
("MH", "USD"),
("MQ", "EUR"),
("MR", "MRU"),
("MU", "MUR"),
("YT", "EUR"),
("MX", "MXN"),
("FM", "USD"),
("MD", "MDL"),
("MC", "EUR"),
("MN", "MNT"),
("MS", "XCD"),
("MA", "MAD"),
("MZ", "MZN"),
("MM", "MMK"),
("NA", "NAD"),
("NR", "AUD"),
("NP", "NPR"),
("NL", "EUR"),
("AN", "ANG"),
("NC", "XPF"),
("NZ", "NZD"),
("NI", "NIO"),
("NE", "XOF"),
("NG", "NGN"),
("NU", "NZD"),
("NF", "AUD"),
("KP", "KPW"),
("MP", "USD"),
("NO", "NOK"),
("OM", "OMR"),
("PK", "PKR"),
("PW", "USD"),
("PS", "ILS"),
("PA", "PAB"),
("PG", "PGK"),
("PY", "PYG"),
("PE", "PEN"),
("PH", "PHP"),
("PN", "NZD"),
("PL", "PLN"),
("PT", "EUR"),
("PR", "USD"),
("QA", "QAR"),
("CG", "XAF"),
("RE", "EUR"),
("RO", "RON"),
("RU", "RUB"),
("RW", "RWF"),
("SH", "SHP"),
("KN", "XCD"),
("LC", "XCD"),
("PM", "EUR"),
("VC", "XCD"),
("WS", "WST"),
("SM", "EUR"),
("ST", "STN"),
("SA", "SAR"),
("SN", "XOF"),
("CS", "RSD"),
("SC", "SCR"),
("SL", "SLL"),
("SG", "SGD"),
("SK", "EUR"),
("SI", "EUR"),
("SB", "SBD"),
("SO", "SOS"),
("ZA", "ZAR"),
("GS", "GBP"),
("KR", "KRW"),
("ES", "EUR"),
("LK", "LKR"),
("SD", "SDG"),
("SR", "SRD"),
("SJ", "NOK"),
("SZ", "SZL"),
("SE", "SEK"),
("CH", "CHF"),
("SY", "SYP"),
("TW", "TWD"),
("TJ", "TJS"),
("TZ", "TZS"),
("TH", "THB"),
("TG", "XOF"),
("TK", "NZD"),
("TO", "TOP"),
("TT", "TTD"),
("TN", "TND"),
("TR", "TRY"),
("TM", "TMT"),
("TC", "USD"),
("TV", "AUD"),
("VI", "USD"),
("UG", "UGX"),
("UA", "UAH"),
("AE", "AED"),
("GB", "GBP"),
("US", "USD"),
("UM", "USD"),
("UY", "UYU"),
("UZ", "UZS"),
("VU", "VUV"),
("VA", "EUR"),
("VE", "VES"),
("VN", "VND"),
("WF", "XPF"),
("EH", "MAD"),
("YE", "YER"),
("ZM", "ZMW"),
("ZW", "ZWL"),
]
.into_iter()
.collect()
});
static COUNTRY_CONTINENTS: LazyLock<FxHashMap<&'static str, &'static str>> = LazyLock::new(|| {
[
("DZ", "AF"),
("AO", "AF"),
("BJ", "AF"),
("BW", "AF"),
("BF", "AF"),
("BI", "AF"),
("CM", "AF"),
("CV", "AF"),
("CF", "AF"),
("TD", "AF"),
("KM", "AF"),
("CD", "AF"),
("DJ", "AF"),
("EG", "AF"),
("GQ", "AF"),
("ER", "AF"),
("ET", "AF"),
("GA", "AF"),
("GM", "AF"),
("GH", "AF"),
("GN", "AF"),
("GW", "AF"),
("CI", "AF"),
("KE", "AF"),
("LS", "AF"),
("LR", "AF"),
("LY", "AF"),
("MG", "AF"),
("MW", "AF"),
("ML", "AF"),
("MR", "AF"),
("MU", "AF"),
("MA", "AF"),
("MZ", "AF"),
("NA", "AF"),
("NE", "AF"),
("NG", "AF"),
("CG", "AF"),
("RW", "AF"),
("ST", "AF"),
("SN", "AF"),
("SC", "AF"),
("SL", "AF"),
("SO", "AF"),
("ZA", "AF"),
("SD", "AF"),
("SZ", "AF"),
("TZ", "AF"),
("TG", "AF"),
("TN", "AF"),
("UG", "AF"),
("EH", "AF"),
("ZM", "AF"),
("ZW", "AF"),
("AQ", "AN"),
("BV", "AN"),
("TF", "AN"),
("HM", "AN"),
("GS", "AN"),
("AF", "AS"),
("AM", "AS"),
("AZ", "AS"),
("BH", "AS"),
("BD", "AS"),
("BT", "AS"),
("IO", "AS"),
("BN", "AS"),
("KH", "AS"),
("CN", "AS"),
("CX", "AS"),
("CC", "AS"),
("GE", "AS"),
("HK", "AS"),
("IN", "AS"),
("ID", "AS"),
("IR", "AS"),
("IQ", "AS"),
("IL", "AS"),
("JP", "AS"),
("JO", "AS"),
("KZ", "AS"),
("KW", "AS"),
("KG", "AS"),
("LA", "AS"),
("LB", "AS"),
("MO", "AS"),
("MY", "AS"),
("MV", "AS"),
("MN", "AS"),
("MM", "AS"),
("NP", "AS"),
("KP", "AS"),
("OM", "AS"),
("PK", "AS"),
("PS", "AS"),
("PH", "AS"),
("QA", "AS"),
("SA", "AS"),
("SG", "AS"),
("KR", "AS"),
("LK", "AS"),
("SY", "AS"),
("TW", "AS"),
("TJ", "AS"),
("TH", "AS"),
("TL", "AS"),
("TR", "AS"),
("TM", "AS"),
("AE", "AS"),
("UZ", "AS"),
("VN", "AS"),
("YE", "AS"),
("AL", "EU"),
("AD", "EU"),
("AT", "EU"),
("BY", "EU"),
("BE", "EU"),
("BA", "EU"),
("BG", "EU"),
("HR", "EU"),
("CY", "EU"),
("CZ", "EU"),
("DK", "EU"),
("EE", "EU"),
("FI", "EU"),
("FR", "EU"),
("DE", "EU"),
("GI", "EU"),
("GR", "EU"),
("HU", "EU"),
("IS", "EU"),
("IE", "EU"),
("IT", "EU"),
("LV", "EU"),
("LI", "EU"),
("LT", "EU"),
("LU", "EU"),
("MK", "EU"),
("MT", "EU"),
("MD", "EU"),
("MC", "EU"),
("ME", "EU"),
("NL", "EU"),
("NO", "EU"),
("PL", "EU"),
("PT", "EU"),
("RO", "EU"),
("RU", "EU"),
("SM", "EU"),
("RS", "EU"),
("CS", "EU"),
("SK", "EU"),
("SI", "EU"),
("ES", "EU"),
("SJ", "EU"),
("SE", "EU"),
("CH", "EU"),
("UA", "EU"),
("GB", "EU"),
("VA", "EU"),
("AI", "NA"),
("AG", "NA"),
("BS", "NA"),
("BB", "NA"),
("BZ", "NA"),
("BM", "NA"),
("CA", "NA"),
("KY", "NA"),
("CR", "NA"),
("CU", "NA"),
("DM", "NA"),
("DO", "NA"),
("SV", "NA"),
("GL", "NA"),
("GD", "NA"),
("GP", "NA"),
("GT", "NA"),
("HT", "NA"),
("HN", "NA"),
("JM", "NA"),
("MQ", "NA"),
("MX", "NA"),
("MS", "NA"),
("AN", "NA"),
("NI", "NA"),
("PA", "NA"),
("PR", "NA"),
("KN", "NA"),
("LC", "NA"),
("PM", "NA"),
("VC", "NA"),
("TC", "NA"),
("US", "NA"),
("UM", "NA"),
("VI", "NA"),
("AS", "OC"),
("AU", "OC"),
("CK", "OC"),
("FJ", "OC"),
("PF", "OC"),
("GU", "OC"),
("KI", "OC"),
("MH", "OC"),
("FM", "OC"),
("NR", "OC"),
("NC", "OC"),
("NZ", "OC"),
("NU", "OC"),
("NF", "OC"),
("MP", "OC"),
("PW", "OC"),
("PG", "OC"),
("PN", "OC"),
("WS", "OC"),
("SB", "OC"),
("TK", "OC"),
("TO", "OC"),
("TV", "OC"),
("VU", "OC"),
("WF", "OC"),
("AR", "SA"),
("BO", "SA"),
("BR", "SA"),
("CL", "SA"),
("CO", "SA"),
("EC", "SA"),
("FK", "SA"),
("GF", "SA"),
("GY", "SA"),
("PY", "SA"),
("PE", "SA"),
("SR", "SA"),
("UY", "SA"),
("VE", "SA"),
]
.into_iter()
.collect()
});
static CONTINENT_NAMES: LazyLock<FxHashMap<&'static str, &'static str>> = LazyLock::new(|| {
[
("AF", "Africa"),
("AN", "Antarctica"),
("AS", "Asia"),
("EU", "Europe"),
("NA", "North America"),
("OC", "Oceania"),
("SA", "South America"),
]
.into_iter()
.collect()
});
static EU_COUNTRIES: LazyLock<FxHashMap<&'static str, bool>> = LazyLock::new(|| {
[
("AT", true),
("BE", true),
("BG", true),
("HR", true),
("CY", true),
("CZ", true),
("DK", true),
("EE", true),
("FI", true),
("FR", true),
("DE", true),
("GR", true),
("HU", true),
("IE", true),
("IT", true),
("LV", true),
("LT", true),
("LU", true),
("MT", true),
("NL", true),
("PL", true),
("PT", true),
("RO", true),
("SK", true),
("SI", true),
("ES", true),
("SE", true),
]
.into_iter()
.collect()
});
fn format_offset(offset_secs: i32) -> String {
let hours = offset_secs / 3600;
let mins = (offset_secs.abs() % 3600) / 60;
if mins == 0 {
format!("UTC{:+}", hours)
} else {
format!("UTC{:+}:{:02}", hours, mins)
}
}
fn calculate_dst(tz: &Tz, offset_secs: i32) -> bool {
let jan = tz
.with_ymd_and_hms(2024, 1, 15, 12, 0, 0)
.unwrap()
.offset()
.fix()
.local_minus_utc();
let jul = tz
.with_ymd_and_hms(2024, 7, 15, 12, 0, 0)
.unwrap()
.offset()
.fix()
.local_minus_utc();
offset_secs != jan.min(jul)
}
pub struct PlaceInput<'a> {
pub city: &'a str,
pub region: &'a str,
pub region_code: &'a str,
pub district: &'a str,
pub country_code: &'a str,
pub postal_code: &'a str,
pub timezone: &'a str,
pub latitude: f64,
pub longitude: f64,
}
pub fn enrich_place(input: PlaceInput) -> Place {
let (timezone_abbr, utc_offset, utc_offset_str, dst_active) = Tz::from_str(input.timezone)
.ok()
.map(|tz| {
let local = Utc::now().with_timezone(&tz);
let offset_secs = local.offset().fix().local_minus_utc();
(
format!("{}", local.format("%Z")),
offset_secs,
format_offset(offset_secs),
calculate_dst(&tz, offset_secs),
)
})
.unwrap_or_else(|| (String::new(), 0, "UTC+0".to_string(), false));
Place {
city: input.city.to_string(),
region: input.region.to_string(),
region_code: input.region_code.to_string(),
district: input.district.to_string(),
country_code: input.country_code.to_string(),
country_name: COUNTRY_NAMES
.get(input.country_code)
.unwrap_or(&"Unknown")
.to_string(),
postal_code: input.postal_code.to_string(),
timezone: input.timezone.to_string(),
timezone_abbr,
utc_offset,
utc_offset_str,
latitude: input.latitude,
longitude: input.longitude,
currency: COUNTRY_CURRENCIES
.get(input.country_code)
.unwrap_or(&"")
.to_string(),
continent_code: COUNTRY_CONTINENTS
.get(input.country_code)
.unwrap_or(&"")
.to_string(),
continent_name: COUNTRY_CONTINENTS
.get(input.country_code)
.and_then(|c| CONTINENT_NAMES.get(c))
.unwrap_or(&"Unknown")
.to_string(),
is_eu: EU_COUNTRIES.contains_key(input.country_code),
dst_active,
}
}