use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SupportedEncodings {
Base64,
Hex,
}
impl SupportedEncodings {
pub fn detect(data: &str) -> Self {
if data.chars().all(|c| c.is_ascii_hexdigit()) {
Self::Hex
} else {
Self::Base64
}
}
pub fn as_str(&self) -> &'static str {
match self {
Self::Base64 => "base64",
Self::Hex => "hex",
}
}
}
impl fmt::Display for SupportedEncodings {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl std::str::FromStr for SupportedEncodings {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"base64" => Ok(Self::Base64),
"hex" => Ok(Self::Hex),
_ => Err(format!(
"Unsupported encoding format: {s}. Supported formats are: base64, hex"
)),
}
}
}