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
use std::convert::From;
use std::fmt;
use std::io;
use std::iter;

/// Error enum for errors that can be encountered while decoding.
#[derive(Debug)]
pub enum DecodeError {
    /// Fewer or more bytes than expected.
    IncompleteData {
        /// the expected size, as specified in the yenc header
        expected_size: usize,
        /// the actual size, as found while reading
        actual_size: usize,
    },
    /// The header or footer line contains unexpected characters or is incomplete.
    InvalidHeader {
        /// the header line
        line: String,
        /// the position in the line where the parsing error occurred
        position: usize,
    },
    /// CRC32 checksum of the part is not the expected checksum.
    InvalidChecksum,
    /// An I/O error occurred.
    IoError(io::Error),
}

/// Error enum for errors that can be encountered when validating the encode options or while encoding.
#[derive(Debug)]
pub enum EncodeError {
    /// Multiple parts (parts > 1), but no part number specified
    PartNumberMissing,
    /// Multiple parts (parts > 1), but no begin offset specified
    PartBeginOffsetMissing,
    /// Multiple parts (parts > 1), but no end offset specified
    PartEndOffsetMissing,
    /// Multiple parts (parts > 1), and begin offset larger than end offset
    PartOffsetsInvalidRange,
    /// I/O Error
    IoError(io::Error),
}

impl From<io::Error> for DecodeError {
    fn from(error: io::Error) -> DecodeError {
        DecodeError::IoError(error)
    }
}

impl From<io::Error> for EncodeError {
    fn from(error: io::Error) -> EncodeError {
        EncodeError::IoError(error)
    }
}

impl fmt::Display for DecodeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            DecodeError::IncompleteData {
                ref expected_size,
                ref actual_size,
            } => write!(
                f,
                "Incomplete data: expected size {}, actual size {}",
                expected_size, actual_size
            ),
            DecodeError::InvalidHeader { ref line, position } => write!(
                f,
                "Invalid header: \n{}\n{}^",
                line,
                iter::repeat(" ").take(position).collect::<String>()
            ),
            DecodeError::InvalidChecksum => write!(f, "Invalid checksum"),
            DecodeError::IoError(ref err) => write!(f, "I/O error {}", err),
        }
    }
}

impl fmt::Display for EncodeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            EncodeError::PartNumberMissing => {
                write!(f, "Multiple parts, but no part number specified.")
            }
            EncodeError::PartBeginOffsetMissing => {
                write!(f, "Multiple parts, but no begin offset specified.")
            }
            EncodeError::PartEndOffsetMissing => {
                write!(f, "Multiple parts, but no end offset specified.")
            }
            EncodeError::PartOffsetsInvalidRange => {
                write!(f, "Multiple parts, begin offset larger than end offset")
            }
            EncodeError::IoError(ref err) => write!(f, "I/O error {}", err),
        }
    }
}
// impl error::Error for DecodeError {}