Skip to main content

instant_epp/host/
mod.rs

1//! Mapping for EPP host objects
2//!
3//! As described in [RFC 5732](https://tools.ietf.org/html/rfc5732).
4
5use std::borrow::Cow;
6use std::fmt;
7use std::net::IpAddr;
8
9use instant_xml::ser::Context;
10use instant_xml::{Deserializer, FromXml, Serializer, ToXml};
11
12pub mod check;
13pub use check::HostCheck;
14
15pub mod create;
16pub use create::HostCreate;
17
18pub mod delete;
19pub use delete::HostDelete;
20
21pub mod info;
22pub use info::{HostInfo, InfoData};
23
24pub mod update;
25pub use update::HostUpdate;
26
27pub const XMLNS: &str = "urn:ietf:params:xml:ns:host-1.0";
28
29/// The `<status>` type on contact transactions
30#[derive(Clone, Copy, Debug, Eq, PartialEq)]
31pub enum Status {
32    ClientDeleteProhibited,
33    ServerDeleteProhibited,
34    ClientUpdateProhibited,
35    ServerUpdateProhibited,
36    Linked,
37    Ok,
38    PendingCreate,
39    PendingDelete,
40    PendingTransfer,
41    PendingUpdate,
42}
43
44impl Status {
45    pub fn as_str(&self) -> &'static str {
46        use Status::*;
47        match self {
48            ClientDeleteProhibited => "clientDeleteProhibited",
49            ServerDeleteProhibited => "serverDeleteProhibited",
50            ClientUpdateProhibited => "clientUpdateProhibited",
51            ServerUpdateProhibited => "serverUpdateProhibited",
52            Linked => "linked",
53            Ok => "ok",
54            PendingCreate => "pendingCreate",
55            PendingDelete => "pendingDelete",
56            PendingTransfer => "pendingTransfer",
57            PendingUpdate => "pendingUpdate",
58        }
59    }
60}
61
62impl ToXml for Status {
63    fn serialize<W: fmt::Write + ?Sized>(
64        &self,
65        _: Option<instant_xml::Id<'_>>,
66        serializer: &mut Serializer<W>,
67    ) -> Result<(), instant_xml::Error> {
68        serializer.write_start("status", XMLNS, None::<Context<0>>)?;
69        serializer.write_attr("s", XMLNS, &self.as_str())?;
70        serializer.end_empty()
71    }
72}
73
74impl<'xml> FromXml<'xml> for Status {
75    fn matches(id: instant_xml::Id<'_>, _: Option<instant_xml::Id<'_>>) -> bool {
76        id == instant_xml::Id {
77            ns: XMLNS,
78            name: "status",
79        }
80    }
81
82    fn deserialize<'cx>(
83        into: &mut Self::Accumulator,
84        field: &'static str,
85        deserializer: &mut Deserializer<'cx, 'xml>,
86    ) -> Result<(), instant_xml::Error> {
87        use instant_xml::de::Node;
88        use instant_xml::{Error, Id};
89
90        let node = match deserializer.next() {
91            Some(result) => result?,
92            None => return Err(Error::MissingValue(field)),
93        };
94
95        let attr = match node {
96            Node::Attribute(attr) => attr,
97            Node::Open(_) | Node::Text(_) => return Err(Error::MissingValue(field)),
98            node => return Err(Error::UnexpectedNode(format!("{node:?} in Status"))),
99        };
100
101        let id = deserializer.attribute_id(&attr)?;
102        let expected = Id { ns: "", name: "s" };
103        if id != expected {
104            return Err(Error::MissingValue(field));
105        }
106
107        *into = Some(match attr.value.as_ref() {
108            "clientDeleteProhibited" => Self::ClientDeleteProhibited,
109            "serverDeleteProhibited" => Self::ServerDeleteProhibited,
110            "clientUpdateProhibited" => Self::ClientUpdateProhibited,
111            "serverUpdateProhibited" => Self::ServerUpdateProhibited,
112            "linked" => Self::Linked,
113            "ok" => Self::Ok,
114            "pendingCreate" => Self::PendingCreate,
115            "pendingDelete" => Self::PendingDelete,
116            "pendingTransfer" => Self::PendingTransfer,
117            "pendingUpdate" => Self::PendingUpdate,
118            val => return Err(Error::UnexpectedValue(format!("invalid status {val:?}"))),
119        });
120
121        deserializer.ignore()?;
122        Ok(())
123    }
124
125    type Accumulator = Option<Self>;
126    const KIND: instant_xml::Kind = instant_xml::Kind::Element;
127}
128
129/// The `<hostAddr>` types domain or host transactions
130#[derive(Debug, FromXml, ToXml)]
131#[xml(rename = "addr", ns(XMLNS))]
132struct HostAddr<'a> {
133    #[xml(attribute, rename = "ip")]
134    ip_version: Option<Cow<'a, str>>,
135    #[xml(direct)]
136    address: Cow<'a, str>,
137}
138
139impl From<&IpAddr> for HostAddr<'static> {
140    fn from(addr: &IpAddr) -> Self {
141        Self {
142            ip_version: Some(match addr {
143                IpAddr::V4(_) => "v4".into(),
144                IpAddr::V6(_) => "v6".into(),
145            }),
146            address: addr.to_string().into(),
147        }
148    }
149}
150
151pub(crate) fn serialize_host_addrs_option<T: AsRef<[IpAddr]>, W: fmt::Write + ?Sized>(
152    addrs: &Option<T>,
153    serializer: &mut Serializer<'_, W>,
154) -> Result<(), instant_xml::Error> {
155    let addrs = match addrs {
156        Some(addrs) => addrs.as_ref(),
157        None => return Ok(()),
158    };
159
160    for addr in addrs {
161        HostAddr::from(addr).serialize(None, serializer)?;
162    }
163
164    Ok(())
165}