bin_encode_decode/common/impl.rs
1use crate::*;
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 #[inline(always)]
60 fn default() -> Self {
61 Charset("")
62 }
63}
64
65impl<'a> Charset<'a> {
66 /// Creates a new Charset instance with default charset.
67 ///
68 /// # Returns
69 ///
70 /// - `Charset<'a>` - New instance with empty charset.
71 #[inline(always)]
72 pub fn new() -> Self {
73 Charset::default()
74 }
75
76 /// Validates if charset meets safety requirements.
77 ///
78 /// # Arguments
79 ///
80 /// - `&str` - Checks if the charset contains exactly CHARSET_LEN unique characters.
81 ///
82 /// # Returns
83 ///
84 /// - `bool` - Validation result.
85 pub(crate) fn judge_charset_safe(charset: &str) -> bool {
86 let mut hash_set: HashSet<char> = HashSet::new();
87 for tmp_char in charset.chars() {
88 hash_set.insert(tmp_char);
89 }
90 if hash_set.len() != CHARSET_LEN {
91 return false;
92 }
93 true
94 }
95
96 /// Sets the character set for encoding/decoding operations.
97 ///
98 /// # Arguments
99 ///
100 /// - `&'b str` - The character set to use.
101 ///
102 /// # Returns
103 ///
104 /// - `&mut Charset<'a>` - Self reference for method chaining.
105 pub fn charset<'b>(&mut self, charset: &'b str) -> &mut Self
106 where
107 'b: 'a,
108 {
109 if self.0 != Charset::default().0 {
110 return self;
111 }
112 self.0 = charset;
113 self
114 }
115
116 /// Encodes input string using current charset.
117 ///
118 /// # Arguments
119 ///
120 /// - `&str` - The string to encode.
121 ///
122 /// # Returns
123 ///
124 /// - `Result<String, EncodeError>` - Encoding result.
125 pub fn encode(&self, encode_str: &str) -> Result<String, EncodeError> {
126 Encode::execute(self.0, encode_str)
127 }
128
129 /// Decodes input string using current charset.
130 ///
131 /// # Arguments
132 ///
133 /// - `&str` - The string to decode.
134 ///
135 /// # Returns
136 ///
137 /// - `Result<String, DecodeError>` - Decoding result.
138 pub fn decode(&self, decode_str: &str) -> Result<String, DecodeError> {
139 Decode::execute(self.0, decode_str)
140 }
141}