Crate country_emoji

Crate country_emoji 

Source
Expand description

§country-emoji

A lightweight, fast Rust library for converting 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

§Quick Start

use country_emoji::{flag, code, name};

// Convert country code to flag emoji
assert_eq!(flag("US"), Some("🇺🇸".to_string()));

// Convert flag emoji to country code  
assert_eq!(code("🇨🇦"), Some("CA"));

// Convert country code to name
assert_eq!(name("DE"), Some("Germany"));

// Convert country name to code (with fuzzy matching)
assert_eq!(code("United Kingdom"), Some("GB"));
assert_eq!(code("UAE"), Some("AE"));  // Handles abbreviations

§Advanced Fuzzy Matching

The library intelligently handles various name formats and variations:

use country_emoji::code;

// Government titles and formal names
assert_eq!(code("Republic of Korea"), Some("KR"));
assert_eq!(code("United States of America"), Some("US"));

// Saint/St. normalization  
assert_eq!(code("Saint Lucia"), Some("LC"));
assert_eq!(code("St. Lucia"), Some("LC"));

// Diacritic handling
assert_eq!(code("Cote d'Ivoire"), Some("CI"));
assert_eq!(code("Côte d'Ivoire"), Some("CI"));

// And/ampersand equivalence
assert_eq!(code("Bosnia and Herzegovina"), Some("BA"));
assert_eq!(code("Bosnia & Herzegovina"), Some("BA"));

§Direct API Functions

For explicit conversions when you know the input type:

use country_emoji::{code_to_flag, flag_to_code, name_to_code, code_to_name};

assert_eq!(code_to_flag("FR"), Some("🇫🇷".to_string()));
assert_eq!(flag_to_code("🇮🇹"), Some("IT"));
assert_eq!(name_to_code("Spain"), Some("ES"));
assert_eq!(code_to_name("BR"), Some("Brazil"));

Functions§

code
Convert a flag emoji or country name to its ISO 3166-1 alpha-2 code.
code_to_flag
Convert an ISO 3166-1 alpha-2 country code to its flag emoji.
code_to_name
Convert an ISO 3166-1 alpha-2 country code to its official name.
flag
Convert a country code or name to its flag emoji.
flag_to_code
Convert a country flag emoji to its ISO 3166-1 alpha-2 code.
is_code
Validate if an optional string is a valid ISO 3166-1 alpha-2 country code.
is_country_flag
Validate if a string represents a valid country flag emoji.
name
Convert a flag emoji or country code to its official country name.
name_to_code
Convert a country name to its ISO 3166-1 alpha-2 code using fuzzy matching.