erase/
errors.rs

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