use crate::*;
impl fmt::Display for EncodeError {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EncodeError::CharsetError => write!(
f,
"EncodeError: Charset is invalid. Please ensure the charset contains exactly {CHARSET_LEN} unique characters."
),
}
}
}
impl fmt::Display for DecodeError {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DecodeError::CharsetError => write!(
f,
"DecodeError: Charset is invalid. Please ensure the charset contains exactly {CHARSET_LEN} unique characters."
),
}
}
}
impl<'a> Default for Charset<'a> {
#[inline(always)]
fn default() -> Self {
Charset("")
}
}
impl<'a> Charset<'a> {
#[inline(always)]
pub fn new() -> Self {
Charset::default()
}
pub(crate) fn judge_charset_safe(charset: &str) -> bool {
let mut hash_set: HashSet<char> = HashSet::new();
for tmp_char in charset.chars() {
hash_set.insert(tmp_char);
}
if hash_set.len() != CHARSET_LEN {
return false;
}
true
}
pub fn charset<'b>(&mut self, charset: &'b str) -> &mut Self
where
'b: 'a,
{
if self.0 != Charset::default().0 {
return self;
}
self.0 = charset;
self
}
pub fn encode(&self, encode_str: &str) -> Result<String, EncodeError> {
Encode::execute(self.0, encode_str)
}
pub fn decode(&self, decode_str: &str) -> Result<String, DecodeError> {
Decode::execute(self.0, decode_str)
}
}