//! Data types common to EPP Requests and Responses pub mod data; use epp_client_macros::*; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; use std::fmt::Display; use crate::epp::xml::EPP_XMLNS; /// Wraps String for easier serialization to and from values that are inner text /// for tags rather than attributes #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] pub struct StringValue(String); impl Default for StringValue { fn default() -> Self { Self(String::from("")) } } impl Display for StringValue { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } /// Trait for StringValue type to add easier conversion from str and String pub trait StringValueTrait { fn to_string_value(&self) -> StringValue; } impl StringValueTrait for &str { fn to_string_value(&self) -> StringValue { StringValue(self.to_string()) } } impl StringValueTrait for String { fn to_string_value(&self) -> StringValue { StringValue(self.to_string()) } } /// 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