From 4de42f195165a56c9a8a68283d7d23e753972231 Mon Sep 17 00:00:00 2001 From: Ritesh Chitlangi Date: Tue, 20 Jul 2021 16:36:35 +0800 Subject: [PATCH] made deserialization in EppClient::transact() generic --- examples/client.rs | 2 +- src/connection.rs | 13 +++++++------ src/epp/quick_xml.rs | 8 ++++---- src/epp/xml.rs | 6 +++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/client.rs b/examples/client.rs index f778b35..d2d63b6 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -14,5 +14,5 @@ async fn main() { let epp_hello = request::Hello::new(); - client.transact(&epp_hello).await.unwrap(); + client.transact::(&epp_hello).await.unwrap(); } diff --git a/src/connection.rs b/src/connection.rs index e1d55a9..fdfc295 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -4,12 +4,13 @@ use std::{str, u32}; use bytes::BytesMut; use std::convert::TryInto; use futures::executor::block_on; -use std::{error::Error, net::ToSocketAddrs, io as stdio}; +use std::{error::Error, fmt::Debug, net::ToSocketAddrs, io as stdio}; use tokio_rustls::{TlsConnector, rustls::ClientConfig, webpki::DNSNameRef, client::TlsStream}; use tokio::{net::TcpStream, io::AsyncWriteExt, io::AsyncReadExt, io::split, io::ReadHalf, io::WriteHalf}; use crate::config::{CONFIG, EppClientConnection}; use crate::error; +use crate::epp::object::EppObject; use crate::epp::request::{EppRequest, Login, Logout}; use crate::epp::response::EppCommandResponse; use crate::epp::xml::EppXml; @@ -146,12 +147,12 @@ impl EppClient { let client_tr_id = EppRequest::generate_client_tr_id(&client.credentials.0)?; let login_request = Login::new(&client.credentials.0, &client.credentials.1, client_tr_id.as_str()); - client.transact(&login_request).await?; + client.transact::(&login_request).await?; Ok(client) } - pub async fn transact(&mut self, request: &EppRequest) -> Result> { + pub async fn transact(&mut self, request: &EppRequest) -> Result> { let epp_xml = request.serialize()?; println!("Request:\r\n{}", epp_xml); @@ -160,11 +161,11 @@ impl EppClient { println!("Response:\r\n{}", response); - let response_obj = EppCommandResponse::deserialize(&response).unwrap(); + let response_obj = E::deserialize(&response)?; println!("Response:\r\n{:?}", response_obj); - Ok(response) + Ok(response_obj) } pub async fn transact_xml(&mut self, xml: &str) -> Result> { @@ -179,7 +180,7 @@ impl EppClient { let client_tr_id = EppRequest::generate_client_tr_id(&self.credentials.0).unwrap(); let epp_logout = Logout::new(client_tr_id.as_str()); - self.transact(&epp_logout).await; + self.transact::(&epp_logout).await; } } diff --git a/src/epp/quick_xml.rs b/src/epp/quick_xml.rs index db266c6..2488746 100644 --- a/src/epp/quick_xml.rs +++ b/src/epp/quick_xml.rs @@ -1,13 +1,13 @@ use quick_xml::de::from_str; use quick_xml::se; use serde::{de::DeserializeOwned, Serialize}; -use std::error::Error; +use std::{error::Error, fmt::Debug}; use crate::epp::object::EppObject; use crate::epp::xml::{EppXml, EPP_XML_HEADER}; -impl EppXml for EppObject { - type Object = EppObject; +impl EppXml for EppObject { + type Output = EppObject; fn serialize(&self) -> Result> { let epp_xml = format!("{}\r\n{}", EPP_XML_HEADER, se::to_string(self)?); @@ -15,7 +15,7 @@ impl EppXml for EppObject { Ok(epp_xml) } - fn deserialize(epp_xml: &str) -> Result> { + fn deserialize(epp_xml: &str) -> Result> { match from_str(epp_xml) { Ok(v) => Ok(v), Err(e) => Err(format!("epp-client Deserialization Error: {}", e).into()), diff --git a/src/epp/xml.rs b/src/epp/xml.rs index 24bf2ba..6d748d2 100644 --- a/src/epp/xml.rs +++ b/src/epp/xml.rs @@ -1,4 +1,4 @@ -use std::error::Error; +use std::{error::Error, fmt::Debug}; // use crate::epp::object::EppObject; @@ -11,8 +11,8 @@ pub const EPP_VERSION: &str = "1.0"; pub const EPP_LANG: &str = "en"; pub trait EppXml { - type Object; + type Output: Debug; fn serialize(&self) -> Result>; - fn deserialize(epp_xml: &str) -> Result>; + fn deserialize(epp_xml: &str) -> Result>; }