1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use core::fmt::Formatter;

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum Error {
    TooFewShards,
    TooManyShards,
    TooFewDataShards,
    TooManyDataShards,
    TooFewParityShards,
    TooManyParityShards,
    TooFewBufferShards,
    TooManyBufferShards,
    IncorrectShardSize,
    TooFewShardsPresent,
    EmptyShard,
    InvalidShardFlags,
    InvalidIndex,
}

impl Error {
    fn to_string(&self) -> &str {
        match *self {
            Error::TooFewShards=> "The number of provided shards is smaller than the one in codec",
            Error::TooManyShards => "The number of provided shards is greater than the one in codec",
            Error::TooFewDataShards => "The number of provided data shards is smaller than the one in codec",
            Error::TooManyDataShards => "The number of provided data shards is greater than the one in codec",
            Error::TooFewParityShards => "The number of provided parity shards is smaller than the one in codec",
            Error::TooManyParityShards => "The number of provided parity shards is greater than the one in codec",
            Error::TooFewBufferShards => "The number of provided buffer shards is smaller than the number of parity shards in codec",
            Error::TooManyBufferShards => "The number of provided buffer shards is greater than the number of parity shards in codec",
            Error::IncorrectShardSize => "At least one of the provided shards is not of the correct size",
            Error::TooFewShardsPresent => "The number of shards present is smaller than number of parity shards, cannot reconstruct missing shards",
            Error::EmptyShard => "The first shard provided is of zero length",
            Error::InvalidShardFlags => "The number of flags does not match the total number of shards",
            Error::InvalidIndex => "The data shard index provided is greater or equal to the number of data shards in codec",
        }
    }
}

impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut Formatter) -> Result<(), core::fmt::Error> {
        write!(f, "{}", self.to_string())
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
    fn description(&self) -> &str {
        self.to_string()
    }
}

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum SBSError {
    TooManyCalls,
    LeftoverShards,
    RSError(Error),
}

impl SBSError {
    fn to_string(&self) -> &str {
        match *self {
            SBSError::TooManyCalls => "Too many calls",
            SBSError::LeftoverShards => "Leftover shards",
            SBSError::RSError(ref e) => e.to_string(),
        }
    }
}

impl core::fmt::Display for SBSError {
    fn fmt(&self, f: &mut Formatter) -> Result<(), core::fmt::Error> {
        write!(f, "{}", self.to_string())
    }
}

#[cfg(feature = "std")]
impl std::error::Error for SBSError {
    fn description(&self) -> &str {
        self.to_string()
    }
}

#[cfg(test)]
mod tests {
    use crate::errors::Error;
    use crate::errors::SBSError;

    #[test]
    fn test_error_to_string_is_okay() {
        assert_eq!(
            Error::TooFewShards.to_string(),
            "The number of provided shards is smaller than the one in codec"
        );
        assert_eq!(
            Error::TooManyShards.to_string(),
            "The number of provided shards is greater than the one in codec"
        );
        assert_eq!(
            Error::TooFewDataShards.to_string(),
            "The number of provided data shards is smaller than the one in codec"
        );
        assert_eq!(
            Error::TooManyDataShards.to_string(),
            "The number of provided data shards is greater than the one in codec"
        );
        assert_eq!(
            Error::TooFewParityShards.to_string(),
            "The number of provided parity shards is smaller than the one in codec"
        );
        assert_eq!(
            Error::TooManyParityShards.to_string(),
            "The number of provided parity shards is greater than the one in codec"
        );
        assert_eq!(
            Error::TooFewBufferShards.to_string(),
            "The number of provided buffer shards is smaller than the number of parity shards in codec"
        );
        assert_eq!(
            Error::TooManyBufferShards.to_string(),
            "The number of provided buffer shards is greater than the number of parity shards in codec"
        );
        assert_eq!(
            Error::IncorrectShardSize.to_string(),
            "At least one of the provided shards is not of the correct size"
        );
        assert_eq!(Error::TooFewShardsPresent.to_string(), "The number of shards present is smaller than number of parity shards, cannot reconstruct missing shards");
        assert_eq!(
            Error::EmptyShard.to_string(),
            "The first shard provided is of zero length"
        );
        assert_eq!(
            Error::InvalidShardFlags.to_string(),
            "The number of flags does not match the total number of shards"
        );
        assert_eq!(
            Error::InvalidIndex.to_string(),
            "The data shard index provided is greater or equal to the number of data shards in codec"
        );
    }

    #[test]
    fn test_sbserror_to_string_is_okay() {
        assert_eq!(SBSError::TooManyCalls.to_string(), "Too many calls");
        assert_eq!(SBSError::LeftoverShards.to_string(), "Leftover shards");
    }

    #[cfg(feature = "std")]
    #[test]
    fn test_error_display_does_not_panic() {
        println!("{}", Error::TooFewShards);
    }

    #[cfg(feature = "std")]
    #[test]
    fn test_sbserror_display_does_not_panic() {
        println!("{}", SBSError::TooManyCalls);
    }
}