9个版本 (4个破坏性)
0.5.0 | 2024年2月12日 |
---|---|
0.4.0 | 2024年2月12日 |
0.3.0 | 2023年7月3日 |
0.2.0 | 2023年7月3日 |
0.1.2 | 2023年3月31日 |
#42 in 地理空间
每月452次下载
22MB
282K SLoC
Keshvar
此库包含每个国家的各种有用信息的集合。
演示
use keshvar::{CurrencyCode, WeekDay, Region, SubdivisionType, find_by_name};
let country_name_in_any_language = "estados unidos"; // The US in spanish!
let country = find_by_name(country_name_in_any_language).unwrap();
assert_eq!(country.iso_long_name(), "The United States of America");
assert_eq!(country.maybe_nationality(), Some("American"));
assert_eq!(country.currency_code(), CurrencyCode::USD);
assert_eq!(country.start_of_week(), WeekDay::Sunday);
assert_eq!(country.emoji(), "🇺🇸");
assert_eq!(country.country_code(), 1);
assert!(country.maybe_population().unwrap() > 330_000_000);
assert_eq!(country.maybe_region(), Some(Region::Americas));
assert!(country.unofficial_name_list().contains(&"United States"));
assert!(country.spoken_language_list().contains(&"en"));
assert!(country.distance_unit().is_mi()); // KM/MI
assert!(country.g7_member() && country.g20_member());
assert!(!country.eu_member() && !country.eea_member()); // Not in `European Union` and `European Economic Area`
assert!(!country.gdpr_compliant()); // It's not GDPR compliant too!
let geo = country.geo();
assert_eq!(
(geo.latitude(), geo.longitude()),
(37.09024, -95.712891)
);
assert_eq!(
(geo.min_latitude(), geo.max_longitude()),
(18.91619, -66.96466)
);
assert_eq!(geo.bounds().northeast().latitude(), 71.3577635769);
assert_eq!(geo.bounds().southwest().longitude(), -171.791110603);
let subdivisions = country.subdivisions();
let california = subdivisions.get("CA").unwrap();
let geo = california.geo().unwrap();
assert_eq!(california.name(), "California");
assert_eq!(
(geo.latitude(), geo.longitude()),
(Some(36.778261), Some(-119.4179324))
);
assert_eq!(california.subdivision_type(), SubdivisionType::State);
assert_eq!(
california.translations().get(&"cs"), // in Czech language
Some(&"Kalifornie")
);
功能
- 支持不同国家、子区域、地区、大陆和世界地区的Cargo功能。
- 对于它支持的每个国家
- 简称、长称和非官方名称(ISO 3166的部分)
- 货币代码
- 地址格式
- 官方和使用的语言
- 国籍
- 邮政编码格式
- 一周的开始
- 电话号码(E.164)
- GDPR合规性
- 增值税(增值税)税率
- 距离单位
- ...
- 国家子划分。(可选)
- 国家和其子划分的地理定位(可选)
- 国家和子划分的翻译(可选)
serde
集成(可选)chrono
集成(可选)iso_currency (ISO 4217)
集成(可选)
安装
将此库作为依赖项添加到您的Cargo.toml
文件中(从命令行或手动操作)。
命令行
在您的Cargo项目的根目录下运行以下命令
cargo add keshvar
手动
在您的Cargo.toml
文件中的dependencies
部分下添加keshvar = "0.1"
。
现在您可以在您的Cargo项目中使用它了。
Cargo功能
默认情况下,所有国家都包含在内。此外,您还可以将 subdivisions
、translations
、geo
、search-iso-short-name
、search-iso-long-name
、search-iso-number
、search-country-code
、search-translations
、emojis
、serde-derive
、chrono-integration
和 iso-currency-integration
添加到您的 Cargo.toml
文件中。
[dependencies]
# 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
keshvar = {version = "<VERSION>", features = ["translations", "geo", "serde-derive"]}
如果您不想支持所有国家,您可以禁用默认功能并包含您想要的国家。
[dependencies]
# Include:
# - only USA and Englang.
# - Subdivisions for included countries (here only USA and Englang).
keshvar = {version = "<VERSION>", default-features = false, features = ["us", "gb", "subdivisions"]}
此外,您还可以仅包含不同的大洲、地区、子地区和世界地区的国家。
[dependencies]
# Include:
# - Countries of `asia` continent.
# - Countries of `oceania` region.
# - Countries of `northern-africa` subregion.
keshvar = {version = "<VERSION>", default-features = false, features = ["asia", "oceania", "northern-africa"]}
大洲 功能名称:africa
| antarctica
| asia
| australia
| europe
| north-america
| south-america
地区 功能名称:americas
| oceania
| region-africa
| region-antarctica
| region-asia
| region-europe
子地区 功能名称: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
世界地区 功能名称:amer
| apac
| emea
示例
国家结构体
此库的主要结构是 Country
结构体。大多数其他类型都有一个 to_country()
方法,我们通常希望将它们转换为这个结构体。
use keshvar::{Country, Alpha2, Alpha3, GEC, IOC};
let country = Country::try_from("US").unwrap();
// IOC (International Olympic Committee):
assert_eq!(IOC::USA.to_country(), country);
// GEC (Geopolitical Entities and Codes):
assert_eq!(GEC::US.to_country(), country);
// ISO 3166 alpha2 code:
assert_eq!(Alpha2::US.to_country(), country);
// ISO 3166 alpha3 code:
assert_eq!(Alpha3::USA.to_country(), country);
更多详情请参阅 Country
。
迭代
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 start_at_mon = country.start_of_week() == WeekDay::Monday;
let use_usd_currency = country.currency_code() == CurrencyCode::USD;
if start_at_mon && use_usd_currency {
list.push(country)
}
}
// Found 17 countries:
assert_eq!(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 supported_continents = ContinentIterator::new().count();
assert_eq!(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:
.alpha2_list()
// Iterate over them:
.iter()
// Try convert them to Alpha2 enum:
.filter_map(|alpha2_str| Alpha2::try_from(*alpha2_str).ok())
// Convert Alpha2 enums to Country structs:
.map(|alpha2| alpha2.to_country())
// Take countries that their start day of the week is sunday:
.filter(|country| country.start_of_week() == 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:
assert_eq!(1, list.len());
assert_eq!(SubRegion::WesternAsia, list[0]);
子地区
在 Cargo.toml
文件中启用 subdivisions
功能
[dependencies]
keshvar = {version = "<VERSION>", features = ["subdivisions"]}
示例
use std::collections::HashMap;
use keshvar::{GEC, Country, Subdivision, SubdivisionType, SubdivisionGeo};
// Load from GEC (Geopolitical Entities and Codes)
let country: Country = GEC::UK.to_country(); // England
// A hashmap containing string subdivision codes as keys and `Subdivision` structs as values:
let subdivisions: &HashMap<_, Subdivision> = country.subdivisions();
let london = subdivisions.get("LND").unwrap();
assert_eq!("London, City of", london.name());
assert_eq!(SubdivisionType::CityCorporation, london.subdivision_type());
// If you enabled `translations` feature:
assert_eq!(Some(&"مدينة لندن"), london.translations().get("ar")); // Arabic
// If you enabled `geo` feature:
let geo = london.geo().unwrap();
assert_eq!(Some(51.5073509), geo.latitude());
assert_eq!(Some(-0.1277583), geo.longitude());
翻译
在 Cargo.toml
文件中启用 translations
功能
[dependencies]
keshvar = {version = "<VERSION>", features = ["translations"]}
示例
use std::collections::HashMap;
use keshvar::{Alpha2, CountryIterator};
// Load from alpha2 code
let country = Alpha2::CN.to_country(); // China
// A hashmap containing languages as keys and translations as values:
let translations = country.translations();
assert_eq!(Some(&"Chine"), translations.get("fr")); // French
assert_eq!(Some(&"Китай"), translations.get("ru")); // Russian
// Find in all translations for country name "Ізраїль"
let search_text = "Ізраїль";
// Iterate over all included countries:
let country = CountryIterator::new()
.find(
|country| {
// Search for the given name in translations:
country
.translations()
.values()
.collect::<Vec<&&str>>()
.contains(&&search_text)
}
).unwrap();
// Actually "Ізраїль" is "Israel" in ukrainian language
assert_eq!("Israel", country.iso_short_name());
地理信息
在 Cargo.toml
文件中启用 geo
功能
[dependencies]
keshvar = {version = "<VERSION>", features = ["geo"]}
示例
use keshvar::{IOC, Country, CountryGeo, CountryGeoBounds};
// Load from IOC (International Olympic Committee)
let country: Country = IOC::INA.to_country(); // The Republic of Indonesia (Asia)
let geo: CountryGeo = country.geo();
assert_eq!((-0.789275, 113.921327), (geo.latitude(), geo.longitude()));
assert_eq!((6.216999899999999, 141.0425), (geo.max_latitude(), geo.max_longitude()));
assert_eq!((-11.1082999, 94.7351), (geo.min_latitude(), geo.min_longitude()));
let bounds = geo.bounds();
assert_eq!((6.216999899999999, 141.0425), (bounds.northeast().latitude(), bounds.northeast().longitude()));
assert_eq!((-11.1082999, 94.7351), (bounds.southwest().latitude(), bounds.southwest().longitude()));
搜索功能
use keshvar::{Alpha2, Alpha3, Region, SubRegion, Continent};
// Utility functions:
use keshvar::{
find_by_iso_short_name, // if `search-iso-short-name` feature is enabled
find_by_iso_long_name, // if `search-iso-long-name` feature is enabled
find_by_code // if `search-country-code` feature is enabled
};
let country = find_by_iso_short_name("united states of america").unwrap();
assert_eq!(Some("American"), country.maybe_nationality());
let country = find_by_iso_long_name("ukraine").unwrap();
assert_eq!(Alpha2::UA, country.alpha2());
assert_eq!(Some(SubRegion::EasternEurope), country.maybe_subregion());
let country = find_by_code(971).unwrap(); // The United Arab Emirates (Asia)
assert_eq!(Alpha3::ARE, country.alpha3());
assert_eq!(Continent::Asia, country.continent());
命名
“keshvar” (/keʃvar/ 或 کِشوَر) 在 波斯语/ فارسی 中的意思是 country
。
贡献
请参阅 CONTRIBUTING.md 文件。
许可证
keshvar
源代码生成器和生成的源代码在 BSD-3-Clause 许可证下分发(见 LICENSE
文件),但用于生成器的数据在 MIT 许可证下分发(见 countries
许可证文件)。此外,来自 世界银行数据集 的 population 数据由其维护者根据 公共领域贡献和许可证 (PDDL) 许可。