Skip to main content

Crate country_emoji

Crate country_emoji 

Source
Expand description

§country-emoji

country-emoji provides conversions between country names, ISO 3166-1 alpha-2 codes, and Unicode flag emojis.

The crate supports exact lookups by code or flag emoji, plus alias-aware and normalized matching for country names.

§Features

  • Exact lookups backed by precomputed tables for codes, flags, and normalized names
  • Country-name normalization for case, whitespace, diacritics, and common abbreviations
  • Alias, formal-name, and fuzzy matching for text input
  • Bidirectional conversions between names, ISO codes, and flag emojis
  • Borrowed string returns for code and name APIs where possible

§Quick Start

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

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

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

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

// Convert a country name to a code.
assert_eq!(code("United Kingdom"), Some("GB"));
assert_eq!(code("UAE"), Some("AE"));

§Name Matching

Text lookups support several normalized and alias-based variations:

use country_emoji::code;

// Formal names and government titles.
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"));

§Explicit Conversion APIs

When the input type is already known, use the direct conversion functions:

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

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
Resolves a flag emoji or country-like text to an ISO 3166-1 alpha-2 code.
code_to_flag
Converts an ISO 3166-1 alpha-2 country code to its flag emoji.
code_to_name
Converts an ISO 3166-1 alpha-2 country code to the preferred country name.
flag
Resolves a country code or country-like text to a Unicode flag emoji.
flag_to_code
Converts a country flag emoji to its ISO 3166-1 alpha-2 code.
is_code
Returns whether an optional string is a valid ISO 3166-1 alpha-2 country code.
is_country_flag
Returns whether a string is a valid country flag emoji.
name
Resolves a flag emoji or ISO 3166-1 alpha-2 code to the preferred country name.
name_to_code
Resolves country-like text to an ISO 3166-1 alpha-2 code.