#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Charset {
Symbol,
Unicode,
Gbk,
Gb2312,
Big5,
}
impl Charset {
#[must_use]
pub fn as_str(&self) -> &'static str {
match self {
Self::Symbol => "Symbol",
Self::Unicode => "Unicode",
Self::Gbk => "GBK",
Self::Gb2312 => "GB2312",
Self::Big5 => "Big5",
}
}
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> Option<Self> {
match s.trim() {
"Symbol" => Some(Self::Symbol),
"Unicode" => Some(Self::Unicode),
"GBK" => Some(Self::Gbk),
"GB2312" => Some(Self::Gb2312),
"Big5" => Some(Self::Big5),
_ => None,
}
}
}
impl std::fmt::Display for Charset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn charset_as_str() {
assert_eq!(Charset::Unicode.as_str(), "Unicode");
assert_eq!(Charset::Gbk.as_str(), "GBK");
}
#[test]
fn charset_from_str() {
assert_eq!(Charset::from_str("Unicode"), Some(Charset::Unicode));
assert_eq!(Charset::from_str("GBK"), Some(Charset::Gbk));
assert_eq!(Charset::from_str("Unknown"), None);
}
#[test]
fn charset_display() {
assert_eq!(Charset::Gb2312.to_string(), "GB2312");
}
}