fix: correct flag emoji for ISO alpha-3 region code (#5557)

* fix: correct flag emoji for ISO alpha-3 region code

* fix: use rust_iso3166 to convert ISO3 region code

* fix: validate ISO country code when generating flag emoji

* fix: correct icon encoding for unlock test in specific regions

---------

Co-authored-by: Tunglies <77394545+Tunglies@users.noreply.github.com>
This commit is contained in:
AetherWing
2025-11-24 20:40:32 +08:00
committed by GitHub
parent 38b306a438
commit 6b3f5eea16
4 changed files with 167 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
use chrono::Local;
use rust_iso3166;
pub fn get_local_date_string() -> String {
let now = Local::now();
@@ -6,16 +7,70 @@ pub fn get_local_date_string() -> String {
}
pub fn country_code_to_emoji(country_code: &str) -> String {
let country_code = country_code.to_uppercase();
if country_code.len() < 2 {
return String::new();
}
let uc = country_code.to_ascii_uppercase();
let bytes = country_code.as_bytes();
// 长度校验:仅允许 2 或 3
match uc.len() {
2 => {
// 校验是否是合法 alpha2
if rust_iso3166::from_alpha2(&uc).is_none() {
return String::new();
}
alpha2_to_emoji(&uc)
}
3 => {
// 转换并校验 alpha3
match rust_iso3166::from_alpha3(&uc) {
Some(c) => {
let alpha2 = c.alpha2.to_ascii_uppercase();
alpha2_to_emoji(&alpha2)
}
None => String::new(),
}
}
_ => String::new(),
}
}
fn alpha2_to_emoji(alpha2: &str) -> String {
let bytes = alpha2.as_bytes();
let c1 = 0x1F1E6 + (bytes[0] as u32) - ('A' as u32);
let c2 = 0x1F1E6 + (bytes[1] as u32) - ('A' as u32);
char::from_u32(c1)
.and_then(|c1| char::from_u32(c2).map(|c2| format!("{c1}{c2}")))
.and_then(|x| char::from_u32(c2).map(|y| format!("{x}{y}")))
.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::country_code_to_emoji;
#[test]
fn country_code_to_emoji_iso2() {
assert_eq!(country_code_to_emoji("CN"), "🇨🇳");
assert_eq!(country_code_to_emoji("us"), "🇺🇸");
}
#[test]
fn country_code_to_emoji_iso3() {
assert_eq!(country_code_to_emoji("CHN"), "🇨🇳");
assert_eq!(country_code_to_emoji("USA"), "🇺🇸");
}
#[test]
fn country_code_to_emoji_invalid() {
assert_eq!(country_code_to_emoji("XXX"), "");
assert_eq!(country_code_to_emoji("ZZ"), "");
}
#[test]
fn country_code_to_emoji_short() {
assert_eq!(country_code_to_emoji("C"), "");
assert_eq!(country_code_to_emoji(""), "");
}
#[test]
fn country_code_to_emoji_long() {
assert_eq!(country_code_to_emoji("CNAAA"), "");
}
}