Keshvar
This library contains collection of all sorts of useful information for every country.
Package | Documentation | Repository
Demo
use ;
let country_name_in_any_language = "estados unidos"; // The US in spanish!
let country = find_by_name.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
assert!;
assert!;
let geo = country.geo;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let subdivisions = country.subdivisions;
let california = subdivisions.get.unwrap;
let geo = california.geo.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Features
- Cargo features to support different countries, sub-regions, regions, continents, and world regions.
- For every country it supports:
- Short and long and unofficial names (parts of ISO 3166)
- Currency code
- Address format
- Official and spoken languages
- Nationality
- Postal code format
- Start of the week
- Phone number (E.164)
- ...
- Country Subdivisions. (Optional)
- GEO locations for countries and their subdivisions (Optional)
- Translations for countries and subdivisions (Optional)
serdeintegration (Optional)chronointegration (Optional)iso_currency (ISO 4217)integration (Optional)
Installation
Add this library as a dependency (from commandline or do it manually) in your Cargo.toml file.
Commandline
Run bellow command inside root directory of your Cargo project:
cargo add keshvar
Manual
Add keshvar = "0.1" under dependencies section inside your Cargo.toml file.
Now you're ready to use it inside your Cargo project.
Cargo features
By default, all countries are included. Additionally, you can add subdivisions, translations, geo, search-iso-short-name, search-iso-long-name, search-iso-number, search-country-code, search-translations, emojis, serde-derive, chrono-integration, and iso-currency-integration to your Cargo.toml file:
[]
# Include:
# - all countries (which is the default).
# - Translations for all country names.
# - Geo for all countries.
# - serde support for serializing/deserializing keshvar's structs and enums
= { = "<VERSION>", = ["translations", "geo", "serde-derive"]}
If you do not want to support all countries, You can disable default features and include what countries you want:
[]
# Include:
# - only USA and Englang.
# - Subdivisions for included countries (here only USA and Englang).
= { = "<VERSION>", = false, = ["us", "gb", "subdivisions"]}
Additionally, You can only include countries for different continents, regions, subregions, and world regions:
[]
# Include:
# - Countries of `asia` continent.
# - Countries of `oceania` region.
# - Countries of `northern-africa` subregion.
= { = "<VERSION>", = false, = ["asia", "oceania", "northern-africa"]}
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
Examples
Country struct
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.
use ;
let country = try_from.unwrap;
// IOC (International Olympic Committee):
assert_eq!;
// GEC (Geopolitical Entities and Codes):
assert_eq!;
// ISO 3166 alpha2 code:
assert_eq!;
// ISO 3166 alpha3 code:
assert_eq!;
For more info see Country.
Iterating
use ;
use ;
let mut list = Vecnew;
// Doing iteration by for loop:
for country in new
// Found 17 countries:
assert_eq!;
assert!; // It contains The Republic of Zimbabwe (ZW alpha2 code)
// Use Any `Iterator` method you want:
let supported_continents = new.count;
assert_eq!;
// Doing iteration in a functional way:
let list: = new
// Start filtering our supported subregions:
.filter.collect;
// So there is just one subregion that contains more than 4 countries that
// their start day of the week is sunday:
assert_eq!;
assert_eq!;
Subdivisions
Enable subdivisions feature inside Cargo.toml file:
[]
= { = "<VERSION>", = ["subdivisions"]}
Example
use HashMap;
use ;
// Load from GEC (Geopolitical Entities and Codes)
let country: Country = GECUK.to_country; // England
// A hashmap containing string subdivision codes as keys and `Subdivision` structs as values:
let subdivisions: & = country.subdivisions;
let london = subdivisions.get.unwrap;
assert_eq!;
assert_eq!;
// If you enabled `translations` feature:
assert_eq!; // Arabic
// If you enabled `geo` feature:
let geo = london.geo.unwrap;
assert_eq!;
assert_eq!;
Translations
Enable translations feature inside Cargo.toml file:
[]
= { = "<VERSION>", = ["translations"]}
Example
use HashMap;
use ;
// Load from alpha2 code
let country = CN.to_country; // China
// A hashmap containing languages as keys and translations as values:
let translations: & = country.translations;
assert_eq!; // French
assert_eq!; // Russian
// Find in all translations for country name "Ізраїль"
let search_text = "Ізраїль";
// Iterate over all included countries:
let country = new
.find.unwrap;
// Actually "Ізраїль" is "Israel" in ukrainian language
assert_eq!;
GEO
Enable geo feature inside Cargo.toml file:
[]
= { = "<VERSION>", = ["geo"]}
Example
use ;
// Load from IOC (International Olympic Committee)
let country: Country = IOCINA.to_country; // The Republic of Indonesia (Asia)
let geo: CountryGeo = country.geo;
assert_eq!;
assert_eq!;
assert_eq!;
let bounds = geo.bounds;
assert_eq!;
assert_eq!;
Search functions
use ;
// Utility functions:
use ;
let country = find_by_iso_short_name.unwrap;
assert_eq!;
let country = find_by_iso_long_name.unwrap;
assert_eq!;
assert_eq!;
let country = find_by_code.unwrap; // The United Arab Emirates (Asia)
assert_eq!;
assert_eq!;
Naming
keshvar (/keʃvar/ or کِشوَر) simply means country in persian/farsi language.
Contribution
See CONTRIBUTING.md file.
License
keshvar source-code generator and the generated source are distributed under BSD-3-Clause license (See LICENSE file) but the data that is used to feed the generator is distributed under MIT License (See countries license file).