reed_solomon_erasure/
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}
19
20impl Error {
21 fn to_string(&self) -> &str {
22 match *self {
23 Error::TooFewShards=> "The number of provided shards is smaller than the one in codec",
24 Error::TooManyShards => "The number of provided shards is greater than the one in codec",
25 Error::TooFewDataShards => "The number of provided data shards is smaller than the one in codec",
26 Error::TooManyDataShards => "The number of provided data shards is greater than the one in codec",
27 Error::TooFewParityShards => "The number of provided parity shards is smaller than the one in codec",
28 Error::TooManyParityShards => "The number of provided parity shards is greater than the one in codec",
29 Error::TooFewBufferShards => "The number of provided buffer shards is smaller than the number of parity shards in codec",
30 Error::TooManyBufferShards => "The number of provided buffer shards is greater than the number of parity shards in codec",
31 Error::IncorrectShardSize => "At least one of the provided shards is not of the correct size",
32 Error::TooFewShardsPresent => "The number of shards present is smaller than number of parity shards, cannot reconstruct missing shards",
33 Error::EmptyShard => "The first shard provided is of zero length",
34 Error::InvalidShardFlags => "The number of flags does not match the total number of shards",
35 Error::InvalidIndex => "The data shard index provided is greater or equal to the number of data shards in codec",
36 }
37 }
38}
39
40impl core::fmt::Display for Error {
41 fn fmt(&self, f: &mut Formatter) -> Result<(), core::fmt::Error> {
42 write!(f, "{}", self.to_string())
43 }
44}
45
46#[cfg(feature = "std")]
47impl std::error::Error for Error {
48 fn description(&self) -> &str {
49 self.to_string()
50 }
51}
52
53#[derive(PartialEq, Debug, Clone, Copy)]
54pub enum SBSError {
55 TooManyCalls,
56 LeftoverShards,
57 RSError(Error),
58}
59
60impl SBSError {
61 fn to_string(&self) -> &str {
62 match *self {
63 SBSError::TooManyCalls => "Too many calls",
64 SBSError::LeftoverShards => "Leftover shards",
65 SBSError::RSError(ref e) => e.to_string(),
66 }
67 }
68}
69
70impl core::fmt::Display for SBSError {
71 fn fmt(&self, f: &mut Formatter) -> Result<(), core::fmt::Error> {
72 write!(f, "{}", self.to_string())
73 }
74}
75
76#[cfg(feature = "std")]
77impl std::error::Error for SBSError {
78 fn description(&self) -> &str {
79 self.to_string()
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use crate::errors::Error;
86 use crate::errors::SBSError;
87
88 #[test]
89 fn test_error_to_string_is_okay() {
90 assert_eq!(
91 Error::TooFewShards.to_string(),
92 "The number of provided shards is smaller than the one in codec"
93 );
94 assert_eq!(
95 Error::TooManyShards.to_string(),
96 "The number of provided shards is greater than the one in codec"
97 );
98 assert_eq!(
99 Error::TooFewDataShards.to_string(),
100 "The number of provided data shards is smaller than the one in codec"
101 );
102 assert_eq!(
103 Error::TooManyDataShards.to_string(),
104 "The number of provided data shards is greater than the one in codec"
105 );
106 assert_eq!(
107 Error::TooFewParityShards.to_string(),
108 "The number of provided parity shards is smaller than the one in codec"
109 );
110 assert_eq!(
111 Error::TooManyParityShards.to_string(),
112 "The number of provided parity shards is greater than the one in codec"
113 );
114 assert_eq!(
115 Error::TooFewBufferShards.to_string(),
116 "The number of provided buffer shards is smaller than the number of parity shards in codec"
117 );
118 assert_eq!(
119 Error::TooManyBufferShards.to_string(),
120 "The number of provided buffer shards is greater than the number of parity shards in codec"
121 );
122 assert_eq!(
123 Error::IncorrectShardSize.to_string(),
124 "At least one of the provided shards is not of the correct size"
125 );
126 assert_eq!(Error::TooFewShardsPresent.to_string(), "The number of shards present is smaller than number of parity shards, cannot reconstruct missing shards");
127 assert_eq!(
128 Error::EmptyShard.to_string(),
129 "The first shard provided is of zero length"
130 );
131 assert_eq!(
132 Error::InvalidShardFlags.to_string(),
133 "The number of flags does not match the total number of shards"
134 );
135 assert_eq!(
136 Error::InvalidIndex.to_string(),
137 "The data shard index provided is greater or equal to the number of data shards in codec"
138 );
139 }
140
141 #[test]
142 fn test_sbserror_to_string_is_okay() {
143 assert_eq!(SBSError::TooManyCalls.to_string(), "Too many calls");
144 assert_eq!(SBSError::LeftoverShards.to_string(), "Leftover shards");
145 }
146
147 #[cfg(feature = "std")]
148 #[test]
149 fn test_error_display_does_not_panic() {
150 println!("{}", Error::TooFewShards);
151 }
152
153 #[cfg(feature = "std")]
154 #[test]
155 fn test_sbserror_display_does_not_panic() {
156 println!("{}", SBSError::TooManyCalls);
157 }
158}