lb64/error.rs
1use std::cmp::PartialEq;
2use std::fmt::{Display, Formatter};
3use std::mem;
4
5/// Possible Configuration errors when either setting or creating a new configuration that may occur
6#[derive(Debug)]
7pub enum ConfigError {
8 ///character set provided isn't of length 64
9 /// # Example:
10 /// ```
11 /// let character_set = &[
12 /// 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
13 /// 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
14 /// 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
15 /// 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+',
16 /// ]; // Throws Error because Length is 63 and not 64
17 /// ```
18 CharacterSetLengthError,
19 /// padding character provided is already used in character set
20 /// # Example:
21 /// ```
22 /// let character_set = &[
23 /// 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
24 /// 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
25 /// 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
26 /// 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
27 /// ];
28 /// let pad = &Some('/'); // Throws Error because '/' is already taken in the character set
29 /// ```
30 NotUniquePaddingError,
31 /// character set provided has duplicate characters
32 /// # Example:
33 /// ```
34 /// let character_set = &[
35 /// 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
36 /// 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
37 /// 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
38 /// 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '+',
39 /// ]; // Throws Error because '+' is defined twice in the character set
40 /// ```
41 DuplicateCharacterError,
42 /// Character in character set isn't representable
43 /// # Example:
44 /// ```
45 /// let character_set = &[
46 /// 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
47 /// 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
48 /// 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
49 /// 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '\0',
50 /// ]; // Throws Error because '\0' isn't representable
51 /// ```
52 CharacterSetUnrepresentableCharacter,
53 /// Padding character isn't representable
54 /// # Example:
55 /// ```
56 /// let pad = &Some('\n'); // Throws Error because '\n' isn't representable
57 /// ```
58 PaddingUnrepresentableCharacter,
59}
60
61impl Display for ConfigError {
62 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
63 match self {
64 ConfigError::CharacterSetLengthError => {
65 f.write_str("Provided Character set length isn't 64")
66 }
67 ConfigError::NotUniquePaddingError => {
68 f.write_str("Padding character provided is already used in character set")
69 }
70 ConfigError::DuplicateCharacterError => {
71 f.write_str("At least one duplicate character found in character set")
72 }
73 ConfigError::CharacterSetUnrepresentableCharacter => {
74 f.write_str("Character set has at leaset one unrepresentable character")
75 }
76 ConfigError::PaddingUnrepresentableCharacter => {
77 f.write_str("Padding is a character that is unrepresentable")
78 }
79 }
80 }
81}
82
83impl std::error::Error for ConfigError {
84 fn description(&self) -> &str {
85 match *self {
86 ConfigError::CharacterSetLengthError => "Provided Character set length isn't 64",
87 ConfigError::NotUniquePaddingError => {
88 "Padding character provided is already used in character set"
89 }
90 ConfigError::DuplicateCharacterError => {
91 "At least one duplicate character found in character set"
92 }
93 ConfigError::CharacterSetUnrepresentableCharacter => {
94 "Character set has at leaset one unrepresentable character"
95 }
96 ConfigError::PaddingUnrepresentableCharacter => {
97 "Padding is a character that is unrepresentable"
98 }
99 }
100 }
101}
102
103impl PartialEq for ConfigError {
104 fn eq(&self, other: &ConfigError) -> bool {
105 mem::discriminant(self) == mem::discriminant(other)
106 }
107}
108
109/// Possible errors when decoding Base64 number
110#[derive(Debug)]
111pub enum Base64Error {
112 /// Unsigned Overflow when decoding Base64 number to unsigned
113 ///
114 /// Only applies to
115 /// [Base64::decode_to_unsigned](../struct.Base64.html#method.decode_to_unsigned)
116 /// # Example:
117 /// ```
118 /// use lb64::{Base64, config::MIME};
119 ///
120 /// let b64 = Base64::new_random(999, MIME);
121 /// match b64.decode_to_unsigned() {
122 /// Ok(value) => println!("This is impossible"),
123 /// Err(e) => println!("{}", e), // Base64Error::OverflowError occurred
124 /// }
125 /// ```
126 OverflowError,
127 /// Invalid character in Base64 provided &str
128 ///
129 /// Only applies to [Base64::new_from_string](../struct.Base64.html#method.new_from_string)
130 /// # Example:
131 /// ```
132 /// use lb64::{Base64, config::MIME};
133 ///
134 /// match Base64::new_from_string(&"^_^", MIME) {
135 /// Ok(value) => println!("This is impossible"),
136 /// Err(e) => println!("{}", e), // Base64Error::InvalidBase64CharacterError occurred
137 /// }
138 /// ```
139 InvalidBase64CharacterError,
140}
141
142impl Display for Base64Error {
143 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
144 match *self {
145 Base64Error::OverflowError => {
146 f.write_str("Unsigned Overflow when decoding Base64 to unsigned")
147 }
148 Base64Error::InvalidBase64CharacterError => {
149 f.write_str("Invalid character in provided Base64 &str")
150 }
151 }
152 }
153}
154
155impl std::error::Error for Base64Error {
156 fn description(&self) -> &str {
157 match *self {
158 Base64Error::OverflowError => {
159 "Unsigned Overflow occured when decoding Base64 to unsigned"
160 }
161 Base64Error::InvalidBase64CharacterError => "Invalid character in provided Base64 &str",
162 }
163 }
164}
165
166impl PartialEq for Base64Error {
167 fn eq(&self, other: &Base64Error) -> bool {
168 mem::discriminant(self) == mem::discriminant(other)
169 }
170}