Skip to main content

yenc/
errors.rs

1use std::convert::From;
2use std::fmt;
3use std::io;
4use std::iter;
5
6/// Error enum for errors that can be encountered while decoding.
7#[derive(Debug)]
8pub enum DecodeError {
9    /// Fewer or more bytes than expected.
10    IncompleteData {
11        /// the expected size, as specified in the yenc header
12        expected_size: usize,
13        /// the actual size, as found while reading
14        actual_size: usize,
15    },
16    /// The header or footer line contains unexpected characters or is incomplete.
17    InvalidHeader {
18        /// the header line
19        line: String,
20        /// the position in the line where the parsing error occurred
21        position: usize,
22    },
23    /// CRC32 checksum of the part is not the expected checksum.
24    InvalidChecksum,
25    /// An I/O error occurred.
26    IoError(io::Error),
27}
28
29/// Error enum for errors that can be encountered when validating the encode options or while encoding.
30#[derive(Debug)]
31pub enum EncodeError {
32    /// Multiple parts (parts > 1), but no part number specified
33    PartNumberMissing,
34    /// Multiple parts (parts > 1), but no begin offset specified
35    PartBeginOffsetMissing,
36    /// Multiple parts (parts > 1), but no end offset specified
37    PartEndOffsetMissing,
38    /// Multiple parts (parts > 1), and begin offset larger than end offset
39    PartOffsetsInvalidRange,
40    /// I/O Error
41    IoError(io::Error),
42}
43
44impl From<io::Error> for DecodeError {
45    fn from(error: io::Error) -> DecodeError {
46        DecodeError::IoError(error)
47    }
48}
49
50impl From<io::Error> for EncodeError {
51    fn from(error: io::Error) -> EncodeError {
52        EncodeError::IoError(error)
53    }
54}
55
56impl fmt::Display for DecodeError {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        match *self {
59            DecodeError::IncompleteData {
60                ref expected_size,
61                ref actual_size,
62            } => write!(
63                f,
64                "Incomplete data: expected size {}, actual size {}",
65                expected_size, actual_size
66            ),
67            DecodeError::InvalidHeader { ref line, position } => write!(
68                f,
69                "Invalid header: \n{}\n{}^",
70                line,
71                iter::repeat(" ").take(position).collect::<String>()
72            ),
73            DecodeError::InvalidChecksum => write!(f, "Invalid checksum"),
74            DecodeError::IoError(ref err) => write!(f, "I/O error {}", err),
75        }
76    }
77}
78
79impl fmt::Display for EncodeError {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match *self {
82            EncodeError::PartNumberMissing => {
83                write!(f, "Multiple parts, but no part number specified.")
84            }
85            EncodeError::PartBeginOffsetMissing => {
86                write!(f, "Multiple parts, but no begin offset specified.")
87            }
88            EncodeError::PartEndOffsetMissing => {
89                write!(f, "Multiple parts, but no end offset specified.")
90            }
91            EncodeError::PartOffsetsInvalidRange => {
92                write!(f, "Multiple parts, begin offset larger than end offset")
93            }
94            EncodeError::IoError(ref err) => write!(f, "I/O error {}", err),
95        }
96    }
97}
98// impl error::Error for DecodeError {}