Skip to content

Commit

Permalink
feat: Population data (#31)
Browse files Browse the repository at this point in the history
* doc: update

* feat: population data
  • Loading branch information
pouriya authored Feb 12, 2024
1 parent e7eb6fc commit b01f6f9
Show file tree
Hide file tree
Showing 257 changed files with 2,367 additions and 1,523 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ 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.nationality(), Some("American"));
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_eq!(country.region(), Some(Region::Americas));
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
Expand Down Expand Up @@ -288,11 +289,11 @@ use keshvar::{
};

let country = find_by_iso_short_name("united states of america").unwrap();
assert_eq!(Some("American"), country.nationality());
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.subregion());
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());
Expand All @@ -306,4 +307,4 @@ keshvar (/keʃvar/ or کِشوَر) simply means `country` in [persian/farsi lan
See [**CONTRIBUTING.md** file](https://github.com/pouriya/keshvar/blob/master/CONTRIBUTING.md).

# 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](https://github.com/countries/countries/blob/master/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](https://github.com/countries/countries/blob/master/LICENSE)). Additionally, The population data which is from [World Bank dataset](http://data.worldbank.org/indicator/SP.POP.TOTL) is licensed by its maintainers under the [Public Domain Dedication and License (PDDL)](http://opendatacommons.org/licenses/pddl/1.0/).
1 change: 1 addition & 0 deletions keshvar-code-generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2021"
serde = { version = "1.0", features = ["derive"]}
serde_yaml = "0.9"
anyhow = "1.0"
csv = "1.3.0"
271 changes: 271 additions & 0 deletions keshvar-code-generator/population.csv

Large diffs are not rendered by default.

27 changes: 21 additions & 6 deletions keshvar-code-generator/src/countries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ pub fn generate_country(destination_file: &PathBuf, info: &CountryInfo) -> Resul
"DistanceUnit",
format!("DistanceUnit::{:?}", info.distance_unit),
),
(
"const POPULATION",
"Option<u64>",
if info.population != 0 {
format!("Some({})", info.population)
} else {
"None".to_string()
},
),
] {
file.write_all(format!(" pub {}: {} = {};\n", name, _type, value).as_bytes())?;
}
Expand Down Expand Up @@ -423,25 +432,25 @@ pub fn new() -> Country {{
continent: Continent::{},
country_code: {},
currency_code: CurrencyCode::{},
gec: {},
maybe_gec: {},
#[cfg(feature = "geo")]
geo: geo::new(),
international_prefix: {:?},
ioc: {},
maybe_ioc: {},
iso_long_name: {:?},
iso_short_name: {:?},
official_language_list: {:?}.to_vec(),
spoken_language_list: {:?}.to_vec(),
national_destination_code_length_list: {:?}.to_vec(),
national_number_length_list: {:?}.to_vec(),
national_prefix: {:?},
nationality: {},
maybe_nationality: {},
number: {:?},
postal_code: {:?},
postal_code_format: {},
region: {},
maybe_region: {},
start_of_week: WeekDay::{},
subregion: {},
maybe_subregion: {},
un_locode: {:?},
unofficial_name_list: {:?}.to_vec(),
world_region: WorldRegion::{},
Expand All @@ -455,8 +464,9 @@ pub fn new() -> Country {{
g20_member: {},
eu_member: {},
eea_member: {},
vat_rates: {},
maybe_vat_rates: {},
distance_unit: {},
maybe_population: {},
}}
}}
"#,
Expand Down Expand Up @@ -520,6 +530,11 @@ pub fn new() -> Country {{
"None".to_string()
},
format!("DistanceUnit::{:?}", info.distance_unit),
if info.population != 0 {
format!("Some({})", info.population)
} else {
"None".to_string()
},
)
.as_bytes(),
)?;
Expand Down
2 changes: 2 additions & 0 deletions keshvar-code-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod countries;
mod currency_code;
mod gec;
mod ioc;
mod population;
mod region;
mod search;
mod structs;
Expand Down Expand Up @@ -226,6 +227,7 @@ fn code_gen_countries(data_directory: PathBuf, output_directory: PathBuf) -> Res
}
}
}
population::read_from_file("population.csv", &mut countries_info_list)?;
log!("Region features: {:?}", region_features);
log!("Continent features: {:?}", continent_features);
log!("SubRegion features: {:?}", subregion_features);
Expand Down
39 changes: 39 additions & 0 deletions keshvar-code-generator/src/population.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::structs::CountryInfo;
use anyhow::{Context, Result};
use std::fs;
use std::io::BufReader;
use std::path::Path;

pub fn read_from_file<F: AsRef<Path>>(
filename: F,
countries_info_list: &mut Vec<(String, CountryInfo)>,
) -> Result<()> {
let file = fs::File::open(&filename).context(format!(
"Could not open population file {:?}",
filename.as_ref()
))?;
let file_reader = BufReader::new(file);
let mut csv_reader = csv::ReaderBuilder::new()
.flexible(true)
.from_reader(file_reader);
for record_result in csv_reader.records() {
let record = record_result?;
if record.len() < 3 {
continue;
}
let alpha3 = &record[1];
if let Some((_, info)) = countries_info_list
.iter_mut()
.find(|(_, info)| info.alpha3 == alpha3)
{
info.population = if record.iter().last().unwrap().trim().is_empty() {
&record[record.len() - 2]
} else {
record.iter().last().unwrap()
}
.parse()
.unwrap()
}
}
Ok(())
}
2 changes: 2 additions & 0 deletions keshvar-code-generator/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub struct CountryInfo {
pub eu_member: Option<bool>,
pub vat_rates: Option<VatRates>,
pub distance_unit: DistanceUnit,
#[serde(default)]
pub population: u64,
}

#[derive(Clone, Debug, Copy, Serialize, Deserialize)]
Expand Down
14 changes: 8 additions & 6 deletions src/countries/ad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub mod consts {
pub const EU_MEMBER: bool = false;
pub const EEA_MEMBER: bool = false;
pub const DISTANCE_UNIT: DistanceUnit = DistanceUnit::Km;
pub const POPULATION: Option<u64> = Some(79824);
#[cfg(feature = "emojis")]
pub const EMOJI: &str = "🇦🇩";
#[cfg(feature = "translations")]
Expand Down Expand Up @@ -423,25 +424,25 @@ pub fn new() -> Country {
continent: Continent::Europe,
country_code: 376,
currency_code: CurrencyCode::EUR,
gec: Some(GEC::AN),
maybe_gec: Some(GEC::AN),
#[cfg(feature = "geo")]
geo: geo::new(),
international_prefix: "00",
ioc: Some(IOC::AND),
maybe_ioc: Some(IOC::AND),
iso_long_name: "The Principality of Andorra",
iso_short_name: "Andorra",
official_language_list: ["ca"].to_vec(),
spoken_language_list: ["ca"].to_vec(),
national_destination_code_length_list: [2].to_vec(),
national_number_length_list: [6, 7, 8, 9].to_vec(),
national_prefix: "None",
nationality: Some("Andorran"),
maybe_nationality: Some("Andorran"),
number: "020",
postal_code: true,
postal_code_format: Some("AD[1-7]0\\d"),
region: Some(Region::Europe),
maybe_region: Some(Region::Europe),
start_of_week: WeekDay::Monday,
subregion: Some(SubRegion::SouthernEurope),
maybe_subregion: Some(SubRegion::SouthernEurope),
un_locode: "AD",
unofficial_name_list: ["Andorre", "Andorra", "アンドラ"].to_vec(),
world_region: WorldRegion::EMEA,
Expand Down Expand Up @@ -590,7 +591,8 @@ pub fn new() -> Country {
g20_member: false,
eu_member: false,
eea_member: false,
vat_rates: None,
maybe_vat_rates: None,
distance_unit: DistanceUnit::Km,
maybe_population: Some(79824),
}
}
14 changes: 8 additions & 6 deletions src/countries/ae.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub mod consts {
pub const EU_MEMBER: bool = false;
pub const EEA_MEMBER: bool = false;
pub const DISTANCE_UNIT: DistanceUnit = DistanceUnit::Km;
pub const POPULATION: Option<u64> = Some(9441129);
#[cfg(feature = "emojis")]
pub const EMOJI: &str = "🇦🇪";
#[cfg(feature = "translations")]
Expand Down Expand Up @@ -895,25 +896,25 @@ pub fn new() -> Country {
continent: Continent::Asia,
country_code: 971,
currency_code: CurrencyCode::AED,
gec: Some(GEC::AE),
maybe_gec: Some(GEC::AE),
#[cfg(feature = "geo")]
geo: geo::new(),
international_prefix: "00",
ioc: Some(IOC::UAE),
maybe_ioc: Some(IOC::UAE),
iso_long_name: "The United Arab Emirates",
iso_short_name: "United Arab Emirates",
official_language_list: ["ar"].to_vec(),
spoken_language_list: ["ar"].to_vec(),
national_destination_code_length_list: [2].to_vec(),
national_number_length_list: [7, 8, 9].to_vec(),
national_prefix: "0",
nationality: Some("Emirian"),
maybe_nationality: Some("Emirian"),
number: "784",
postal_code: false,
postal_code_format: None,
region: Some(Region::Asia),
maybe_region: Some(Region::Asia),
start_of_week: WeekDay::Monday,
subregion: Some(SubRegion::WesternAsia),
maybe_subregion: Some(SubRegion::WesternAsia),
un_locode: "AE",
unofficial_name_list: ["United Arab Emirates", "الإمارات العربية المتحدة", "Vereinigte Arabische Emirate", "Émirats Arabes Unis", "Emiratos Árabes Unidos", "アラブ首長国連邦", "Verenigde Arabische Emiraten"].to_vec(),
world_region: WorldRegion::EMEA,
Expand All @@ -927,7 +928,8 @@ pub fn new() -> Country {
g20_member: false,
eu_member: false,
eea_member: false,
vat_rates: Some(VatRates{standard: 5.0, reduced: Vec::from([]), super_reduced: None, parking: None}),
maybe_vat_rates: Some(VatRates{standard: 5.0, reduced: Vec::from([]), super_reduced: None, parking: None}),
distance_unit: DistanceUnit::Km,
maybe_population: Some(9441129),
}
}
14 changes: 8 additions & 6 deletions src/countries/af.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub mod consts {
pub const EU_MEMBER: bool = false;
pub const EEA_MEMBER: bool = false;
pub const DISTANCE_UNIT: DistanceUnit = DistanceUnit::Km;
pub const POPULATION: Option<u64> = Some(41128771);
#[cfg(feature = "emojis")]
pub const EMOJI: &str = "🇦🇫";
#[cfg(feature = "translations")]
Expand Down Expand Up @@ -777,25 +778,25 @@ pub fn new() -> Country {
continent: Continent::Asia,
country_code: 93,
currency_code: CurrencyCode::AFN,
gec: Some(GEC::AF),
maybe_gec: Some(GEC::AF),
#[cfg(feature = "geo")]
geo: geo::new(),
international_prefix: "00",
ioc: Some(IOC::AFG),
maybe_ioc: Some(IOC::AFG),
iso_long_name: "The Islamic Republic of Afghanistan",
iso_short_name: "Afghanistan",
official_language_list: ["ps", "tk", "uz"].to_vec(),
spoken_language_list: ["ps", "tk", "uz"].to_vec(),
national_destination_code_length_list: [2].to_vec(),
national_number_length_list: [8, 9].to_vec(),
national_prefix: "0",
nationality: Some("Afghan"),
maybe_nationality: Some("Afghan"),
number: "004",
postal_code: true,
postal_code_format: Some("\\d{4}"),
region: Some(Region::Asia),
maybe_region: Some(Region::Asia),
start_of_week: WeekDay::Monday,
subregion: Some(SubRegion::SouthernAsia),
maybe_subregion: Some(SubRegion::SouthernAsia),
un_locode: "AF",
unofficial_name_list: ["Afghanistan", "Afganistán", "アフガニスタン"].to_vec(),
world_region: WorldRegion::APAC,
Expand Down Expand Up @@ -947,7 +948,8 @@ pub fn new() -> Country {
g20_member: false,
eu_member: false,
eea_member: false,
vat_rates: None,
maybe_vat_rates: None,
distance_unit: DistanceUnit::Km,
maybe_population: Some(41128771),
}
}
14 changes: 8 additions & 6 deletions src/countries/ag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub mod consts {
pub const EU_MEMBER: bool = false;
pub const EEA_MEMBER: bool = false;
pub const DISTANCE_UNIT: DistanceUnit = DistanceUnit::Km;
pub const POPULATION: Option<u64> = Some(93763);
#[cfg(feature = "emojis")]
pub const EMOJI: &str = "🇦🇬";
#[cfg(feature = "translations")]
Expand Down Expand Up @@ -388,25 +389,25 @@ pub fn new() -> Country {
continent: Continent::NorthAmerica,
country_code: 1,
currency_code: CurrencyCode::XCD,
gec: Some(GEC::AC),
maybe_gec: Some(GEC::AC),
#[cfg(feature = "geo")]
geo: geo::new(),
international_prefix: "011",
ioc: Some(IOC::ANT),
maybe_ioc: Some(IOC::ANT),
iso_long_name: "Antigua and Barbuda",
iso_short_name: "Antigua and Barbuda",
official_language_list: ["en"].to_vec(),
spoken_language_list: ["en"].to_vec(),
national_destination_code_length_list: [3].to_vec(),
national_number_length_list: [10].to_vec(),
national_prefix: "1",
nationality: Some("Antiguan, Barbudan"),
maybe_nationality: Some("Antiguan, Barbudan"),
number: "028",
postal_code: false,
postal_code_format: None,
region: Some(Region::Americas),
maybe_region: Some(Region::Americas),
start_of_week: WeekDay::Monday,
subregion: Some(SubRegion::Caribbean),
maybe_subregion: Some(SubRegion::Caribbean),
un_locode: "AG",
unofficial_name_list: ["Antigua and Barbuda", "Antigua und Barbuda", "Antigua et Barbuda", "Antigua y Barbuda", "アンティグア・バーブーダ", "Antigua en Barbuda"].to_vec(),
world_region: WorldRegion::AMER,
Expand All @@ -420,7 +421,8 @@ pub fn new() -> Country {
g20_member: false,
eu_member: false,
eea_member: false,
vat_rates: None,
maybe_vat_rates: None,
distance_unit: DistanceUnit::Km,
maybe_population: Some(93763),
}
}
Loading

0 comments on commit b01f6f9

Please sign in to comment.