From f37f9c448bc9835f2422d5da118f539ba703fb6b Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 29 May 2021 17:23:47 +0100 Subject: [PATCH] conv_reader_writer --- src/formats/.#error.rs | 1 - src/formats/error.rs | 166 ----------------------------------------- 2 files changed, 167 deletions(-) delete mode 120000 src/formats/.#error.rs delete mode 100644 src/formats/error.rs diff --git a/src/formats/.#error.rs b/src/formats/.#error.rs deleted file mode 120000 index 60413f0..0000000 --- a/src/formats/.#error.rs +++ /dev/null @@ -1 +0,0 @@ -avril@eientei.14063:1621696860 \ No newline at end of file diff --git a/src/formats/error.rs b/src/formats/error.rs deleted file mode 100644 index 0204d57..0000000 --- a/src/formats/error.rs +++ /dev/null @@ -1,166 +0,0 @@ -//! Formatting error(s). -use super::*; -use std::{ - error, fmt, -}; - -/// A formatting error kind. See `Error`. -#[derive(Debug)] -pub enum ErrorKind -{ - IO(io::Error), - Json(serde_json::Error), - Cbor(serde_cbor::Error), - Lexpr(serde_lexpr::Error), - /// Unknown format ID - UnknownFormat(u32), - /// Unknown error - Unknown, -} - -impl error::Error for ErrorKind -{ - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - Some(match &self { - ErrorKind::IO(i) => i, - ErrorKind::Json(i) => i, - ErrorKind::Cbor(i) => i, - ErrorKind::Lexpr(i) => i, - _ => return None, - }) - } -} -impl fmt::Display for ErrorKind -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result - { - match self { - Self::IO(_) => write!(f, "io"), - - Self::Json(_) => write!(f, "conv json"), - Self::Cbor(_) => write!(f, "conv cbor"), - Self::Lexpr(_) => write!(f, "conv lexpr"), - - Self::UnknownFormat(fmt) => write!(f, "unknown format ID: {}", fmt), - - _ => write!(f, "unknown error") - } - } -} - - -impl From for ErrorKind -{ - fn from(from: io::Error) -> Self - { - Self::IO(from) - } -} - -impl From for ErrorKind -{ - fn from(from: serde_json::Error) -> Self - { - Self::Json(from) - } -} - -impl From for ErrorKind -{ - fn from(from: serde_cbor::Error) -> Self - { - Self::Cbor(from) - } -} - -impl From for ErrorKind -{ - fn from(from: serde_lexpr::Error) -> Self - { - Self::Lexpr(from) - } -} - - -/// A formatting error. -#[derive(Debug)] -pub struct Error(Box, bool); - -impl Error -{ - /// An encode error - #[inline] pub fn encode(kind: impl Into) -> Self - { - Self(Box::new(kind.into()), true) - } - /// A decode error - #[inline] pub fn decode(kind: impl Into) -> Self - { - Self(Box::new(kind.into()), false) - } - - /// What kind of error was it? - #[inline] pub fn kind(&self) -> &ErrorKind - { - &self.0 - } - - /// Consume into the kind of error, which contains the original error object from the conversion (if there is one) - #[inline] pub fn into_inner(self) -> ErrorKind - { - *self.0 - } - - /// Is an encode error? - #[inline] pub fn is_encode(&self) -> bool - { - self.1 - } - - /// Is a decode error? - #[inline] pub fn is_decode(&self) -> bool - { - !self.1 - } -} - -impl error::Error for Error -{ - #[inline] fn source(&self) -> Option<&(dyn error::Error + 'static)> { - use error::Error; - self.0.source() - } -} - -impl fmt::Display for Error -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result - { - write!(f, "formatting error in ")?; - if self.1 { - write!(f, "encode") - } else { - write!(f, "decode") - } - } -} - -impl From<(io::Error, bool)> for Error -{ - #[inline] fn from(from: (io::Error, bool)) -> Self - { - Self(Box::new(ErrorKind::IO(from.0)), from.1) - } -} - -impl From for io::Error -{ - fn from(from: Error) -> Self - { - match *from.0 - { - ErrorKind::IO(io) => io, - _ => io::Error::new(io::ErrorKind::Other, from), - } - } -}