From 467ac03df784cfc6f027321c27cc7fabc7d867e4 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 7 Dec 2021 11:32:44 +0100 Subject: [PATCH] Preserve quick-xml errors --- src/error.rs | 6 ++++-- src/request.rs | 2 +- src/xml.rs | 29 +++++++++-------------------- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/error.rs b/src/error.rs index 8037d17..2014476 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,14 +1,16 @@ //! Error types to wrap internal errors and make EPP errors easier to read -use crate::response::ResponseStatus; +use std::error::Error as StdError; use std::fmt::Display; +use crate::response::ResponseStatus; + /// Error enum holding the possible error types #[derive(Debug)] pub enum Error { Io(std::io::Error), Command(ResponseStatus), - Deserialize(String), + Xml(Box), Other(String), } diff --git a/src/request.rs b/src/request.rs index 8d49cad..00128ae 100644 --- a/src/request.rs +++ b/src/request.rs @@ -18,7 +18,7 @@ pub trait Transaction: Command + Sized { &self, extension: Option<&Ext>, client_tr_id: &str, - ) -> Result> { + ) -> Result { as EppXml>::serialize(&CommandDocument::new(CommandWrapper { command: Self::COMMAND, data: self, diff --git a/src/xml.rs b/src/xml.rs index af0743a..ebdc500 100644 --- a/src/xml.rs +++ b/src/xml.rs @@ -1,41 +1,30 @@ //! Types to use in serialization to and deserialization from EPP XML -use quick_xml::de::from_str; -use quick_xml::se; use serde::{de::DeserializeOwned, Serialize}; -use std::error::Error; -use crate::error; +use crate::error::Error; pub const EPP_XML_HEADER: &str = r#""#; /// Trait to be implemented by serializers. Currently the only included serializer is `quick-xml` pub trait EppXml: Sized { /// Serializes the EppObject instance to an EPP XML document - fn serialize(&self) -> Result> + fn serialize(&self) -> Result where Self: Serialize, { - let epp_xml = format!("{}\r\n{}", EPP_XML_HEADER, se::to_string(self)?); - - Ok(epp_xml) + Ok(format!( + "{}\r\n{}", + EPP_XML_HEADER, + quick_xml::se::to_string(self).map_err(|e| Error::Xml(e.into()))? + )) } /// Deserializes an EPP XML document to an EppObject instance - fn deserialize(epp_xml: &str) -> Result + fn deserialize(epp_xml: &str) -> Result where Self: DeserializeOwned + Sized, { - let object: Self = match from_str(epp_xml) { - Ok(v) => v, - Err(e) => { - return Err(error::Error::Deserialize(format!( - "epp-client Deserialization Error: {}", - e - ))) - } - }; - // object.xml = Some(epp_xml.to_string()); - Ok(object) + quick_xml::de::from_str::(epp_xml).map_err(|e| Error::Xml(e.into())) } }