1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Types for EPP host check request

use std::fmt::{self, Debug};

use instant_xml::{FromXml, Serializer, ToXml};

use super::XMLNS;
use crate::common::{NoExtension, EPP_XMLNS};
use crate::request::{Command, Transaction};

impl<'a> Transaction<NoExtension> for HostCheck<'a> {}

impl<'a> Command for HostCheck<'a> {
    type Response = CheckData;
    const COMMAND: &'static str = "check";
}

// Request

/// Type for data under the host `<check>` tag
#[derive(Debug, ToXml)]
#[xml(rename = "check", ns(XMLNS))]
struct HostCheckData<'a> {
    /// List of hosts to be checked for availability
    name: &'a [&'a str],
}

fn serialize_hosts<W: fmt::Write + ?Sized>(
    hosts: &[&str],
    serializer: &mut Serializer<W>,
) -> Result<(), instant_xml::Error> {
    HostCheckData { name: hosts }.serialize(None, serializer)
}

/// The EPP `check` command for hosts
#[derive(Clone, Debug, ToXml)]
#[xml(rename = "check", ns(EPP_XMLNS))]
pub struct HostCheck<'a> {
    /// The list of hosts to be checked
    #[xml(serialize_with = "serialize_hosts")]
    pub hosts: &'a [&'a str],
}

// Response

#[derive(Debug, FromXml)]
#[xml(rename = "name", ns(XMLNS))]
pub struct Checked {
    #[xml(attribute, rename = "avail")]
    pub available: bool,
    #[xml(attribute)]
    pub reason: Option<String>,
    #[xml(direct)]
    pub id: String,
}

#[derive(Debug, FromXml)]
#[xml(rename = "cd", ns(XMLNS))]
pub struct CheckedHost {
    /// Data under the `<cd>` tag
    #[xml(rename = "cd")]
    pub inner: Checked,
}

/// Type that represents the `<chkData>` tag for host check response
#[derive(Debug, FromXml)]
#[xml(rename = "chkData", ns(XMLNS))]
pub struct CheckData {
    pub list: Vec<CheckedHost>,
}

#[cfg(test)]
mod tests {
    use super::HostCheck;
    use crate::response::ResultCode;
    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};

    #[test]
    fn command() {
        let object = HostCheck {
            hosts: &["ns1.eppdev-1.com", "host1.eppdev-1.com"],
        };
        assert_serialized("request/host/check.xml", &object);
    }

    #[test]
    fn response() {
        let object = response_from_file::<HostCheck>("response/host/check.xml");
        let result = object.res_data().unwrap();

        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
        assert_eq!(object.result.message, SUCCESS_MSG);
        assert_eq!(result.list[0].inner.id, "host1.eppdev-1.com");
        assert!(result.list[0].inner.available);
        assert_eq!(result.list[1].inner.id, "ns1.testing.com");
        assert!(!result.list[1].inner.available);
        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }
}