Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@
name = "rust_iso3166"
version = "0.1.14"
edition = "2021"
description="ISO 3166-1 (Codes for the representation of names of countries and their subdivisions – Part 1: Country codes) is a standard defining codes for the names of countries, dependent territories, and special areas of geographical interest. It is the first part of the ISO 3166 standard published by the International Organization for Standardization."
repository="https://github.com/rust-iso/rust_iso3166"
license="Apache-2.0"
description = "ISO 3166-1 (Codes for the representation of names of countries and their subdivisions – Part 1: Country codes) is a standard defining codes for the names of countries, dependent territories, and special areas of geographical interest. It is the first part of the ISO 3166 standard published by the International Organization for Standardization."
repository = "https://github.com/rust-iso/rust_iso3166"
license = "Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
exclude = ["src/*.py"]
documentation = "https://docs.rs/rust_iso3166/"
keywords=["ISO3166", "ISO3166-1", "ISO3166-2", "ISO3166-3"]
keywords = ["ISO3166", "ISO3166-1", "ISO3166-2", "ISO3166-3"]

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = []
serde = ["dep:serde"]

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(direct_wasm)'] }


[dependencies]
phf = { version = "^0.11.1", features = ["macros"] }
serde = { version = "^1.0.228", optional = true }

[dev-dependencies]
serde_json = { version = "^1.0.107" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
prettytable-rs = "^0.10"
Expand Down
78 changes: 36 additions & 42 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,49 @@
#[cfg(not(target_arch = "wasm32"))]
fn main() {
use prettytable::{row, Table};
use std::env;
use prettytable::{row, Table};
use std::env;

let mut args = env::args();
let script_name = match args.next() {
Some(arg) => arg,
None => String::from(""),
};
let query = match args.next() {
Some(arg) => arg,
None => String::from(""),
};
let query = &query.to_lowercase();
let mut args = env::args();
let script_name = args.next().unwrap_or_default();
let query = args.next().unwrap_or_default();
let query = &query.to_lowercase();

eprintln!("Usage: {} [query]", script_name);
let mut found = false;
let mut table = Table::new();
table.add_row(row!["Name", "Alpha2", "Alpha3", "Numeric"]);
eprintln!("Usage: {} [query]", script_name);
let mut found = false;
let mut table = Table::new();
table.add_row(row!["Name", "Alpha2", "Alpha3", "Numeric"]);

for country in rust_iso3166::ALL {
if country.alpha2.to_lowercase().contains(query)
|| country.alpha3.to_lowercase().contains(query)
|| country.numeric_str().to_lowercase().contains(query)
{
table.add_row(row![
country.name,
country.alpha2,
country.alpha3,
country.numeric_str()
]);
found = true;
for country in rust_iso3166::ALL {
if country.alpha2.to_lowercase().contains(query)
|| country.alpha3.to_lowercase().contains(query)
|| country.numeric_str().to_lowercase().contains(query)
{
table.add_row(row![
country.name,
country.alpha2,
country.alpha3,
country.numeric_str()
]);
found = true;
}
}
}

if !found {
for country in rust_iso3166::ALL {
if country.name.to_lowercase().contains(query) {
table.add_row(row![
country.name,
country.alpha2,
country.alpha3,
country.numeric_str()
]);
}
if !found {
for country in rust_iso3166::ALL {
if country.name.to_lowercase().contains(query) {
table.add_row(row![
country.name,
country.alpha2,
country.alpha3,
country.numeric_str()
]);
}
}
}
}
table.printstd();
table.printstd();
}

#[cfg(target_arch = "wasm32")]
fn main() {
unimplemented!();
unimplemented!();
}
46 changes: 34 additions & 12 deletions src/iso3166_2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

use phf::phf_map;
use phf::Map;
#[cfg(all(direct_wasm,target_arch = "wasm32"))]
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(all(direct_wasm, target_arch = "wasm32"))]
use wasm_bindgen::prelude::*;

/// # Sample code
Expand All @@ -11,12 +12,12 @@ use wasm_bindgen::prelude::*;
/// assert!(subdivisions.unwrap().len() > 0);
/// let country = rust_iso3166::iso3166_2::from_code("GB-EDH");
/// assert_eq!("Edinburgh, City of", country.unwrap().name);
/// println!("{:?}", rust_iso3166::iso3166_2::SUBDIVISION_COUNTRY_MAP);
/// println!("{:?}", rust_iso3166::iso3166_2::SUBDIVISION_MAP);
/// println!("{:?}", rust_iso3166::iso3166_2::SUBDIVISION_COUNTRY_MAP);
/// println!("{:?}", rust_iso3166::iso3166_2::SUBDIVISION_MAP);
/// ```

/// Data for each Country Code defined by ISO 3166-2
#[cfg(all(direct_wasm,target_arch = "wasm32"))]
#[cfg(all(direct_wasm, target_arch = "wasm32"))]
#[wasm_bindgen]
#[derive(Debug, Ord, PartialOrd, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Subdivision {
Expand All @@ -34,7 +35,7 @@ pub struct Subdivision {
region_code: &'static str,
}

#[cfg(all(direct_wasm,target_arch = "wasm32"))]
#[cfg(all(direct_wasm, target_arch = "wasm32"))]
#[wasm_bindgen]
impl Subdivision {
#[wasm_bindgen(getter)]
Expand Down Expand Up @@ -68,7 +69,7 @@ impl Subdivision {
}
}

#[cfg(any(not(direct_wasm),not(target_arch = "wasm32")))]
#[cfg(any(not(direct_wasm), not(target_arch = "wasm32")))]
#[derive(Debug, Ord, PartialOrd, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Subdivision {
///Name
Expand Down Expand Up @@ -96,6 +97,30 @@ pub fn from_code(code: &str) -> Option<Subdivision> {
SUBDIVISION_MAP.get(code).cloned()
}

#[cfg(feature = "serde")]
impl Serialize for Subdivision {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.code)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Subdivision {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::Error;
let s = String::deserialize(deserializer)?;
let s_upper = s.to_uppercase();
from_code(&s_upper)
.ok_or_else(|| D::Error::custom(format!("Invalid ISO 3166-2 code: {}", s)))
}
}

pub const AF_BAL: Subdivision = Subdivision {
name: "Balkh",
code: "AF-BAL",
Expand Down Expand Up @@ -46140,8 +46165,7 @@ pub const ZW_MW: Subdivision = Subdivision {
region_code: "MW",
};


///Subdivision map with Code key
///Subdivision map with Code key
pub const SUBDIVISION_MAP: Map<&str, Subdivision> = phf_map! {

"AF-BAL" => AF_BAL,
Expand Down Expand Up @@ -51263,8 +51287,7 @@ pub const SUBDIVISION_MAP: Map<&str, Subdivision> = phf_map! {

};


///Subdivision map with Code key
///Subdivision map with Code key
pub const SUBDIVISION_COUNTRY_MAP: Map<&str, &[Subdivision]> = phf_map! {

"AF" => &[
Expand Down Expand Up @@ -56885,4 +56908,3 @@ ZW_MW,
],

};

Loading