This library contains collection of all sorts of useful information for every country.
Package | Documentation | Repository
serde
integration (Optional)chrono
integration (Optional)iso_currency (ISO 4217)
integration (Optional)Add this library as a dependency (from commandline or do it manually) in your Cargo.toml
file.
Run bellow command inside root directory of your Cargo project:
shell
cargo add keshvar
Add keshvar = "0.1"
under dependencies
section inside your Cargo.toml
file.
Now you're ready to use it inside your Cargo project.
By default, all countries are included. Additionally, you can add subdivisions
, translations
, geo
, serde-derive
, chrono-integration
, and iso-currency-integration
to your Cargo.toml
file:
```toml
[dependencies]
keshvar = {version = "
If you do not want to support all countries, You can disable default features and include what countries you want:
toml
[dependencies]
keshvar = {version = "
Additionally, You can only include countries for different continents, regions, subregions, and world regions:
toml
[dependencies]
asia
continent.oceania
region.northern-africa
subregion.keshvar = {version = "
[Continent](crate::Continent) feature names:
africa|
antarctica|
asia|
australia|
europe|
north-america|
south-america`
Region feature names: americas
| oceania
| region-africa
| region-antarctica
| region-asia
| region-europe
Subregion feature names: australia-and-new-zealand
| caribbean
| central-america
| central-asia
| eastern-africa
| eastern-asia
| eastern-europe
| melanesia
| micronesia
| middle-africa
| northern-africa
| northern-america
| northern-europe
| polynesia
| south-eastern-asia
| southern-africa
| southern-asia
| southern-europe
| subregion-south-america
| western-africa
| western-asia
| western-europe
World region feature names: amer
| apac
| emea
Main struct of this library is the Country
struct. Most other types have a to_country()
method, and we usually want to convert them to this struct.
```rust
use keshvar::{Country, CurrencyCode, WeekDay};
let country = Country::tryfrom("US").unwrap(); // "US" is its alpha2 code
asserteq!("United States of America", country.isoshortname());
asserteq!("The United States of America", country.isolongname());
assert!(country.unofficialnamelist().contains(&"United States"));
asserteq!(CurrencyCode::USD, country.currencycode());
asserteq!(WeekDay::Sunday, country.startofweek());
``
For more info see [
Country`](crate::Country).
```rust use keshvar::{ CountryIterator, // To iterate over all included countries ContinentIterator, // To iterate over all supported continents (based on included countries) SubRegionIterator, // To iterate over all supported subregions (based on included countries) }; use keshvar::{Alpha2, SubRegion, WeekDay, CurrencyCode};
let mut list = Vec::new(); // Doing iteration by for loop: for country in CountryIterator::new() { let startatmon = country.startofweek() == WeekDay::Monday; let useusdcurrency = country.currencycode() == CurrencyCode::USD; if startatmon && useusdcurrency { list.push(country) } } // Found 17 countries: asserteq!(17, list.len()); assert!(list.contains(&Alpha2::ZW.to_country())); // It contains The Republic of Zimbabwe (ZW alpha2 code)
// Use Any Iterator
method you want:
let supportedcontinents = ContinentIterator::new().count();
asserteq!(7, supported_continents);
// Doing iteration in a functional way: let list: Vec<_> = SubRegionIterator::new() // Start filtering our supported subregions: .filter( |subregion| { subregion // Get alpha2 codes for countries of this subregion: .alpha2list() // Iterate over them: .iter() // Try convert them to Alpha2 enum: .filtermap(|alpha2str| Alpha2::tryfrom(*alpha2str).ok()) // Convert Alpha2 enums to Country structs: .map(|alpha2| alpha2.tocountry()) // Take countries that their start day of the week is sunday: .filter(|country| country.startofweek() == WeekDay::Sunday) // Check if this filter has more than 4 output items: .count() > 4 } ).collect(); // So there is just one subregion that contains more than 4 countries that // their start day of the week is sunday: asserteq!(1, list.len()); asserteq!(SubRegion::WesternAsia, list[0]); ```
Enable subdivisions
feature inside Cargo.toml
file:
toml
[dependencies]
keshvar = {version = "<VERSION>", features = ["subdivisions"]}
```rust use std::collections::HashMap; use keshvar::{GEC, Country, Subdivision, SubdivisionType, SubdivisionGeo};
// Load from GEC (Geopolitical Entities and Codes)
let country: Country = GEC::UK.tocountry(); // England
// A hashmap containing string subdivision codes as keys and Subdivision
structs as values:
let subdivisions: &HashMap<&str, Subdivision> = country.subdivisions();
let london = subdivisions.get("LND").unwrap();
asserteq!("London, City of", london.name());
asserteq!(SubdivisionType::CityCorporation, london.subdivisiontype());
// If you enabled translations
feature:
assert_eq!(Some(&"مدينة لندن"), london.translations().get("ar")); // Arabic
// If you enabled geo
feature:
let geo = london.geo().unwrap();
asserteq!(Some(51.5073509), geo.latitude());
asserteq!(Some(-0.1277583), geo.longitude());
```
Enable translations
feature inside Cargo.toml
file:
toml
[dependencies]
keshvar = {version = "<VERSION>", features = ["translations"]}
```rust use std::collections::HashMap; use keshvar::{Alpha2, CountryIterator};
// Load from alpha2 code let country = Alpha2::CN.tocountry(); // China // A hashmap containing languages as keys and translations as values: let translations: &HashMap<&str, &str> = country.translations(); asserteq!(Some(&"Chine"), translations.get("fr")); // French assert_eq!(Some(&"Китай"), translations.get("ru")); // Russian
// Find in all translations for country name "Ізраїль"
let searchtext = "Ізраїль";
// Iterate over all included countries:
let country = CountryIterator::new()
.find(
|country| {
// Search for the given name in translations:
country
.translations()
.values()
.collect::
Enable geo
feature inside Cargo.toml
file:
toml
[dependencies]
keshvar = {version = "<VERSION>", features = ["geo"]}
```rust use keshvar::{IOC, Country, CountryGeo, CountryGeoBounds};
// Load from IOC (International Olympic Committee) let country: Country = IOC::INA.tocountry(); // The Republic of Indonesia (Asia) let geo: CountryGeo = country.geo(); asserteq!((-0.789275, 113.921327), (geo.latitude(), geo.longitude())); asserteq!((6.216999899999999, 141.0425), (geo.maxlatitude(), geo.maxlongitude())); asserteq!((-11.1082999, 94.7351), (geo.minlatitude(), geo.minlongitude())); let bounds = geo.bounds(); asserteq!((6.216999899999999, 141.0425), (bounds.northeast().latitude(), bounds.northeast().longitude())); asserteq!((-11.1082999, 94.7351), (bounds.southwest().latitude(), bounds.southwest().longitude())); ```
```rust use keshvar::{Alpha2, Alpha3, Region, SubRegion, Continent}; // Utility functions: use keshvar::{findbyisoshortname, findbyisolongname, findbycode};
let country = findbyisoshortname("united states of america").unwrap(); assert_eq!(Some("American"), country.nationality());
let country = findbyisolongname("ukraine").unwrap(); asserteq!(Alpha2::UA, country.alpha2()); asserteq!(Some(SubRegion::EasternEurope), country.subregion());
let country = findbycode(971).unwrap(); // The United Arab Emirates (Asia) asserteq!(Alpha3::ARE, country.alpha3()); asserteq!(Continent::Asia, country.continent()); ```