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 if hash_set.len() != CHARSET_LEN {
92 return false;
93 }
94 true
95 }
96
97 /// Sets the character set for encoding/decoding operations.
98 ///
99 /// # Arguments
100 ///
101 /// - `&'b str` - The character set to use.
102 ///
103 /// # Returns
104 ///
105 /// - `&mut Self` - Self reference for method chaining.
106 pub fn charset<'b>(&mut self, charset: &'b str) -> &mut Self
107 where
108 'b: 'a,
109 {
110 if self.0 != Charset::default().0 {
111 return self;
112 }
113 self.0 = charset;
114 self
115 }
116
117 /// Encodes input string using current charset.
118 ///
119 /// # Arguments
120 ///
121 /// - `&str` - The string to encode.
122 ///
123 /// # Returns
124 ///
125 /// - `Result<String, EncodeError>` - Encoding result.
126 pub fn encode(&self, encode_str: &str) -> Result<String, EncodeError> {
127 Encode::execute(self.0, encode_str)
128 }
129
130 /// Decodes input string using current charset.
131 ///
132 /// # Arguments
133 ///
134 /// - `&str` - The string to decode.
135 ///
136 /// # Returns
137 ///
138 /// - `Result<String, DecodeError>` - Decoding result.
139 pub fn decode(&self, decode_str: &str) -> Result<String, DecodeError> {
140 Decode::execute(self.0, decode_str)
141 }
142}