From 0c6283fbf4bf0ad2a2a175013fd26edc25945bda Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 2 Mar 2023 15:38:48 +0100 Subject: [PATCH] Provide more precise types for poll/ack commands Ideally we might have modeled this with an enum with one named field in the variants, but instant-xml is not yet up to the task; in the mean-time, this provides more robust types, and manual ToXml implementations aren't too verbose anyway. --- src/poll.rs | 88 +++++++++++++-------------- tests/resources/request/poll/ack.xml | 2 +- tests/resources/request/poll/poll.xml | 2 +- 3 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/poll.rs b/src/poll.rs index 11df360..386a491 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -6,28 +6,55 @@ use crate::extensions::low_balance::LowBalance; use crate::host::info::InfoData; use crate::request::{Command, Transaction}; -impl<'a> Transaction for MessagePoll<'a> {} +impl Transaction for MessagePoll {} -impl<'a> Command for MessagePoll<'a> { +impl Command for MessagePoll { type Response = MessagePollResponse; const COMMAND: &'static str = "poll"; } +impl Transaction for MessageAck<'_> {} + +impl Command for MessageAck<'_> { + type Response = String; + const COMMAND: &'static str = "poll"; +} + // Request -#[derive(Debug, ToXml)] -/// Type for EPP XML `` command for message poll -#[xml(rename = "poll", ns(EPP_XMLNS))] -pub struct MessagePoll<'a> { - /// The type of operation to perform - /// The value is "req" for message polling - #[xml(attribute)] - op: &'a str, +/// Type for EPP XML `` command with `op="req"` +#[derive(Debug)] +pub struct MessagePoll; + +impl ToXml for MessagePoll { + fn serialize( + &self, + _: Option>, + serializer: &mut instant_xml::Serializer, + ) -> Result<(), instant_xml::Error> { + serializer.write_start("poll", EPP_XMLNS)?; + serializer.write_attr("op", EPP_XMLNS, &"req")?; + serializer.end_empty() + } } -impl Default for MessagePoll<'static> { - fn default() -> Self { - Self { op: "req" } +/// Type for EPP XML `` command with `op="ack"` +#[derive(Debug)] +pub struct MessageAck<'a> { + /// The ID of the message to be acknowledged + pub message_id: &'a str, +} + +impl ToXml for MessageAck<'_> { + fn serialize( + &self, + _: Option>, + serializer: &mut instant_xml::Serializer, + ) -> Result<(), instant_xml::Error> { + serializer.write_start("poll", EPP_XMLNS)?; + serializer.write_attr("op", EPP_XMLNS, &"ack")?; + serializer.write_attr("msgID", EPP_XMLNS, &self.message_id)?; + serializer.end_empty() } } @@ -45,35 +72,6 @@ pub enum MessagePollResponse { LowBalance(LowBalance), } -impl<'a> Transaction for MessageAck<'a> {} - -impl<'a> Command for MessageAck<'a> { - type Response = String; - const COMMAND: &'static str = "poll"; -} - -#[derive(Debug, ToXml)] -/// Type for EPP XML `` command for message ack -#[xml(rename = "poll", ns(EPP_XMLNS))] -pub struct MessageAck<'a> { - /// The type of operation to perform - /// The value is "ack" for message acknowledgement - #[xml(attribute)] - op: &'a str, - /// The ID of the message to be acknowledged - #[xml(rename = "msgID", attribute)] - message_id: &'a str, -} - -impl<'a> MessageAck<'a> { - pub fn new(message_id: &'a str) -> Self { - Self { - op: "ack", - message_id, - } - } -} - #[cfg(test)] mod tests { use super::{MessageAck, MessagePoll, MessagePollResponse}; @@ -86,7 +84,9 @@ mod tests { #[test] fn ack_command() { - let object = MessageAck::new("12345"); + let object = MessageAck { + message_id: "12345", + }; assert_serialized("request/poll/ack.xml", &object); } @@ -104,7 +104,7 @@ mod tests { #[test] fn poll_command() { - let object = MessagePoll::default(); + let object = MessagePoll; assert_serialized("request/poll/poll.xml", &object); } diff --git a/tests/resources/request/poll/ack.xml b/tests/resources/request/poll/ack.xml index 48a74bb..d05f1a1 100644 --- a/tests/resources/request/poll/ack.xml +++ b/tests/resources/request/poll/ack.xml @@ -1,7 +1,7 @@ - + cltrid:1626454866 diff --git a/tests/resources/request/poll/poll.xml b/tests/resources/request/poll/poll.xml index 715c181..da5867e 100644 --- a/tests/resources/request/poll/poll.xml +++ b/tests/resources/request/poll/poll.xml @@ -1,7 +1,7 @@ - + cltrid:1626454866