1use std::fmt::{self, Debug, Formatter};
19
20#[derive(Debug)]
24pub enum Error {
25 InvalidToken,
29
30 IoError(std::io::Error),
33
34 OtherError(Box<dyn std::error::Error + Sync + Send>),
39}
40
41impl fmt::Display for Error {
42 #[cfg_attr(feature = "nightly_coverage", coverage(off))]
43 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
44 match self {
45 Self::InvalidToken => f.write_str("invalid token provided to internal function"),
46 Self::IoError(err) => write!(f, "underlying IO error: {}", err),
47 Self::OtherError(err) => write!(f, "other error during loop operation: {}", err),
48 }
49 }
50}
51
52impl From<std::io::Error> for Error {
53 #[cfg_attr(feature = "nightly_coverage", coverage(off))]
54 fn from(value: std::io::Error) -> Self {
55 Self::IoError(value)
56 }
57}
58
59impl From<Box<dyn std::error::Error + Sync + Send>> for Error {
60 #[cfg_attr(feature = "nightly_coverage", coverage(off))]
61 fn from(value: Box<dyn std::error::Error + Sync + Send>) -> Self {
62 Self::OtherError(value)
63 }
64}
65
66impl From<Error> for std::io::Error {
67 #[cfg_attr(feature = "nightly_coverage", coverage(off))]
69 fn from(err: Error) -> Self {
70 match err {
71 Error::IoError(source) => source,
72 Error::InvalidToken => Self::new(std::io::ErrorKind::InvalidInput, err.to_string()),
73 Error::OtherError(source) => Self::new(std::io::ErrorKind::Other, source),
74 }
75 }
76}
77
78impl std::error::Error for Error {
79 #[cfg_attr(feature = "nightly_coverage", coverage(off))]
80 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
81 match self {
82 Self::InvalidToken => None,
83 Self::IoError(err) => Some(err),
84 Self::OtherError(err) => Some(&**err),
85 }
86 }
87}
88
89pub type Result<T> = core::result::Result<T, Error>;
91
92pub struct InsertError<T> {
94 pub inserted: T,
96 pub error: Error,
98}
99
100impl<T> Debug for InsertError<T> {
101 #[cfg_attr(feature = "nightly_coverage", coverage(off))]
102 fn fmt(&self, formatter: &mut Formatter) -> core::result::Result<(), fmt::Error> {
103 write!(formatter, "{:?}", self.error)
104 }
105}
106
107impl<T> fmt::Display for InsertError<T> {
108 #[cfg_attr(feature = "nightly_coverage", coverage(off))]
109 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
110 write!(f, "error inserting event source: {}", &self.error)
111 }
112}
113
114impl<T> From<InsertError<T>> for crate::Error {
115 #[cfg_attr(feature = "nightly_coverage", coverage(off))]
118 fn from(e: InsertError<T>) -> crate::Error {
119 e.error
120 }
121}
122
123impl<T> std::error::Error for InsertError<T> {
124 #[cfg_attr(feature = "nightly_coverage", coverage(off))]
125 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
126 Some(&self.error)
127 }
128}