From 5dce8501d5c6cde6c2d6018731472fbb016e216c Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 12 Mar 2022 17:32:24 +0100 Subject: [PATCH] Let callers create CommandDocument directly --- src/client.rs | 4 ++-- src/request.rs | 34 +++++++++++++++------------------- src/tests/mod.rs | 4 ++-- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/client.rs b/src/client.rs index 401b475..6b0312f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -19,7 +19,7 @@ pub use crate::connection::Connector; use crate::connection::{self, EppConnection}; use crate::error::Error; use crate::hello::{Greeting, GreetingDocument, HelloDocument}; -use crate::request::{Command, Extension, Transaction}; +use crate::request::{Command, CommandDocument, Extension, Transaction}; use crate::response::Response; use crate::xml::EppXml; @@ -126,7 +126,7 @@ impl EppClient { Ext: Extension + 'e, { let data = data.into(); - let document = >::command(data.command, data.extension, id); + let document = CommandDocument::new(data.command, data.extension, id); let xml = document.serialize()?; debug!("{}: request: {}", self.connection.registry, &xml); diff --git a/src/request.rs b/src/request.rs index 88cf340..301a688 100644 --- a/src/request.rs +++ b/src/request.rs @@ -15,19 +15,6 @@ 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 command<'c, 'e: 'c, 'i: 'c>( - &'c self, - extension: Option<&'e Ext>, - client_tr_id: &'i str, - ) -> CommandDocument<'c, Self, Ext> { - CommandDocument::new(CommandWrapper { - command: Self::COMMAND, - data: self, - extension, - client_tr_id: client_tr_id.into(), - }) - } - fn deserialize_response( epp_xml: &str, ) -> Result, Error> { @@ -55,7 +42,7 @@ pub trait Extension: Serialize + Debug { #[derive(Debug, PartialEq)] /// Type corresponding to the <command> tag in an EPP XML request /// with an <extension> tag -pub struct CommandWrapper<'a, D, E> { +struct CommandWrapper<'a, D, E> { pub command: &'static str, /// The instance that will be used to populate the <command> tag pub data: &'a D, @@ -80,16 +67,25 @@ impl<'a, D: Serialize, E: Serialize> Serialize for CommandWrapper<'a, D, E> { #[derive(Debug, PartialEq, Serialize)] #[serde(rename = "epp")] -pub struct CommandDocument<'a, D, E> { +pub(crate) struct CommandDocument<'a, Cmd, Ext> { xmlns: &'static str, - command: CommandWrapper<'a, D, E>, + command: CommandWrapper<'a, Cmd, Ext>, } -impl<'a, D, E> CommandDocument<'a, D, E> { - pub fn new(command: CommandWrapper<'a, D, E>) -> Self { +impl<'a, Cmd, Ext> CommandDocument<'a, Cmd, Ext> { + pub(crate) fn new(data: &'a Cmd, extension: Option<&'a Ext>, client_tr_id: &'a str) -> Self + where + Cmd: Transaction, + Ext: Extension, + { Self { xmlns: EPP_XMLNS, - command, + command: CommandWrapper { + command: Cmd::COMMAND, + data, + extension, + client_tr_id: client_tr_id.into(), + }, } } } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 203bd45..fa9a283 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -6,7 +6,7 @@ use regex::Regex; use crate::{ client::RequestData, - request::{Command, Extension, Transaction}, + request::{Command, CommandDocument, Extension, Transaction}, xml::EppXml, }; @@ -44,7 +44,7 @@ pub(crate) fn assert_serialized<'c, 'e, Cmd, Ext>( { let expected = get_xml(path).unwrap(); let req = req.into(); - let document = >::command(req.command, req.extension, CLTRID); + let document = CommandDocument::new(req.command, req.extension, CLTRID); let actual = EppXml::serialize(&document).unwrap(); assert_eq!(expected, actual); }