| Crates.io | nationify |
| lib.rs | nationify |
| version | 0.2.5 |
| created_at | 2024-12-09 19:12:15.04344+00 |
| updated_at | 2026-01-03 12:08:00.144725+00 |
| description | A library that provide information about countries |
| homepage | |
| repository | https://github.com/mohamadzoh/nationify |
| max_upload_size | |
| id | 1477734 |
| size | 1,166,878 |
Nationify is a Rust library designed for querying and managing country-related data. It provides an intuitive interface for retrieving information such as ISO codes, names, regions, languages, and geographical data.
Add nationify as a dependency in your Cargo.toml:
[dependencies]
nationify = "0.2.5"
The nationify crate provides the following features - by default all are enabled:
Get a list of all country ISO codes.
use nationify::iso_codes;
fn main() {
let codes = iso_codes();
println!("ISO Codes: {:?}", codes);
}
Fetch all country names.
use nationify::country_names;
fn main() {
let names = country_names();
println!("Country Names: {:?}", names);
}
Find a country by its ISO code or name.
use nationify::{by_iso_code, by_country_name};
fn main() {
if let Some(country) = by_iso_code("US") {
println!("Country by ISO Code: {:?}", country);
}
if let Some(country) = by_country_name("United States") {
println!("Country by Name: {:?}", country);
}
}
Perform case-insensitive searches for countries.
use nationify::by_country_name_or_code_case_insensitive;
fn main() {
if let Some(country) = by_country_name_or_code_case_insensitive("united states") {
println!("Case-Insensitive Search: {:?}", country);
}
}
Fetch a list of unique continents, regions, or subregions.
use nationify::{continents, regions, world_subregions};
fn main() {
println!("Continents: {:?}", continents());
println!("Regions: {:?}", regions());
println!("Subregions: {:?}", world_subregions());
}
Filter countries by specific geographical areas.
use nationify::{by_region, by_continent, by_subregion};
fn main() {
let african_countries = by_region("Africa");
println!("Countries in Africa: {:?}", african_countries);
let asian_countries = by_continent("Asia");
println!("Countries in Asia: {:?}", asian_countries);
let subregion_countries = by_subregion("Northern Europe");
println!("Countries in Northern Europe: {:?}", subregion_countries);
}
Search countries based on official or spoken languages.
use nationify::{by_languages_official, by_languages_spoken};
fn main() {
let english_official = by_languages_official("English");
println!("Countries where English is an official language: {:?}", english_official);
let english_spoken = by_languages_spoken("English");
println!("Countries where English is spoken: {:?}", english_spoken);
}
Access a comprehensive database of 1,609 ports across 185 countries with rich search and filtering capabilities.
use nationify::*;
// Get all ports
let all = all_ports();
println!("Total ports: {}", all.len());
// Get port statistics
let stats = port_statistics();
println!("Countries with ports: {}", stats.total_countries);
// Get unique lists
let countries = port_countries(); // All countries with ports
let cities = port_cities(); // All cities with ports
let timezones = port_timezones(); // All timezones
// Find nearest port to coordinates
if let Some(port) = closest_port(25.2048, 55.2708) {
println!("Closest: {}", port.display_name());
println!("Distance: {:.2} km", port.distance_to(25.2048, 55.2708));
println!("Distance: {:.2} nm", port.distance_to_nautical_miles(25.2048, 55.2708));
}
// Find N closest ports
let nearest = closest_ports(40.7128, -74.0060, 5);
for port in nearest {
println!("{}", port.display_name());
}
// Search by country, city, name
let uae_ports = ports_by_country("United Arab Emirates");
let dubai_ports = ports_by_city("Dubai");
let named_ports = ports_by_name("Singapore");
// Search by state/timezone
let ca_ports = ports_by_state("California");
let tz_ports = ports_by_timezone("Asia/Dubai");
// Find by codes
let port = port_by_unloc("AEDXB").unwrap();
let port = port_by_code("52005").unwrap();
// General search (searches name, city, code, unlocs)
let results = search_ports("Singapore");
// Find ports within radius (km)
let nearby = ports_within_radius(1.3521, 103.8198, 100.0);
// Find ports in bounding box
let bbox_ports = ports_in_bounding_box(30.0, 45.0, -5.0, 40.0);
// Find ports by region
let regional = ports_by_region("Southeast Asia");
let port = closest_port(25.2048, 55.2708).unwrap();
// Distance calculations
port.distance_to(lat, lng); // Kilometers
port.distance_to_nautical_miles(lat, lng); // Nautical miles
port.distance_to_miles(lat, lng); // Miles
// Field matching (case-insensitive)
port.matches_code("AEDXB");
port.matches_city("Dubai");
port.matches_country("United Arab Emirates");
port.matches_state("Dubai");
port.matches_name("Dubai");
port.matches_timezone("Asia/Dubai");
// Other helpers
port.has_unloc("AEDXB");
port.has_alias("alternate_name");
port.serves_region("Middle East");
port.display_name(); // "Port Name, City, Country"
port.matches_search("dub");
// Use in filters for cleaner code
let results: Vec<_> = all_ports()
.iter()
.filter(|p| p.matches_country("United States") &&
p.matches_city("New York"))
.collect();
// Total count
let total = ports_count();
// Count by country
let us_count = ports_count_by_country("United States");
Access detailed metadata for countries.
use nationify::by_country_name;
fn main() {
if let Some(country) = by_country_name("United States") {
println!("ISO Code: {}", country.iso_code);
println!("Continent: {}", country.continent);
println!("Region: {}", country.region);
println!("Subregion: {}", country.subregion);
println!("Official Languages: {:?}", country.languages_official);
println!("Spoken Languages: {:?}", country.languages_spoken);
}
}
CountryThe Country struct provides comprehensive details for each country.
#[derive(Debug, Clone, PartialEq)]
pub struct Country<'a> {
pub iso_code: &'a str,
pub alpha3: &'a str,
pub continent: &'a str,
pub region: &'a str,
pub subregion: &'a str,
pub languages_official: &'a [&'a str],
pub languages_spoken: &'a [&'a str],
pub geo: Geo,
// ... additional fields
}
PortThe Port struct provides comprehensive details about ports worldwide.
#[derive(Debug, Clone, PartialEq)]
pub struct Port {
pub name: &'static str, // Port name
pub city: &'static str, // City
pub state: &'static str, // State/province
pub country: &'static str, // Country
pub latitude: f64, // Latitude
pub longitude: f64, // Longitude
pub timezone: &'static str, // Timezone (e.g., "Asia/Dubai")
pub unlocs: &'static [&'static str], // UN/LOCODE codes
pub code: &'static str, // Port code
pub port_code: &'static str, // Primary port code
pub alias: &'static [&'static str], // Alternative names
pub regions: &'static [&'static str], // Regions served
}
Port Methods:
distance_to(lat, lng) -> f64 - Distance in kilometersdistance_to_nautical_miles(lat, lng) -> f64 - Distance in nautical milesdistance_to_miles(lat, lng) -> f64 - Distance in mileshas_unloc(code) -> bool - Check if port has UNLOC codehas_alias(name) -> bool - Check if port has aliasserves_region(region) -> bool - Check if serves regiondisplay_name() -> String - Formatted "Name, City, Country"matches_search(query) -> bool - Check if matches search querymatches_code(code) -> bool - Check if matches port code (case-insensitive)matches_city(city) -> bool - Check if matches city (case-insensitive)matches_country(country) -> bool - Check if matches country (case-insensitive)matches_state(state) -> bool - Check if matches state (case-insensitive)matches_name(name) -> bool - Check if matches name (case-insensitive)matches_timezone(tz) -> bool - Check if matches timezone (case-insensitive)Available Port Functions:
all_ports() - Get all portsclosest_port(lat, lng) - Find nearest portclosest_ports(lat, lng, n) - Find N nearest portsports_by_country(country) - Filter by countryports_by_city(city) - Filter by cityports_by_name(name) - Filter by nameports_by_state(state) - Filter by stateports_by_timezone(tz) - Filter by timezoneport_by_unloc(code) - Find by UNLOC codeport_by_code(code) - Find by port codesearch_ports(query) - General searchports_within_radius(lat, lng, km) - Within radiusports_in_bounding_box(...) - In bounding boxports_by_region(region) - By regionport_countries() - List all countriesport_cities() - List all citiesport_timezones() - List all timezonesports_count() - Total countports_count_by_country(country) - Count by countryport_statistics() - Full statisticsGeo and BoundsGeographical data includes latitude, longitude, and boundary information.
all_ports() - Get all 1,609 portsclosest_port(lat, lng) - Find nearest portclosest_ports(lat, lng, n) - Find N nearest portsports_count() - Total port countports_by_country(country) - Filter by countryports_by_city(city) - Filter by cityports_by_name(name) - Filter by port nameports_by_state(state) - Filter by state/provinceports_by_timezone(tz) - Filter by timezoneports_by_region(region) - Filter by served regionsearch_ports(query) - General search across fieldsport_by_unloc(code) - Find by UN/LOCODEport_by_code(code) - Find by port codeports_within_radius(lat, lng, km) - Find within radiusports_in_bounding_box(min_lat, max_lat, min_lng, max_lng) - Find in bounding boxport_countries() - List all countries with portsport_cities() - List all cities with portsport_timezones() - List all timezonesports_count_by_country(country) - Count by countryport_statistics() - Get comprehensive statisticsAll matches_* methods are case-insensitive for easier filtering:
Distance Calculations:
distance_to(lat, lng) -> f64 - Kilometersdistance_to_nautical_miles(lat, lng) -> f64 - Nautical milesdistance_to_miles(lat, lng) -> f64 - MilesField Matching:
matches_code(code) -> bool - Match port codematches_city(city) -> bool - Match citymatches_country(country) -> bool - Match countrymatches_state(state) -> bool - Match statematches_name(name) -> bool - Match namematches_timezone(tz) -> bool - Match timezoneOther Methods:
has_unloc(code) -> bool - Check for UNLOC codehas_alias(name) -> bool - Check for aliasserves_region(region) -> bool - Check if serves regiondisplay_name() -> String - Returns "Name, City, Country"matches_search(query) -> bool - General searchRun the comprehensive test suite (52 tests):
cargo test --features ports
cargo test --all-features
Rusty Rails is a larger project aiming to bridge the gap between Rust and Ruby/Ruby on Rails. We are actively working on recreating ruby library into rust that seamlessly make working in rust more easy and fun for new developers.
Contributions to the Nationify library are welcome! Feel free to open issues, submit pull requests, or provide feedback to help improve this library.