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.
This commit is contained in:
Dirkjan Ochtman 2023-03-02 15:38:48 +01:00
parent f5ea188fbb
commit 0c6283fbf4
3 changed files with 46 additions and 46 deletions

View File

@ -6,28 +6,55 @@ use crate::extensions::low_balance::LowBalance;
use crate::host::info::InfoData; use crate::host::info::InfoData;
use crate::request::{Command, Transaction}; use crate::request::{Command, Transaction};
impl<'a> Transaction<NoExtension> for MessagePoll<'a> {} impl Transaction<NoExtension> for MessagePoll {}
impl<'a> Command for MessagePoll<'a> { impl Command for MessagePoll {
type Response = MessagePollResponse; type Response = MessagePollResponse;
const COMMAND: &'static str = "poll"; const COMMAND: &'static str = "poll";
} }
impl Transaction<NoExtension> for MessageAck<'_> {}
impl Command for MessageAck<'_> {
type Response = String;
const COMMAND: &'static str = "poll";
}
// Request // Request
#[derive(Debug, ToXml)] /// Type for EPP XML `<poll>` command with `op="req"`
/// Type for EPP XML `<poll>` command for message poll #[derive(Debug)]
#[xml(rename = "poll", ns(EPP_XMLNS))] pub struct MessagePoll;
pub struct MessagePoll<'a> {
/// The type of operation to perform impl ToXml for MessagePoll {
/// The value is "req" for message polling fn serialize<W: std::fmt::Write + ?Sized>(
#[xml(attribute)] &self,
op: &'a str, _: Option<instant_xml::Id<'_>>,
serializer: &mut instant_xml::Serializer<W>,
) -> 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> { /// Type for EPP XML `<poll>` command with `op="ack"`
fn default() -> Self { #[derive(Debug)]
Self { op: "req" } pub struct MessageAck<'a> {
/// The ID of the message to be acknowledged
pub message_id: &'a str,
}
impl ToXml for MessageAck<'_> {
fn serialize<W: std::fmt::Write + ?Sized>(
&self,
_: Option<instant_xml::Id<'_>>,
serializer: &mut instant_xml::Serializer<W>,
) -> 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), LowBalance(LowBalance),
} }
impl<'a> Transaction<NoExtension> for MessageAck<'a> {}
impl<'a> Command for MessageAck<'a> {
type Response = String;
const COMMAND: &'static str = "poll";
}
#[derive(Debug, ToXml)]
/// Type for EPP XML `<poll>` 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)] #[cfg(test)]
mod tests { mod tests {
use super::{MessageAck, MessagePoll, MessagePollResponse}; use super::{MessageAck, MessagePoll, MessagePollResponse};
@ -86,7 +84,9 @@ mod tests {
#[test] #[test]
fn ack_command() { fn ack_command() {
let object = MessageAck::new("12345"); let object = MessageAck {
message_id: "12345",
};
assert_serialized("request/poll/ack.xml", &object); assert_serialized("request/poll/ack.xml", &object);
} }
@ -104,7 +104,7 @@ mod tests {
#[test] #[test]
fn poll_command() { fn poll_command() {
let object = MessagePoll::default(); let object = MessagePoll;
assert_serialized("request/poll/poll.xml", &object); assert_serialized("request/poll/poll.xml", &object);
} }

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0"> <epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<command> <command>
<poll op="ack" msgID="12345"></poll> <poll op="ack" msgID="12345" />
<clTRID>cltrid:1626454866</clTRID> <clTRID>cltrid:1626454866</clTRID>
</command> </command>
</epp> </epp>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0"> <epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<command> <command>
<poll op="req"></poll> <poll op="req" />
<clTRID>cltrid:1626454866</clTRID> <clTRID>cltrid:1626454866</clTRID>
</command> </command>
</epp> </epp>