1use std::fmt::{Debug, Display};
8
9#[derive(Debug)]
17pub enum Error {
18 IoError {
20 source: std::io::Error,
21 },
22 FromUtf8Error {
23 source: std::string::FromUtf8Error,
24 },
25}
26
27pub type Result<T> = std::result::Result<Error, T>;
31
32#[inline]
38pub fn io_error(source: std::io::Error) -> Error {
39 Error::IoError { source }
40}
41
42#[inline]
44pub fn from_utf8_error(source: std::string::FromUtf8Error) -> Error {
45 Error::FromUtf8Error { source }
46}
47
48impl Display for Error {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 write!(
55 f,
56 "{}",
57 match self {
58 Error::IoError { source } => format!("An I/O error occurred; source: {}", source),
59 Error::FromUtf8Error { source } => format!(
60 "An error occurred making a string from UTF-8 bytes; source: {}",
61 source
62 ),
63 }
64 )
65 }
66}
67
68impl std::error::Error for Error {
69 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
70 #[allow(unreachable_patterns)]
71 match self {
72 Error::IoError { source } => Some(source),
73 Error::FromUtf8Error { source } => Some(source),
74 _ => None,
75 }
76 }
77}
78
79impl From<std::io::Error> for Error {
80 fn from(source: std::io::Error) -> Self {
81 io_error(source)
82 }
83}
84
85impl From<std::string::FromUtf8Error> for Error {
86 fn from(source: std::string::FromUtf8Error) -> Self {
87 from_utf8_error(source)
88 }
89}