7 releases
| 0.3.2 | Sep 21, 2025 |
|---|---|
| 0.3.1 | Sep 21, 2025 |
| 0.2.0 | Nov 27, 2021 |
| 0.1.2 | Oct 26, 2020 |
#34 in Internationalization (i18n)
1,062 downloads per month
Used in railwayapp
47KB
725 lines
country-emoji
A lightweight, fast Rust library that converts between country names, ISO 3166-1 codes and flag emojis. Features intelligent fuzzy matching, normalization, and comprehensive country data.
Features
- โก Fast lookups - Optimized for performance with pre-compiled regex patterns
- ๐ง Fuzzy matching - Handles alternative names, government titles, and formatting variations
- ๐ Comprehensive data - All ISO 3166-1 countries including recent additions
- โจ Normalization - Handles diacritics, case-insensitivity, whitespace, and abbreviations
- ๐ Bidirectional conversion - Convert between any combination of codes, names, and flag emojis
- ๐ Zero-copy - Returns string slices where possible for optimal memory usage
Usage
Basic Operations
use country_emoji::{flag, code, name};
// Generate flag emoji from country code
assert_eq!(flag("US"), Some("๐บ๐ธ".to_string())); // โ ๐บ๐ธ
// Extract country code from flag emoji
assert_eq!(code("๐จ๐ฆ"), Some("CA")); // โ "CA"
// Get country name from code
assert_eq!(name("DE"), Some("Germany")); // โ "Germany"
// Convert country name to code
assert_eq!(code("Japan"), Some("JP")); // โ "JP"
```### Advanced Fuzzy Matching
The library handles many name variations and formats intelligently:
```rust
use country_emoji::code;
// Alternative names and abbreviations
assert_eq!(code("UK"), Some("GB")); // โ ๐ฌ๐ง
assert_eq!(code("UAE"), Some("AE")); // โ ๐ฆ๐ช
assert_eq!(code("Russia"), Some("RU")); // โ ๐ท๐บ
// Government titles and formal names
assert_eq!(code("Republic of Moldova"), Some("MD")); // โ ๐ฒ๐ฉ
assert_eq!(code("Democratic People's Republic of Korea"), Some("KP")); // โ ๐ฐ๐ต
assert_eq!(code("United States of America"), Some("US")); // โ ๐บ๐ธ
// Comma-reversed formats
assert_eq!(code("Virgin Islands, British"), Some("VG")); // โ ๐ป๐ฌ
assert_eq!(code("Korea, Republic of"), Some("KR")); // โ ๐ฐ๐ท
// Saint/St. normalization
assert_eq!(code("Saint Lucia"), Some("LC")); // โ ๐ฑ๐จ
assert_eq!(code("St. Lucia"), Some("LC"));
assert_eq!(code("St Lucia"), Some("LC"));
// And/ampersand equivalence
assert_eq!(code("Bosnia and Herzegovina"), Some("BA")); // โ ๐ง๐ฆ
assert_eq!(code("Bosnia & Herzegovina"), Some("BA"));
// Diacritic handling
assert_eq!(code("Cote d'Ivoire"), Some("CI")); // โ ๐จ๐ฎ
assert_eq!(code("Cรดte d'Ivoire"), Some("CI"));
// Partial matching for unique names
assert_eq!(code("Vatican"), Some("VA")); // โ ๐ป๐ฆ
Direct API Functions
For explicit conversions, use the direct API:
use country_emoji::{code_to_flag, flag_to_code, name_to_code, code_to_name, is_code, is_country_flag};
assert_eq!(code_to_flag("FR"), Some("๐ซ๐ท".to_string())); // โ ๐ซ๐ท
assert_eq!(flag_to_code("๐ฎ๐น"), Some("IT")); // โ "IT"
assert_eq!(name_to_code("Spain"), Some("ES")); // โ "ES"
assert_eq!(code_to_name("BR"), Some("Brazil")); // โ "Brazil"
assert!(is_code(Some("CA"))); // โ
Valid
assert!(is_country_flag("๐ฏ๐ต")); // โ
Valid
Error Handling
The library returns None for invalid or ambiguous inputs:
use country_emoji::code;
// Invalid inputs
assert_eq!(code("ZZ"), None); // โ Invalid
assert_eq!(code("Atlantis"), None); // โ Non-existent
// Ambiguous inputs (prevents false matches)
assert_eq!(code("Korea"), None); // โ Ambiguous (๐ฐ๐ต or ๐ฐ๐ท?)
assert_eq!(code("United"), None); // โ Too vague
Performance
This library is optimized for high performance:
- Pre-compiled regex patterns for fast text normalization
- Cached normalized data to avoid repeated processing
- Early exit strategies in matching algorithms
- Benchmarked - Run
cargo benchto see performance metrics
Typical performance (release build):
- Exact matches: ~40ฮผs for 10 lookups
- Fuzzy matches: ~800ฮผs for 10 complex queries
- Flag operations: ~500ns per conversion
Country Data
The library includes comprehensive country data:
- All 249 ISO 3166-1 assigned codes
- Current country names (e.g., "North Macedonia", "Eswatini")
- Alternative names and historical names
- Common abbreviations (UK, UAE, USA, etc.)
- Territories and dependencies (Puerto Rico, Guam, etc.)
- Recent additions (South Sudan, Curaรงao, Sint Maarten)
Installation
Add this to your Cargo.toml:
[dependencies]
country-emoji = "0.3"
Related Libraries
Don't need Rust? Check out these alternatives:
- JavaScript: country-emoji - Original inspiration
- Swift: SwiftFlags
Contributing & Feedback
- Bug reports: GitHub Issues
- Support development: Patreon
- Contact: leodutra.br+foss@gmail.com or @leodutra
Credits
This library builds upon excellent prior work:
- country-emoji (JavaScript) - Original concept and API design
- flag-emoji-from-country-code - Flag emoji generation technique
License
MIT @ Leo Dutra
Dependencies
~5MB
~118K SLoC