Skip to main content

instant_epp/contact/
mod.rs

1//! Mapping for EPP contact objects
2//!
3//! As described in [RFC 5733](https://tools.ietf.org/html/rfc5733).
4
5use std::borrow::Cow;
6use std::fmt;
7use std::str::FromStr;
8
9use instant_xml::ser::Context;
10use instant_xml::{display_to_xml, from_xml_str, Deserializer, FromXml, Serializer, ToXml};
11
12pub mod check;
13pub use check::ContactCheck;
14
15pub mod create;
16pub use create::ContactCreate;
17
18pub mod delete;
19pub use delete::ContactDelete;
20
21pub mod info;
22pub use info::ContactInfo;
23
24pub mod update;
25pub use update::ContactUpdate;
26
27pub const XMLNS: &str = "urn:ietf:params:xml:ns:contact-1.0";
28
29#[derive(Clone, Debug)]
30pub struct Country(celes::Country);
31
32impl<'xml> FromXml<'xml> for Country {
33    fn matches(id: instant_xml::Id<'_>, _: Option<instant_xml::Id<'_>>) -> bool {
34        id == instant_xml::Id {
35            ns: XMLNS,
36            name: "cc",
37        }
38    }
39
40    fn deserialize<'cx>(
41        into: &mut Self::Accumulator,
42        field: &'static str,
43        deserializer: &mut instant_xml::Deserializer<'cx, 'xml>,
44    ) -> Result<(), instant_xml::Error> {
45        from_xml_str(into, field, deserializer)
46    }
47
48    type Accumulator = Option<Self>;
49    const KIND: instant_xml::Kind = instant_xml::Kind::Scalar;
50}
51
52impl ToXml for Country {
53    fn serialize<W: fmt::Write + ?Sized>(
54        &self,
55        field: Option<instant_xml::Id<'_>>,
56        serializer: &mut instant_xml::Serializer<W>,
57    ) -> Result<(), instant_xml::Error> {
58        display_to_xml(&self.0.alpha2, field, serializer)
59    }
60}
61
62impl FromStr for Country {
63    type Err = <celes::Country as FromStr>::Err;
64
65    fn from_str(s: &str) -> Result<Self, Self::Err> {
66        Ok(Self(celes::Country::from_str(s)?))
67    }
68}
69
70impl std::ops::Deref for Country {
71    type Target = celes::Country;
72
73    fn deref(&self) -> &Self::Target {
74        &self.0
75    }
76}
77
78/// The `<authInfo>` tag for domain and contact transactions
79#[derive(Clone, Debug, FromXml, PartialEq, ToXml)]
80#[xml(rename = "authInfo", ns(XMLNS))]
81pub struct ContactAuthInfo<'a> {
82    /// The `<pw>` tag under `<authInfo>`
83    #[xml(rename = "pw")]
84    pub password: Cow<'a, str>,
85}
86
87impl<'a> ContactAuthInfo<'a> {
88    /// Creates a ContactAuthInfo instance with the given password
89    pub fn new(password: &'a str) -> Self {
90        Self {
91            password: password.into(),
92        }
93    }
94}
95
96/// The data for `<voice>` types on domain transactions
97#[derive(Clone, Debug, FromXml, PartialEq, ToXml)]
98#[xml(rename = "voice", ns(XMLNS))]
99pub struct Voice<'a> {
100    /// The value of the 'x' attr on `<voice>` and `<fax>` tags
101    #[xml(rename = "x", attribute)]
102    pub extension: Option<Cow<'a, str>>,
103    /// The inner text on the `<voice>` and `<fax>` tags
104    #[xml(direct)]
105    pub number: Cow<'a, str>,
106}
107
108impl<'a> Voice<'a> {
109    /// Creates a new Phone instance with a given phone number
110    pub fn new(number: &'a str) -> Self {
111        Self {
112            extension: None,
113            number: number.into(),
114        }
115    }
116
117    /// Sets the extension value of the Phone type
118    pub fn set_extension(&mut self, ext: &'a str) {
119        self.extension = Some(ext.into());
120    }
121}
122
123/// The data for `<voice>` and `<fax>` types on domain transactions
124#[derive(Clone, Debug, FromXml, PartialEq, ToXml)]
125#[xml(rename = "fax", ns(XMLNS))]
126pub struct Fax<'a> {
127    /// The value of the 'x' attr on `<voice>` and `<fax>` tags
128    #[xml(rename = "x", attribute)]
129    pub extension: Option<Cow<'a, str>>,
130    /// The inner text on the `<voice>` and `<fax>` tags
131    #[xml(direct)]
132    pub number: Cow<'a, str>,
133}
134
135impl<'a> Fax<'a> {
136    /// Creates a new Phone instance with a given phone number
137    pub fn new(number: &'a str) -> Self {
138        Self {
139            extension: None,
140            number: number.into(),
141        }
142    }
143
144    /// Sets the extension value of the Phone type
145    pub fn set_extension(&mut self, ext: &'a str) {
146        self.extension = Some(ext.into());
147    }
148}
149
150/// The `<addr>` type on contact transactions
151#[derive(Clone, Debug, FromXml, ToXml)]
152#[xml(rename = "addr", ns(XMLNS))]
153pub struct Address<'a> {
154    /// The `<street>` tags under `<addr>`
155    pub street: Vec<Cow<'a, str>>,
156    /// The `<city>` tag under `<addr>`
157    pub city: Cow<'a, str>,
158    /// The `<sp>` tag under `<addr>`
159    #[xml(rename = "sp")]
160    pub province: Option<Cow<'a, str>>,
161    /// The `<pc>` tag under `<addr>`
162    #[xml(rename = "pc")]
163    pub postal_code: Option<Cow<'a, str>>,
164    /// The `<cc>` tag under `<addr>`
165    #[xml(rename = "cc")]
166    pub country: Country,
167}
168
169impl<'a> Address<'a> {
170    /// Creates a new Address instance
171    pub fn new(
172        street: &[&'a str],
173        city: &'a str,
174        province: Option<&'a str>,
175        postal_code: Option<&'a str>,
176        country: Country,
177    ) -> Self {
178        let street = street.iter().map(|&s| s.into()).collect();
179
180        Self {
181            street,
182            city: city.into(),
183            province: province.map(|sp| sp.into()),
184            postal_code: postal_code.map(|pc| pc.into()),
185            country,
186        }
187    }
188}
189
190/// The `<postalInfo>` type on contact transactions
191#[derive(Clone, Debug, FromXml, ToXml)]
192#[xml(rename = "postalInfo", ns(XMLNS))]
193pub struct PostalInfo<'a> {
194    /// The 'type' attr on `<postalInfo>`
195    #[xml(rename = "type", attribute)]
196    pub info_type: InfoType,
197    /// The `<name>` tag under `<postalInfo>`
198    pub name: Cow<'a, str>,
199    /// The `<org>` tag under `<postalInfo>`
200    #[xml(rename = "org")]
201    pub organization: Option<Cow<'a, str>>,
202    /// The `<addr>` tag under `<postalInfo>`
203    pub address: Address<'a>,
204}
205
206impl<'a> PostalInfo<'a> {
207    /// Creates a new PostalInfo instance
208    pub fn new(
209        info_type: InfoType,
210        name: &'a str,
211        organization: Option<&'a str>,
212        address: Address<'a>,
213    ) -> Self {
214        Self {
215            info_type,
216            name: name.into(),
217            organization: organization.map(|org| org.into()),
218            address,
219        }
220    }
221}
222
223#[derive(Debug, Clone, Copy, PartialEq, ToXml, FromXml)]
224#[xml(scalar)]
225pub enum InfoType {
226    #[xml(rename = "loc")]
227    Local,
228    #[xml(rename = "int")]
229    International,
230}
231
232/// The `<status>` type on contact transactions
233#[derive(Clone, Copy, Debug, Eq, PartialEq)]
234pub enum Status {
235    ClientDeleteProhibited,
236    ServerDeleteProhibited,
237    ClientTransferProhibited,
238    ServerTransferProhibited,
239    ClientUpdateProhibited,
240    ServerUpdateProhibited,
241    Linked,
242    Ok,
243    PendingCreate,
244    PendingDelete,
245    PendingTransfer,
246    PendingUpdate,
247}
248
249impl Status {
250    pub fn as_str(&self) -> &'static str {
251        use Status::*;
252        match self {
253            ClientDeleteProhibited => "clientDeleteProhibited",
254            ServerDeleteProhibited => "serverDeleteProhibited",
255            ClientTransferProhibited => "clientTransferProhibited",
256            ServerTransferProhibited => "serverTransferProhibited",
257            ClientUpdateProhibited => "clientUpdateProhibited",
258            ServerUpdateProhibited => "serverUpdateProhibited",
259            Linked => "linked",
260            Ok => "ok",
261            PendingCreate => "pendingCreate",
262            PendingDelete => "pendingDelete",
263            PendingTransfer => "pendingTransfer",
264            PendingUpdate => "pendingUpdate",
265        }
266    }
267}
268
269impl ToXml for Status {
270    fn serialize<W: fmt::Write + ?Sized>(
271        &self,
272        _: Option<instant_xml::Id<'_>>,
273        serializer: &mut Serializer<W>,
274    ) -> Result<(), instant_xml::Error> {
275        serializer.write_start("status", XMLNS, None::<Context<0>>)?;
276        serializer.write_attr("s", XMLNS, &self.as_str())?;
277        serializer.end_empty()
278    }
279}
280
281impl<'xml> FromXml<'xml> for Status {
282    fn matches(id: instant_xml::Id<'_>, _: Option<instant_xml::Id<'_>>) -> bool {
283        id == instant_xml::Id {
284            ns: XMLNS,
285            name: "status",
286        }
287    }
288
289    fn deserialize<'cx>(
290        into: &mut Self::Accumulator,
291        field: &'static str,
292        deserializer: &mut Deserializer<'cx, 'xml>,
293    ) -> Result<(), instant_xml::Error> {
294        use instant_xml::de::Node;
295        use instant_xml::{Error, Id};
296
297        let node = match deserializer.next() {
298            Some(result) => result?,
299            None => return Err(Error::MissingValue(field)),
300        };
301
302        let attr = match node {
303            Node::Attribute(attr) => attr,
304            Node::Open(_) | Node::Text(_) => return Err(Error::MissingValue(field)),
305            node => return Err(Error::UnexpectedNode(format!("{node:?} in Status"))),
306        };
307
308        let id = deserializer.attribute_id(&attr)?;
309        let expected = Id { ns: "", name: "s" };
310        if id != expected {
311            return Err(Error::MissingValue(field));
312        }
313
314        *into = Some(match attr.value.as_ref() {
315            "clientDeleteProhibited" => Self::ClientDeleteProhibited,
316            "serverDeleteProhibited" => Self::ServerDeleteProhibited,
317            "clientTransferProhibited" => Self::ClientTransferProhibited,
318            "serverTransferProhibited" => Self::ServerTransferProhibited,
319            "clientUpdateProhibited" => Self::ClientUpdateProhibited,
320            "serverUpdateProhibited" => Self::ServerUpdateProhibited,
321            "linked" => Self::Linked,
322            "ok" => Self::Ok,
323            "pendingCreate" => Self::PendingCreate,
324            "pendingDelete" => Self::PendingDelete,
325            "pendingTransfer" => Self::PendingTransfer,
326            "pendingUpdate" => Self::PendingUpdate,
327            val => return Err(Error::UnexpectedValue(format!("invalid status {val:?}"))),
328        });
329
330        deserializer.ignore()?;
331        Ok(())
332    }
333
334    type Accumulator = Option<Self>;
335    const KIND: instant_xml::Kind = instant_xml::Kind::Element;
336}