crate::ix!();
#[derive(Debug,PartialOrd,Ord,PartialEq,Eq,Hash,Clone,Copy,StrumDisplay,StrumEnumIter,StrumEnumVariantNames)]
#[strum(ascii_case_insensitive, serialize_all = "title_case")]
pub enum AsiaRegion {
Afghanistan,
Armenia,
Azerbaijan,
Bangladesh,
Bhutan,
Cambodia,
China(ChinaRegion),
EastTimor,
#[strum(serialize = "GCC States")]
GccStates,
India(IndiaRegion),
Indonesia(IndonesiaRegion),
Iran,
Iraq,
#[strum(serialize = "Israel and Palestine")]
IsraelAndPalestine,
Japan(JapanRegion),
Jordan,
Kazakhstan,
Kyrgyzstan,
Laos,
Lebanon,
#[strum(serialize = "Malaysia, Singapore, and Brunei")]
MalaysiaSingaporeBrunei,
Maldives,
Mongolia,
Myanmar,
Nepal,
NorthKorea,
Pakistan,
Philippines,
RussianFederation(RussianFederationRegion),
SouthKorea,
SriLanka,
Syria,
Taiwan,
Tajikistan,
Thailand,
Turkmenistan,
Uzbekistan,
Vietnam,
Yemen,
}
impl Default for AsiaRegion {
fn default() -> Self {
AsiaRegion::China(ChinaRegion::default())
}
}
#[cfg(test)]
mod test_asia_region {
use super::*;
use serde_json;
use std::convert::TryFrom;
use std::str::FromStr;
#[test]
fn test_default() {
let def = AsiaRegion::default();
if let AsiaRegion::China(cr) = def {
assert_eq!(cr, ChinaRegion::Xinjiang);
} else {
panic!("Default AsiaRegion is not China(Xinjiang)!");
}
}
#[test]
fn test_from_str() {
let parsed = AsiaRegion::from_str("Armenia").expect("Should parse Armenia");
assert_eq!(parsed, AsiaRegion::Armenia);
}
#[test]
fn test_abbreviations() {
assert_eq!(AsiaRegion::Afghanistan.abbreviation(), "AF");
assert_eq!(AsiaRegion::China(ChinaRegion::Beijing).abbreviation(), "CN");
assert_eq!(AsiaRegion::India(IndiaRegion::CentralZone).abbreviation(), "IN");
assert_eq!(AsiaRegion::IsraelAndPalestine.abbreviation(), "IL-PS");
assert_eq!(AsiaRegion::MalaysiaSingaporeBrunei.abbreviation(), "MY-SG-BN");
assert_eq!(AsiaRegion::RussianFederation(RussianFederationRegion::CentralFederalDistrict).abbreviation(), "RU");
}
#[test]
fn test_europe_region_variants_listed() {
let variants = AsiaRegion::VARIANTS;
assert!(variants.contains(&"Armenia"));
assert!(variants.contains(&"China"));
assert!(variants.contains(&"Japan"));
}
#[test]
fn test_asia_region_to_country_success() {
assert_eq!(Country::try_from(AsiaRegion::Afghanistan).unwrap(), Country::Afghanistan);
assert_eq!(Country::try_from(AsiaRegion::Iran).unwrap(), Country::Iran);
assert_eq!(Country::try_from(AsiaRegion::China(ChinaRegion::Hebei)).unwrap(), Country::China);
assert_eq!(Country::try_from(AsiaRegion::India(IndiaRegion::SouthernZone)).unwrap(), Country::India);
assert_eq!(Country::try_from(AsiaRegion::Indonesia(IndonesiaRegion::Sumatra)).unwrap(), Country::Indonesia);
assert_eq!(Country::try_from(AsiaRegion::Japan(JapanRegion::Kanto)).unwrap(), Country::Japan);
assert_eq!(Country::try_from(AsiaRegion::RussianFederation(RussianFederationRegion::SiberianFederalDistrict)).unwrap(), Country::Russia);
assert_eq!(Country::try_from(AsiaRegion::Nepal).unwrap(), Country::Nepal);
assert_eq!(Country::try_from(AsiaRegion::IsraelAndPalestine).unwrap(), Country::Israel);
assert_eq!(Country::try_from(AsiaRegion::MalaysiaSingaporeBrunei).unwrap(), Country::Malaysia);
}
#[test]
fn test_asia_region_to_country_errors() {
match Country::try_from(AsiaRegion::GccStates) {
Err( AsiaRegionConversionError::UnsupportedRegion { .. } ) => {}
_ => panic!("Expected UnsupportedRegion for GCC States"),
}
}
#[test]
fn test_country_to_asia_region_success() {
assert_eq!(AsiaRegion::try_from(Country::Afghanistan).unwrap(), AsiaRegion::Afghanistan);
assert_eq!(AsiaRegion::try_from(Country::Iran).unwrap(), AsiaRegion::Iran);
assert_eq!(AsiaRegion::try_from(Country::China).unwrap(), AsiaRegion::China(ChinaRegion::default()));
assert_eq!(AsiaRegion::try_from(Country::India).unwrap(), AsiaRegion::India(IndiaRegion::default()));
assert_eq!(AsiaRegion::try_from(Country::Indonesia).unwrap(), AsiaRegion::Indonesia(IndonesiaRegion::default()));
assert_eq!(AsiaRegion::try_from(Country::Japan).unwrap(), AsiaRegion::Japan(JapanRegion::default()));
assert_eq!(AsiaRegion::try_from(Country::Russia).unwrap(), AsiaRegion::RussianFederation(RussianFederationRegion::default()));
assert_eq!(AsiaRegion::try_from(Country::Israel).unwrap(), AsiaRegion::IsraelAndPalestine);
assert_eq!(AsiaRegion::try_from(Country::Malaysia).unwrap(), AsiaRegion::MalaysiaSingaporeBrunei);
}
#[test]
fn test_country_to_asia_region_errors() {
match AsiaRegion::try_from(Country::Brazil) {
Err(AsiaRegionConversionError::NotAsian { .. }) => {}
_ => panic!("Expected NotAsian for Brazil"),
}
match AsiaRegion::try_from(Country::USA) {
Err(AsiaRegionConversionError::NotAsian { .. }) => {}
_ => panic!("Expected NotAsian for USA"),
}
}
#[test]
fn test_iso_code_conversions() {
let region = AsiaRegion::Japan(JapanRegion::Hokkaido);
let alpha2: Iso3166Alpha2 = region.try_into().unwrap();
let alpha3: Iso3166Alpha3 = region.try_into().unwrap();
let code: CountryCode = region.try_into().unwrap();
assert_eq!(alpha2, Iso3166Alpha2::JP);
assert_eq!(alpha3, Iso3166Alpha3::JPN);
match code {
CountryCode::Alpha2(a2) => assert_eq!(a2, Iso3166Alpha2::JP),
_ => panic!("Expected Alpha2 code"),
}
}
#[test]
fn test_serialize_deserialize_non_subdivided() {
let region = AsiaRegion::Laos;
let serialized = serde_json::to_string(®ion).expect("Serialize");
let deserialized: AsiaRegion = serde_json::from_str(&serialized).expect("Deserialize");
assert_eq!(region, deserialized);
}
#[test]
fn test_serialize_deserialize_subdivided() {
let region = AsiaRegion::China(ChinaRegion::Beijing);
let serialized = serde_json::to_string(®ion).expect("Serialize");
let deserialized: AsiaRegion = serde_json::from_str(&serialized).expect("Deserialize");
assert_eq!(region, deserialized);
let region2 = AsiaRegion::Indonesia(IndonesiaRegion::Sulawesi);
let serialized2 = serde_json::to_string(®ion2).expect("Serialize");
let deserialized2: AsiaRegion = serde_json::from_str(&serialized2).expect("Deserialize");
assert_eq!(region2, deserialized2);
}
#[test]
fn test_round_trip_country_asia_region() {
let region = AsiaRegion::IsraelAndPalestine;
let c: Country = region.try_into().unwrap();
assert_eq!(c, Country::Israel);
let back: AsiaRegion = c.try_into().unwrap();
assert_eq!(back, AsiaRegion::IsraelAndPalestine);
let region2 = AsiaRegion::MalaysiaSingaporeBrunei;
let c2: Country = region2.try_into().unwrap();
assert_eq!(c2, Country::Malaysia);
let back2: AsiaRegion = c2.try_into().unwrap();
assert_eq!(back2, AsiaRegion::MalaysiaSingaporeBrunei);
}
#[test]
fn test_error_conditions_iso_codes() {
match Iso3166Alpha2::try_from(AsiaRegion::GccStates) {
Err(AsiaRegionConversionError::UnsupportedRegion { .. }) => {}
_ => panic!("Expected error for GCC States -> ISO codes"),
}
}
}