Nationify
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.
Installation
Add nationify as a dependency in your Cargo.toml:
[]
= "0.2.5"
Crate Features
The nationify crate provides the following features - by default all are enabled:
- serde - Enables Serde serialization of definitions.
- phf - Enables compile-time country lookup by ISO code.
- iso_code - The ISO 3166-1 alpha-2 code of the country.
- alpha3 - The ISO 3166-1 alpha-3 code of the country.
- continent - The continent the country is located in.
- country_code - The country code.
- currency_code - The currency code used in the country.
- distance_unit - The distance unit used in the country.
- economic_unions - The economic unions the country is part of.
- gec - The GEC (Geographic Encoding Class) code.
- geo - The geographic information.
- international_prefix - The international dialing prefix.
- ioc - The IOC (International Olympic Committee) code.
- iso_long_name - The long name of the country in ISO format.
- iso_short_name - The short name of the country in ISO format.
- languages_official - The official languages of the country.
- languages_spoken - The spoken languages in the country.
- national_destination_code_lengths - The national destination code lengths.
- national_number_lengths - The national number lengths.
- national_prefix - The national prefix.
- nationality - The nationality.
- number - The country number.
- postal_code - Whether the country has a postal code.
- postal_code_format - The postal code format.
- region - The region the country is located in.
- start_of_week - The start of the week in the country.
- subregion - The subregion the country is located in.
- un_locode - The UN/LOCODE of the country.
- unofficial_names - The unofficial names of the country.
- world_region - The world region the country is located in.
- ports - Ports information and lookup functionality.
Features & Examples
1. Retrieve All ISO Codes
Get a list of all country ISO codes.
use iso_codes;
2. Retrieve All Country Names
Fetch all country names.
use country_names;
3. Search by ISO Code or Country Name
Find a country by its ISO code or name.
use ;
4. Case-Insensitive Search
Perform case-insensitive searches for countries.
use by_country_name_or_code_case_insensitive;
5. Retrieve Continents, Regions, and Subregions
Fetch a list of unique continents, regions, or subregions.
use ;
6. Filter by Region, Continent, or Subregion
Filter countries by specific geographical areas.
use ;
7. Query by Languages
Search countries based on official or spoken languages.
use ;
8. Ports - Comprehensive Port Database & Search
Access a comprehensive database of 1,609 ports across 185 countries with rich search and filtering capabilities.
Port Database Statistics
- Total Ports: 1,609
- Countries: 185
- Cities: 1,567
- Timezones: 221
Basic Port Queries
use *;
// Get all ports
let all = all_ports;
println!;
// Get port statistics
let stats = port_statistics;
println!;
// 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 Closest Ports
// Find nearest port to coordinates
if let Some = closest_port
// Find N closest ports
let nearest = closest_ports;
for port in nearest
Search and Filter Ports
// Search by country, city, name
let uae_ports = ports_by_country;
let dubai_ports = ports_by_city;
let named_ports = ports_by_name;
// Search by state/timezone
let ca_ports = ports_by_state;
let tz_ports = ports_by_timezone;
// Find by codes
let port = port_by_unloc.unwrap;
let port = port_by_code.unwrap;
// General search (searches name, city, code, unlocs)
let results = search_ports;
Geographic Queries
// Find ports within radius (km)
let nearby = ports_within_radius;
// Find ports in bounding box
let bbox_ports = ports_in_bounding_box;
// Find ports by region
let regional = ports_by_region;
Port Methods & Filtering
let port = closest_port.unwrap;
// Distance calculations
port.distance_to; // Kilometers
port.distance_to_nautical_miles; // Nautical miles
port.distance_to_miles; // Miles
// Field matching (case-insensitive)
port.matches_code;
port.matches_city;
port.matches_country;
port.matches_state;
port.matches_name;
port.matches_timezone;
// Other helpers
port.has_unloc;
port.has_alias;
port.serves_region;
port.display_name; // "Port Name, City, Country"
port.matches_search;
// Use in filters for cleaner code
let results: = all_ports
.iter
.filter
.collect;
Count Operations
// Total count
let total = ports_count;
// Count by country
let us_count = ports_count_by_country;
9. Additional Country Metadata
Access detailed metadata for countries.
use by_country_name;
Struct Definitions
Country
The Country struct provides comprehensive details for each country.
Port
The Port struct provides comprehensive details about ports worldwide.
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 statistics
Geo and Bounds
Geographical data includes latitude, longitude, and boundary information.
Ports API Reference
Port Database Overview
- Total Ports: 1,609
- Countries: 185
- Cities: 1,567
- Timezones: 221
Available Functions
Core Functions
all_ports()- Get all 1,609 portsclosest_port(lat, lng)- Find nearest portclosest_ports(lat, lng, n)- Find N nearest portsports_count()- Total port count
Search Functions
ports_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 fields
Lookup Functions
port_by_unloc(code)- Find by UN/LOCODEport_by_code(code)- Find by port code
Geographic Functions
ports_within_radius(lat, lng, km)- Find within radiusports_in_bounding_box(min_lat, max_lat, min_lng, max_lng)- Find in bounding box
Aggregation Functions
port_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 statistics
Port Methods
All 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- Miles
Field 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 timezone
Other 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 search
Testing
Run the comprehensive test suite (52 tests):
Rusty Rails Project
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.
Contributing
Contributions to the Nationify library are welcome! Feel free to open issues, submit pull requests, or provide feedback to help improve this library.