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
use std::{error::Error, fmt};

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SnowBinErrorTypes {
    DataSizeNotAllowed,
    VerifyHashingNotEnabled,
    CouldNotCreateOrOpenFile,
    IOWriteError,
    HeaderTooLong,
    IOWriterClosed,
    DataTooLong,
    IOReadError,
    MalformedHeader,
    MalformedUInt,
    WrongSpecVersion,
    ReachedEOF,
    HashDoesNotMatch,
}

#[derive(Debug)]
pub struct SnowBinError {
    desc: String,
    error_type: SnowBinErrorTypes,
}

impl SnowBinError {
    pub fn new(error_type: SnowBinErrorTypes) -> Self {
        let desc = match error_type {
            SnowBinErrorTypes::DataSizeNotAllowed => {
                String::from("Data Size not 8, 16, 32, 64, or 128.")
            }
            SnowBinErrorTypes::VerifyHashingNotEnabled => {
                String::from("Verify hashing not enabled, please enable the feature.")
            }
            SnowBinErrorTypes::CouldNotCreateOrOpenFile => {
                String::from("Could not create or open the file.")
            }
            SnowBinErrorTypes::IOWriteError => String::from("Could not write to the file."),
            SnowBinErrorTypes::HeaderTooLong => String::from("Header exceeds max length."),
            SnowBinErrorTypes::IOWriterClosed => {
                String::from("Could not write to the file, because the close function was called.")
            }
            SnowBinErrorTypes::DataTooLong => String::from("Data exceeds max length."),
            SnowBinErrorTypes::IOReadError => String::from("Could not read from the file."),
            SnowBinErrorTypes::MalformedHeader => {
                String::from("File did not start with \"SNOW_BIN\".")
            }
            SnowBinErrorTypes::MalformedUInt => {
                String::from("Could not pull a uint from the file when expected.")
            }
            SnowBinErrorTypes::WrongSpecVersion => String::from("Spec version does not match."),
            SnowBinErrorTypes::ReachedEOF => {
                String::from("Reached the end of the file, without finding the header specified.")
            }
            SnowBinErrorTypes::HashDoesNotMatch => {
                String::from("Verification hash did not match data hash.")
            }
        };

        Self { desc, error_type }
    }

    pub fn error_type(&self) -> SnowBinErrorTypes {
        self.error_type
    }
}

impl fmt::Display for SnowBinError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.desc)
    }
}

impl Error for SnowBinError {
    fn description(&self) -> &str {
        &self.desc
    }
}