steganography_lib/
errors.rs1use std::{error::Error, fmt::Display};
2
3pub type Result<T> = std::result::Result<T, ErrorKind>;
4
5#[derive(Debug)]
6pub enum ErrorKind {
7 SysIOError(std::io::Error),
8 PngDecodingError(png::DecodingError),
9 PngEncodingError(png::EncodingError),
10
11 DataTooLarge(usize, usize),
12 HashMismatch(u64, u64),
13
14 TransformationError(Box<dyn Error>),
15}
16
17impl Display for ErrorKind {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 match self {
20 Self::SysIOError(_) => write!(f, "failed to perform I/O by system"),
21 Self::PngDecodingError(_) => write!(f, "failed to decode PNG image"),
22 Self::PngEncodingError(_) => write!(f, "failed to encode PNG image"),
23 Self::DataTooLarge(actual, available) => write!(
24 f,
25 "cannot fit {} bits in {} bytes available",
26 actual, available
27 ),
28 Self::HashMismatch(expected, actual) => {
29 write!(f, "expecting hash '{}', found '{}'", expected, actual)
30 }
31 Self::TransformationError(e) => write!(f, "{}", e),
32 }
33 }
34}
35
36impl Error for ErrorKind {
37 fn source(&self) -> Option<&(dyn Error + 'static)> {
38 match self {
39 Self::SysIOError(e) => Some(e),
40 Self::PngDecodingError(e) => Some(e),
41 Self::PngEncodingError(e) => Some(e),
42 Self::DataTooLarge(_, _) | Self::HashMismatch(_, _) => None,
43 Self::TransformationError(e) => Some(e.as_ref()),
44 }
45 }
46}