From 3f916473706f4d19cd3142b1f02e5987ef5b521a Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 12 Mar 2022 18:12:16 +0100 Subject: [PATCH] Move response deserialization into client --- src/client.rs | 20 +++++++++++++------- src/request.rs | 18 +----------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/client.rs b/src/client.rs index 6b0312f..5268579 100644 --- a/src/client.rs +++ b/src/client.rs @@ -20,7 +20,7 @@ use crate::connection::{self, EppConnection}; use crate::error::Error; use crate::hello::{Greeting, GreetingDocument, HelloDocument}; use crate::request::{Command, CommandDocument, Extension, Transaction}; -use crate::response::Response; +use crate::response::{Response, ResponseDocument, ResponseStatus}; use crate::xml::EppXml; /// An `EppClient` provides an interface to sending EPP requests to a registry @@ -133,13 +133,19 @@ impl EppClient { let response = self.connection.transact(&xml)?.await?; debug!("{}: response: {}", self.connection.registry, &response); - match Cmd::deserialize_response(&response) { - Ok(response) => Ok(response), - Err(e) => { - error!(%response, "Failed to deserialize response: {}", e); - Err(e) - } + let rsp = + as EppXml>::deserialize(&response)?; + if rsp.data.result.code.is_success() { + return Ok(rsp.data); } + + let err = crate::error::Error::Command(ResponseStatus { + result: rsp.data.result, + tr_ids: rsp.data.tr_ids, + }); + + error!(%response, "Failed to deserialize response for transaction: {}", err); + Err(err) } /// Accepts raw EPP XML and returns the raw EPP XML response to it. diff --git a/src/request.rs b/src/request.rs index 301a688..464bcea 100644 --- a/src/request.rs +++ b/src/request.rs @@ -5,30 +5,14 @@ use std::fmt::Debug; use crate::{ common::{StringValue, EPP_XMLNS}, - response::{Response, ResponseDocument, ResponseStatus}, xml::EppXml, - Error, }; pub const EPP_VERSION: &str = "1.0"; pub const EPP_LANG: &str = "en"; /// Trait to set correct value for xml tags when tags are being generated from generic types -pub trait Transaction: Command + Sized { - fn deserialize_response( - epp_xml: &str, - ) -> Result, Error> { - let rsp = - as EppXml>::deserialize(epp_xml)?; - match rsp.data.result.code.is_success() { - true => Ok(rsp.data), - false => Err(crate::error::Error::Command(ResponseStatus { - result: rsp.data.result, - tr_ids: rsp.data.tr_ids, - })), - } - } -} +pub trait Transaction: Command + Sized {} pub trait Command: Serialize + Debug { type Response: DeserializeOwned + Debug;