use crate::CountryCode;
use core::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IpTag {
Country(CountryCode),
Private,
Special,
Unknown,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TagFormat {
FlagAndCode,
FlagOnly,
CodeOnly,
DefaultText,
}
impl IpTag {
pub fn emoji(&self) -> String {
match self {
IpTag::Country(code) => code.flag().unwrap_or_else(|| "π".to_string()),
IpTag::Private => "π ".to_string(),
IpTag::Special => "βοΈ".to_string(),
IpTag::Unknown => "π".to_string(),
}
}
pub fn format(&self, fmt: TagFormat) -> String {
match (self, fmt) {
(IpTag::Country(code), TagFormat::FlagAndCode) => {
let flag = code.flag().unwrap_or_else(|| "π".to_string());
format!("{} {}", flag, code.to_string())
}
(IpTag::Country(code), TagFormat::FlagOnly) => {
code.flag().unwrap_or_else(|| "π".to_string())
}
(IpTag::Country(code), TagFormat::CodeOnly) => code.to_string(),
_ => self.to_string(),
}
}
}
impl fmt::Display for IpTag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
IpTag::Country(code) => {
let flag = code.flag().unwrap_or_else(|| "π".to_string());
write!(f, "{} {}", flag, code.to_string())
}
IpTag::Private => write!(f, "π PRIVATE"),
IpTag::Special => write!(f, "βοΈ SPECIAL"),
IpTag::Unknown => write!(f, "π UNKNOWN"),
}
}
}