instant_epp/host/
check.rs

1//! Types for EPP host check request
2
3use std::fmt::{self, Debug};
4
5use instant_xml::{FromXml, Serializer, ToXml};
6
7use super::XMLNS;
8use crate::common::{NoExtension, EPP_XMLNS};
9use crate::request::{Command, Transaction};
10
11impl<'a> Transaction<NoExtension> for HostCheck<'a> {}
12
13impl<'a> Command for HostCheck<'a> {
14    type Response = CheckData;
15    const COMMAND: &'static str = "check";
16}
17
18// Request
19
20/// Type for data under the host `<check>` tag
21#[derive(Debug, ToXml)]
22#[xml(rename = "check", ns(XMLNS))]
23struct HostCheckData<'a> {
24    /// List of hosts to be checked for availability
25    name: &'a [&'a str],
26}
27
28fn serialize_hosts<W: fmt::Write + ?Sized>(
29    hosts: &[&str],
30    serializer: &mut Serializer<W>,
31) -> Result<(), instant_xml::Error> {
32    HostCheckData { name: hosts }.serialize(None, serializer)
33}
34
35/// The EPP `check` command for hosts
36#[derive(Clone, Debug, ToXml)]
37#[xml(rename = "check", ns(EPP_XMLNS))]
38pub struct HostCheck<'a> {
39    /// The list of hosts to be checked
40    #[xml(serialize_with = "serialize_hosts")]
41    pub hosts: &'a [&'a str],
42}
43
44// Response
45
46#[derive(Debug, FromXml)]
47#[xml(rename = "name", ns(XMLNS))]
48pub struct Checked {
49    #[xml(attribute, rename = "avail")]
50    pub available: bool,
51    #[xml(attribute)]
52    pub reason: Option<String>,
53    #[xml(direct)]
54    pub id: String,
55}
56
57#[derive(Debug, FromXml)]
58#[xml(rename = "cd", ns(XMLNS))]
59pub struct CheckedHost {
60    /// Data under the `<cd>` tag
61    #[xml(rename = "cd")]
62    pub inner: Checked,
63}
64
65/// Type that represents the `<chkData>` tag for host check response
66#[derive(Debug, FromXml)]
67#[xml(rename = "chkData", ns(XMLNS))]
68pub struct CheckData {
69    pub list: Vec<CheckedHost>,
70}
71
72#[cfg(test)]
73mod tests {
74    use super::HostCheck;
75    use crate::response::ResultCode;
76    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};
77
78    #[test]
79    fn command() {
80        let object = HostCheck {
81            hosts: &["ns1.eppdev-1.com", "host1.eppdev-1.com"],
82        };
83        assert_serialized("request/host/check.xml", &object);
84    }
85
86    #[test]
87    fn response() {
88        let object = response_from_file::<HostCheck>("response/host/check.xml");
89        let result = object.res_data().unwrap();
90
91        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
92        assert_eq!(object.result.message, SUCCESS_MSG);
93        assert_eq!(result.list[0].inner.id, "host1.eppdev-1.com");
94        assert!(result.list[0].inner.available);
95        assert_eq!(result.list[1].inner.id, "ns1.testing.com");
96        assert!(!result.list[1].inner.available);
97        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
98        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
99    }
100}