rustfs_erasure_codec/
errors.rs1use core::fmt::Formatter;
2
3#[derive(PartialEq, Debug, Clone, Copy)]
4pub enum Error {
5 TooFewShards,
6 TooManyShards,
7 TooFewDataShards,
8 TooManyDataShards,
9 TooFewParityShards,
10 TooManyParityShards,
11 TooFewBufferShards,
12 TooManyBufferShards,
13 IncorrectShardSize,
14 TooFewShardsPresent,
15 EmptyShard,
16 InvalidShardFlags,
17 InvalidIndex,
18 InvalidCustomMatrix,
19 UnsupportedCodecFamily,
20 UnsupportedLeopardPrototype,
21}
22
23impl Error {
24 fn as_str(self) -> &'static str {
25 match self {
26 Error::TooFewShards => "The number of provided shards is smaller than the one in codec",
27 Error::TooManyShards => {
28 "The number of provided shards is greater than the one in codec"
29 }
30 Error::TooFewDataShards => {
31 "The number of provided data shards is smaller than the one in codec"
32 }
33 Error::TooManyDataShards => {
34 "The number of provided data shards is greater than the one in codec"
35 }
36 Error::TooFewParityShards => {
37 "The number of provided parity shards is smaller than the one in codec"
38 }
39 Error::TooManyParityShards => {
40 "The number of provided parity shards is greater than the one in codec"
41 }
42 Error::TooFewBufferShards => {
43 "The number of provided buffer shards is smaller than the number of parity shards in codec"
44 }
45 Error::TooManyBufferShards => {
46 "The number of provided buffer shards is greater than the number of parity shards in codec"
47 }
48 Error::IncorrectShardSize => {
49 "At least one of the provided shards is not of the correct size"
50 }
51 Error::TooFewShardsPresent => {
52 "The number of shards present is smaller than number of parity shards, cannot reconstruct missing shards"
53 }
54 Error::EmptyShard => "The first shard provided is of zero length",
55 Error::InvalidShardFlags => {
56 "The number of flags does not match the total number of shards"
57 }
58 Error::InvalidIndex => {
59 "The data shard index provided is greater or equal to the number of data shards in codec"
60 }
61 Error::InvalidCustomMatrix => {
62 "The supplied custom matrix is invalid or missing for MatrixMode::Custom"
63 }
64 Error::UnsupportedCodecFamily => {
65 "The selected codec family is not supported for this field or configuration"
66 }
67 Error::UnsupportedLeopardPrototype => {
68 "The selected Leopard codec family is only available as a prototype skeleton in this build"
69 }
70 }
71 }
72}
73
74impl core::fmt::Display for Error {
75 fn fmt(&self, f: &mut Formatter) -> Result<(), core::fmt::Error> {
76 write!(f, "{}", self.as_str())
77 }
78}
79
80#[cfg(feature = "std")]
81impl std::error::Error for Error {
82 fn description(&self) -> &str {
83 self.as_str()
84 }
85}
86
87#[derive(PartialEq, Debug, Clone, Copy)]
88pub enum SBSError {
89 TooManyCalls,
90 LeftoverShards,
91 RSError(Error),
92}
93
94impl SBSError {
95 fn as_str(self) -> &'static str {
96 match self {
97 SBSError::TooManyCalls => "Too many calls",
98 SBSError::LeftoverShards => "Leftover shards",
99 SBSError::RSError(ref e) => e.as_str(),
100 }
101 }
102}
103
104impl core::fmt::Display for SBSError {
105 fn fmt(&self, f: &mut Formatter) -> Result<(), core::fmt::Error> {
106 write!(f, "{}", self.as_str())
107 }
108}
109
110#[cfg(feature = "std")]
111impl std::error::Error for SBSError {
112 fn description(&self) -> &str {
113 self.as_str()
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 extern crate alloc;
120
121 use crate::errors::Error;
122 use crate::errors::SBSError;
123
124 #[test]
125 fn test_error_to_string_is_okay() {
126 assert_eq!(
127 alloc::format!("{}", Error::TooFewShards),
128 "The number of provided shards is smaller than the one in codec"
129 );
130 assert_eq!(
131 alloc::format!("{}", Error::TooManyShards),
132 "The number of provided shards is greater than the one in codec"
133 );
134 assert_eq!(
135 alloc::format!("{}", Error::TooFewDataShards),
136 "The number of provided data shards is smaller than the one in codec"
137 );
138 assert_eq!(
139 alloc::format!("{}", Error::TooManyDataShards),
140 "The number of provided data shards is greater than the one in codec"
141 );
142 assert_eq!(
143 alloc::format!("{}", Error::TooFewParityShards),
144 "The number of provided parity shards is smaller than the one in codec"
145 );
146 assert_eq!(
147 alloc::format!("{}", Error::TooManyParityShards),
148 "The number of provided parity shards is greater than the one in codec"
149 );
150 assert_eq!(
151 alloc::format!("{}", Error::TooFewBufferShards),
152 "The number of provided buffer shards is smaller than the number of parity shards in codec"
153 );
154 assert_eq!(
155 alloc::format!("{}", Error::TooManyBufferShards),
156 "The number of provided buffer shards is greater than the number of parity shards in codec"
157 );
158 assert_eq!(
159 alloc::format!("{}", Error::IncorrectShardSize),
160 "At least one of the provided shards is not of the correct size"
161 );
162 assert_eq!(
163 alloc::format!("{}", Error::TooFewShardsPresent),
164 "The number of shards present is smaller than number of parity shards, cannot reconstruct missing shards"
165 );
166 assert_eq!(
167 alloc::format!("{}", Error::EmptyShard),
168 "The first shard provided is of zero length"
169 );
170 assert_eq!(
171 alloc::format!("{}", Error::InvalidShardFlags),
172 "The number of flags does not match the total number of shards"
173 );
174 assert_eq!(
175 alloc::format!("{}", Error::InvalidIndex),
176 "The data shard index provided is greater or equal to the number of data shards in codec"
177 );
178 assert_eq!(
179 alloc::format!("{}", Error::InvalidCustomMatrix),
180 "The supplied custom matrix is invalid or missing for MatrixMode::Custom"
181 );
182 assert_eq!(
183 alloc::format!("{}", Error::UnsupportedCodecFamily),
184 "The selected codec family is not supported for this field or configuration"
185 );
186 assert_eq!(
187 alloc::format!("{}", Error::UnsupportedLeopardPrototype),
188 "The selected Leopard codec family is only available as a prototype skeleton in this build"
189 );
190 }
191
192 #[test]
193 fn test_sbserror_to_string_is_okay() {
194 assert_eq!(
195 alloc::format!("{}", SBSError::TooManyCalls),
196 "Too many calls"
197 );
198 assert_eq!(
199 alloc::format!("{}", SBSError::LeftoverShards),
200 "Leftover shards"
201 );
202 }
203
204 #[cfg(feature = "std")]
205 #[test]
206 fn test_error_display_does_not_panic() {
207 println!("{}", Error::TooFewShards);
208 }
209
210 #[cfg(feature = "std")]
211 #[test]
212 fn test_sbserror_display_does_not_panic() {
213 println!("{}", SBSError::TooManyCalls);
214 }
215}