From 1011818be6c50642b9f1c3c91a7c2fac6c27d0a1 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 25 May 2020 15:34:17 +0200 Subject: [PATCH] Split out general std::io::Error wrapper --- implementations/rust/src/error.rs | 30 +++++++++++++++++++++++++++ implementations/rust/src/lib.rs | 1 + implementations/rust/src/ser.rs | 34 ++----------------------------- 3 files changed, 33 insertions(+), 32 deletions(-) create mode 100644 implementations/rust/src/error.rs diff --git a/implementations/rust/src/error.rs b/implementations/rust/src/error.rs new file mode 100644 index 0000000..1509063 --- /dev/null +++ b/implementations/rust/src/error.rs @@ -0,0 +1,30 @@ +#[derive(Debug)] +pub struct Error { + inner: std::io::Error, +} + +impl std::convert::From for std::io::Error { + fn from(e: Error) -> Self { + e.inner + } +} + +impl std::convert::From for Error { + fn from(inner: std::io::Error) -> Self { + Error { inner } + } +} + +impl serde::ser::Error for Error { + fn custom(msg: T) -> Self { + Error { inner: std::io::Error::new(std::io::ErrorKind::Other, msg.to_string()) } + } +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> { + self.inner.fmt(f) + } +} + +impl std::error::Error for Error {} diff --git a/implementations/rust/src/lib.rs b/implementations/rust/src/lib.rs index 51a3bd9..f9afe0c 100644 --- a/implementations/rust/src/lib.rs +++ b/implementations/rust/src/lib.rs @@ -1,5 +1,6 @@ pub mod de; pub mod ser; +pub mod error; pub mod symbol; pub mod value; diff --git a/implementations/rust/src/ser.rs b/implementations/rust/src/ser.rs index aba3ea9..f4b4889 100644 --- a/implementations/rust/src/ser.rs +++ b/implementations/rust/src/ser.rs @@ -1,5 +1,5 @@ use serde::Serialize; -use super::value::writer::{self, Writer}; +use super::value::writer::Writer; use super::value::{ Value, NestedValue, PlainValue, Domain, NullDomain, @@ -7,39 +7,9 @@ use super::value::{ Encoder, }; +pub use super::error::Error; type Result = std::result::Result; -#[derive(Debug)] -pub struct Error { - inner: writer::Error, -} - -impl std::convert::From for writer::Error { - fn from(e: Error) -> Self { - e.inner - } -} - -impl std::convert::From for Error { - fn from(inner: writer::Error) -> Self { - Error { inner } - } -} - -impl serde::ser::Error for Error { - fn custom(msg: T) -> Self { - Error { inner: writer::Error::new(std::io::ErrorKind::Other, msg.to_string()) } - } -} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> { - self.inner.fmt(f) - } -} - -impl std::error::Error for Error {} - #[derive(Debug)] pub struct Serializer<'a, W: Writer, N: NestedValue, D: Domain> { pub write: &'a mut W,