#![forbid(unsafe_code)]
use chrono_tz::Tz;
pub mod prelude {
pub use crate::{Country, Currency, Language};
}
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
pub struct Country {
pub name: &'static str,
pub capital: Option<&'static str>,
pub region: Option<&'static str>,
pub alpha_2: &'static str,
pub alpha_3: &'static str,
pub timezones: &'static [Timezone],
pub currencies: &'static [Currency],
pub languages: &'static [Language],
pub call_codes: &'static [&'static str],
}
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct Currency {
pub code: Option<&'static str>,
pub name: Option<&'static str>,
pub symbol: Option<&'static str>,
}
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct Language {
pub iso639_1: Option<&'static str>,
pub iso639_2: Option<&'static str>,
pub name: Option<&'static str>,
pub native_name: Option<&'static str>,
}
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct Timezone {
pub iana_identifier: &'static str,
}
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
impl Country {
pub fn from_name(name: &str) -> Option<&'static Self> {
NAMES.get(name)
}
#[cfg(feature = "from_capitals")]
pub fn from_capital(capital: &str) -> Option<&'static [Self]> {
CAPTIAL.get(capital).map(|e| *e)
}
#[cfg(feature = "from_regions")]
pub fn from_region(region: &str) -> Option<&'static [Self]> {
REGIONS.get(region).map(|e| *e)
}
#[cfg(feature = "from_alpha_2")]
pub fn from_alpha_2(alpha_2: &str) -> Option<&'static [Self]> {
ALPHA_2.get(alpha_2).map(|e| *e)
}
#[cfg(feature = "from_alpha_3")]
pub fn from_alpha_3(alpha_3: &str) -> Option<&'static [Self]> {
ALPHA_3.get(alpha_3).map(|e| *e)
}
}
impl Timezone {
pub fn timezone(&self) -> Result<Tz, String> {
self.iana_identifier.parse()
}
}
#[cfg(test)]
mod test {
use super::*;
macro_rules! india_check {
( $india : expr ) => {
assert_eq!($india.capital.unwrap(), "New Delhi");
assert_eq!($india.region.unwrap(), "Southern Asia");
assert_eq!($india.alpha_2, "IN");
assert_eq!($india.alpha_3, "IND");
assert_eq!($india.timezones[0].iana_identifier, "Asia/Kolkata");
assert_eq!($india.call_codes[0], "91");
assert_eq!(
$india.currencies[0],
Currency {
code: Some("INR"),
name: Some("Indian rupee"),
symbol: Some("₹"),
}
);
assert_eq!(
$india.languages[0],
Language {
iso639_1: Some("hi"),
iso639_2: Some("hin"),
name: Some("Hindi"),
native_name: Some("हिन्दी"),
}
);
assert_eq!(
$india.languages[1],
Language {
iso639_1: Some("en"),
iso639_2: Some("eng"),
name: Some("English"),
native_name: Some("English"),
}
);
};
}
#[test]
fn basic_country_fetching_from_name() {
let india = Country::from_name("India").unwrap();
india_check!(india);
}
#[cfg(feature = "from_capitals")]
#[test]
fn basic_country_fetching_from_capital() {
let india = Country::from_capital("New Delhi").unwrap();
india_check!(india[0]);
}
#[cfg(feature = "from_alpha_3")]
#[test]
fn basic_country_fetching_from_alpha_2() {
let india = Country::from_alpha_2("IN").unwrap()[0];
india_check!(india);
}
#[cfg(feature = "from_alpha_3")]
#[test]
fn basic_country_fetching_from_alpha_3() {
let india = Country::from_alpha_3("IND").unwrap()[0];
india_check!(india);
}
#[cfg(feature = "from_regions")]
#[test]
fn basic_country_fetching_from_region() {
let southern_asia = Country::from_region("Southern Asia").unwrap();
assert!(southern_asia.contains(Country::from_name("India").unwrap()));
}
}