|
@@ -0,0 +1,45 @@
|
|
|
+use std::fmt::{self, Display};
|
|
|
+use serde::{ser, de};
|
|
|
+
|
|
|
+pub type Result<T> = std::result::Result<T, Error>;
|
|
|
+
|
|
|
+#[derive(Debug)]
|
|
|
+pub enum Error {
|
|
|
+ Message(String),
|
|
|
+ Io(std::io::Error),
|
|
|
+ Eof,
|
|
|
+}
|
|
|
+
|
|
|
+impl std::error::Error for Error {}
|
|
|
+
|
|
|
+impl Display for Error {
|
|
|
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
+ match self {
|
|
|
+ Error::Message(message) => formatter.write_str(message),
|
|
|
+ Error::Eof => formatter.write_str("unexpected end of input"),
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl ser::Error for Error {
|
|
|
+ fn custom<T: Display>(message: T) -> Self {
|
|
|
+ Error::Message(message.to_string())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl de::Error for Error {
|
|
|
+ fn custom<T: Display>(message: T) -> Self {
|
|
|
+ Error::Message(message.to_string())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+pub trait MapError<T> {
|
|
|
+ /// Returns self if no error occurred, otherwise converts the error to a serde_blocktree error.
|
|
|
+ fn map_error(self) -> Result<T>;
|
|
|
+}
|
|
|
+
|
|
|
+impl<T> MapError<T> for std::io::Result<T> {
|
|
|
+ fn map_error(self) -> Result<T> {
|
|
|
+ self.or_else(|err| Result::Err(Error::Io(err)))
|
|
|
+ }
|
|
|
+}
|