From fce6c7a2ebf9b9f59fd3b2413998e27cb9c1a6ff Mon Sep 17 00:00:00 2001 From: Nicholas Rempel Date: Fri, 26 Nov 2021 11:20:25 -0800 Subject: [PATCH] Refactor object::data into common module --- .../src/{epp/object/data.rs => common.rs} | 139 ++++++++++++++++- epp-client/src/contact/check.rs | 2 +- epp-client/src/contact/create.rs | 21 ++- epp-client/src/contact/delete.rs | 2 +- epp-client/src/contact/info.rs | 5 +- epp-client/src/contact/update.rs | 7 +- epp-client/src/domain/check.rs | 2 +- epp-client/src/domain/create.rs | 8 +- epp-client/src/domain/delete.rs | 2 +- epp-client/src/domain/info.rs | 5 +- epp-client/src/domain/renew.rs | 3 +- epp-client/src/domain/rgp/report.rs | 2 +- epp-client/src/domain/rgp/request.rs | 2 +- epp-client/src/domain/transfer.rs | 3 +- epp-client/src/domain/update.rs | 8 +- epp-client/src/epp.rs | 1 - epp-client/src/epp/object.rs | 140 ------------------ epp-client/src/epp/request.rs | 9 +- epp-client/src/epp/response.rs | 2 +- epp-client/src/epp/xml.rs | 1 - epp-client/src/epp/xml/quick_xml.rs | 2 +- epp-client/src/host/check.rs | 2 +- epp-client/src/host/create.rs | 5 +- epp-client/src/host/delete.rs | 2 +- epp-client/src/host/info.rs | 3 +- epp-client/src/host/update.rs | 5 +- epp-client/src/lib.rs | 1 + epp-client/src/message/ack.rs | 2 +- epp-client/src/message/poll.rs | 2 +- epp-client/src/tests/de.rs | 1 - epp-client/src/tests/se.rs | 9 +- 31 files changed, 196 insertions(+), 202 deletions(-) rename epp-client/src/{epp/object/data.rs => common.rs} (65%) delete mode 100644 epp-client/src/epp/object.rs diff --git a/epp-client/src/epp/object/data.rs b/epp-client/src/common.rs similarity index 65% rename from epp-client/src/epp/object/data.rs rename to epp-client/src/common.rs index f8b901e..ff484ca 100644 --- a/epp-client/src/epp/object/data.rs +++ b/epp-client/src/common.rs @@ -1,7 +1,142 @@ //! Common data types included in EPP Requests and Responses -use crate::epp::object::StringValue; -use serde::{Deserialize, Serialize}; +use std::fmt::Display; + +use epp_client_macros::ElementName; +use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; + +const EPP_XMLNS: &str = "urn:ietf:params:xml:ns:epp-1.0"; + +/// Wraps String for easier serialization to and from values that are inner text +/// for tags rather than attributes +#[derive(Default, Serialize, Deserialize, Debug, PartialEq, Clone)] +pub struct StringValue(String); + +impl Display for StringValue { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +impl From<&str> for StringValue { + fn from(s: &str) -> Self { + Self(s.to_owned()) + } +} + +impl From for StringValue { + fn from(s: String) -> Self { + Self(s) + } +} + +/// Trait to set correct value for xml tags when tags are being generated from generic types +pub trait ElementName { + const ELEMENT: &'static str; +} + +#[derive(Serialize, Deserialize, Debug, PartialEq, ElementName)] +#[element_name(name = "empty")] +/// An empty placeholder tag. To be refactored to something more compliant later. +pub struct EmptyTag; + +/// An EPP XML Document that is used either as an EPP XML request or +/// an EPP XML response +#[derive(Deserialize, Debug, PartialEq)] +#[serde(rename = "epp")] +pub struct EppObject { + /// XML namespace for the <epp> tag + pub xmlns: String, + /// the request or response object that is set or received in the EPP XML document + #[serde(alias = "greeting", alias = "response")] + pub data: T, + // TODO: save serialized xml in the instance for debugging or client logging purposes + // #[serde(skip)] + // pub xml: Option, +} + +impl Serialize for EppObject { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut state = serializer.serialize_struct("epp", 4)?; + state.serialize_field("xmlns", &self.xmlns)?; + state.serialize_field(T::ELEMENT, &self.data)?; + state.end() + } +} + +/// The