bin_encode_decode/common/impl.rs
1use super::*;
2
3/// Provides display formatting for EncodeError.
4///
5/// Implements human-readable error messages for encoding failures.
6impl fmt::Display for EncodeError {
7 /// Formats the EncodeError for display purposes.
8 ///
9 /// # Arguments
10 ///
11 /// - `&mut fmt::Formatter<'_>` - The formatter to use.
12 ///
13 /// # Returns
14 ///
15 /// - `fmt::Result` - Result of the formatting operation.
16 #[inline(always)]
17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18 match self {
19 EncodeError::CharsetError => write!(
20 f,
21 "EncodeError: Charset is invalid. Please ensure the charset contains exactly {CHARSET_LEN} unique characters."
22 ),
23 }
24 }
25}
26
27/// Provides display formatting for DecodeError.
28///
29/// Implements human-readable error messages for decoding failures.
30impl fmt::Display for DecodeError {
31 /// Formats the DecodeError for display purposes.
32 ///
33 /// # Arguments
34 ///
35 /// - `&mut fmt::Formatter<'_>` - The formatter to use.
36 ///
37 /// # Returns
38 ///
39 /// - `fmt::Result` - Result of the formatting operation.
40 #[inline(always)]
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 match self {
43 DecodeError::CharsetError => write!(
44 f,
45 "DecodeError: Charset is invalid. Please ensure the charset contains exactly {CHARSET_LEN} unique characters."
46 ),
47 }
48 }
49}
50
51/// Provides default implementation for Charset.
52///
53/// Creates a new Charset instance with empty string as default charset.
54///
55/// # Returns
56///
57/// - `Charset<'a>` - New instance with empty charset.
58impl<'a> Default for Charset<'a> {
59 /// Creates an empty charset instance.
60 #[inline(always)]
61 fn default() -> Self {
62 Charset("")
63 }
64}
65
66impl<'a> Charset<'a> {
67 /// Creates a new Charset instance with default charset.
68 ///
69 /// # Returns
70 ///
71 /// - `Charset<'a>` - New instance with empty charset.
72 #[inline(always)]
73 pub fn new() -> Self {
74 Charset::default()
75 }
76
77 /// Validates if charset meets safety requirements.
78 ///
79 /// # Arguments
80 ///
81 /// - `&str` - Checks if the charset contains exactly CHARSET_LEN unique characters.
82 ///
83 /// # Returns
84 ///
85 /// - `bool` - Validation result.
86 pub(crate) fn judge_charset_safe(charset: &str) -> bool {
87 let mut hash_set: HashSet<char> = HashSet::new();
88 for tmp_char in charset.chars() {
89 hash_set.insert(tmp_char);
90 }
91 hash_set.len() == CHARSET_LEN
92 }
93
94 /// Sets the character set for encoding/decoding operations.
95 ///
96 /// # Arguments
97 ///
98 /// - `&'b str` - The character set to use.
99 ///
100 /// # Returns
101 ///
102 /// - `&mut Self` - Self reference for method chaining.
103 pub fn charset<'b>(&mut self, charset: &'b str) -> &mut Self
104 where
105 'b: 'a,
106 {
107 if self.0 != Charset::default().0 {
108 return self;
109 }
110 self.0 = charset;
111 self
112 }
113
114 /// Encodes input string using current charset.
115 ///
116 /// # Arguments
117 ///
118 /// - `&str` - The string to encode.
119 ///
120 /// # Returns
121 ///
122 /// - `Result<String, EncodeError>` - Encoding result.
123 pub fn encode(&self, encode_str: &str) -> Result<String, EncodeError> {
124 Encode::execute(self.0, encode_str)
125 }
126
127 /// Decodes input string using current charset.
128 ///
129 /// # Arguments
130 ///
131 /// - `&str` - The string to decode.
132 ///
133 /// # Returns
134 ///
135 /// - `Result<String, DecodeError>` - Decoding result.
136 pub fn decode(&self, decode_str: &str) -> Result<String, DecodeError> {
137 Decode::execute(self.0, decode_str)
138 }
139}