Skip to main content

instant_epp/
hello.rs

1use std::fmt::Debug;
2
3use chrono::{DateTime, Utc};
4use instant_xml::{Deserializer, FromXml, ToXml};
5
6use crate::common::{ServiceExtension, EPP_XMLNS};
7
8// Request
9
10#[derive(Debug, PartialEq, ToXml)]
11#[xml(rename = "hello", ns(EPP_XMLNS))]
12pub(crate) struct Hello;
13
14// Response
15
16/// Type for data within the `<svcMenu>` section of an EPP greeting
17#[derive(Debug, Eq, PartialEq)]
18pub struct ServiceMenu {
19    pub version: String,
20    pub langs: Vec<String>,
21    pub services: Services,
22}
23
24/// Offered services by the remote EPP server, represented by the `<svcs>` tag in EPP greeting XML
25#[derive(Debug, Eq, PartialEq)]
26pub struct Services {
27    /// The service URIs being offered by the EPP server represented by `<objURI>` in EPP XML
28    pub obj_uris: Vec<String>,
29    /// The service extensions being offered by the EPP server represented by `<svcExtension>` in EPP XML
30    pub svc_ext: Option<ServiceExtension<'static>>,
31}
32
33/// Simplified service menu type for deserialization to `ServiceMenu` type from EPP greeting XML
34///
35/// Implements the `epp:svcMenuType` type defined in RFC 5730.
36#[derive(Debug, FromXml, PartialEq)]
37#[xml(ns(EPP_XMLNS), rename = "svcMenu")]
38struct FlattenedServiceMenu {
39    version: String,
40    #[xml(rename = "lang")]
41    langs: Vec<String>,
42    #[xml(rename = "objURI")]
43    obj_uris: Vec<String>,
44    #[xml(rename = "svcExtension")]
45    svc_ext: Option<ServiceExtension<'static>>,
46}
47
48impl<'xml> FromXml<'xml> for ServiceMenu {
49    fn matches(id: instant_xml::Id<'_>, field: Option<instant_xml::Id<'_>>) -> bool {
50        FlattenedServiceMenu::matches(id, field)
51    }
52
53    /// Deserializes the `<svcMenu>` data to the `ServiceMenu` type
54    fn deserialize<'cx>(
55        into: &mut Self::Accumulator,
56        field: &'static str,
57        deserializer: &mut Deserializer<'cx, 'xml>,
58    ) -> Result<(), instant_xml::Error> {
59        let mut value = None;
60        FlattenedServiceMenu::deserialize(&mut value, field, deserializer)?;
61        let flattened = match value {
62            Some(value) => value,
63            None => return Ok(()),
64        };
65
66        *into = Some(Self {
67            version: flattened.version,
68            langs: flattened.langs,
69            services: Services {
70                obj_uris: flattened.obj_uris,
71                svc_ext: flattened.svc_ext,
72            },
73        });
74
75        Ok(())
76    }
77
78    type Accumulator = Option<Self>;
79    const KIND: instant_xml::Kind = FlattenedServiceMenu::KIND;
80}
81
82/// Type corresponding to `<all>` in the EPP greeting XML
83#[derive(Debug, Eq, FromXml, PartialEq)]
84#[xml(rename = "all", ns(EPP_XMLNS))]
85pub struct All;
86
87/// Type corresponding to `<none>` in the EPP greeting XML
88#[derive(Debug, Eq, FromXml, PartialEq)]
89#[xml(rename = "none", ns(EPP_XMLNS))]
90pub struct NoAccess;
91
92/// Type corresponding to `<null>` in the EPP greeting XML
93#[derive(Debug, Eq, FromXml, PartialEq)]
94#[xml(rename = "null", ns(EPP_XMLNS))]
95pub struct Null;
96
97/// Type corresponding to `<personal>` in the EPP greeting XML
98#[derive(Debug, Eq, FromXml, PartialEq)]
99#[xml(rename = "personal", ns(EPP_XMLNS))]
100pub struct Personal;
101
102/// Type corresponding to `<personalAndOther>` in the EPP greeting XML
103#[derive(Debug, Eq, FromXml, PartialEq)]
104#[xml(rename = "personalAndOther", ns(EPP_XMLNS))]
105pub struct PersonalAndOther;
106
107/// Type corresponding to `<other>` in the EPP greeting XML
108#[derive(Debug, Eq, FromXml, PartialEq)]
109#[xml(rename = "other", ns(EPP_XMLNS))]
110pub struct Other;
111
112/// Type corresponding to possible `<retention>` type values
113#[derive(Debug, Eq, FromXml, PartialEq)]
114#[xml(forward)]
115pub enum AccessType {
116    /// Data for the `<all>` tag
117    All(All),
118    /// Data for the `<none>` tag
119    NoAccess(NoAccess),
120    /// Data for the `<null>` tag
121    Null(Null),
122    /// Data for the `<personal>` tag
123    Personal(Personal),
124    /// Data for the `<personalAndOther>` tag
125    PersonalAndOther(PersonalAndOther),
126    /// Data for the `<other>` tag
127    Other(Other),
128}
129
130#[derive(Debug, Eq, FromXml, PartialEq)]
131#[xml(rename = "access", ns(EPP_XMLNS))]
132pub struct Access {
133    inner: AccessType,
134}
135
136/// Type corresponding to possible `<purpose>` type values
137#[derive(Debug, Eq, FromXml, PartialEq)]
138#[xml(forward)]
139pub enum PurposeType {
140    /// Data for the `<admin>` tag
141    Admin(Admin),
142    /// Data for the `<contact>` tag
143    Contact(Contact),
144    /// Data for the `<prov>` tag
145    Prov(Prov),
146    /// Data for the `<other>` tag
147    OtherPurpose(OtherPurpose),
148}
149
150#[derive(Debug, Eq, FromXml, PartialEq)]
151#[xml(rename = "admin", ns(EPP_XMLNS))]
152pub struct Admin;
153
154#[derive(Debug, Eq, FromXml, PartialEq)]
155#[xml(rename = "contact", ns(EPP_XMLNS))]
156pub struct Contact;
157
158#[derive(Debug, Eq, FromXml, PartialEq)]
159#[xml(rename = "prov", ns(EPP_XMLNS))]
160pub struct Prov;
161
162#[derive(Debug, Eq, FromXml, PartialEq)]
163#[xml(rename = "otherPurpose", ns(EPP_XMLNS))]
164pub struct OtherPurpose;
165
166/// Type corresponding to `<purpose>` in the EPP greeting XML
167#[derive(Debug, Eq, FromXml, PartialEq)]
168#[xml(rename = "purpose", ns(EPP_XMLNS))]
169pub struct Purpose {
170    pub purpose: Vec<PurposeType>,
171}
172
173/// Type corresponding to possible `<purpose>` type values
174#[derive(Debug, Eq, FromXml, PartialEq)]
175#[xml(forward)]
176pub enum RecipientType {
177    /// Data for the `<other>` tag
178    Other(Other),
179    /// Data for the `<ours>` tag
180    Ours(Ours),
181    /// Data for the `<public>` tag
182    Public(Public),
183    /// Data for the `<same>` tag
184    Same(Same),
185    /// Data for the `<unrelated>` tag
186    Unrelated(Unrelated),
187}
188
189#[derive(Debug, Eq, FromXml, PartialEq)]
190#[xml(rename = "ours", ns(EPP_XMLNS))]
191pub struct Ours;
192
193#[derive(Debug, Eq, FromXml, PartialEq)]
194#[xml(rename = "public", ns(EPP_XMLNS))]
195pub struct Public;
196
197#[derive(Debug, Eq, FromXml, PartialEq)]
198#[xml(rename = "unrelated", ns(EPP_XMLNS))]
199pub struct Unrelated;
200
201#[derive(Debug, Eq, FromXml, PartialEq)]
202#[xml(rename = "same", ns(EPP_XMLNS))]
203pub struct Same;
204
205/// Type corresponding to `<recipeint>` in the EPP greeting XML
206#[derive(Debug, Eq, FromXml, PartialEq)]
207#[xml(rename = "recipient", ns(EPP_XMLNS))]
208pub struct Recipient {
209    pub recipient: Vec<RecipientType>,
210}
211
212/// Type corresponding to `<business>` in the EPP greeting XML
213#[derive(Debug, Eq, FromXml, PartialEq)]
214#[xml(rename = "business", ns(EPP_XMLNS))]
215pub struct Business;
216
217/// Type corresponding to `<indefinite>` in the EPP greeting XML
218#[derive(Debug, Eq, FromXml, PartialEq)]
219#[xml(rename = "indefinite", ns(EPP_XMLNS))]
220pub struct Indefinite;
221
222/// Type corresponding to `<legal>` in the EPP greeting XML
223#[derive(Debug, Eq, FromXml, PartialEq)]
224#[xml(rename = "legal", ns(EPP_XMLNS))]
225pub struct Legal;
226
227/// Type corresponding to `<none>` in the EPP greeting XML
228#[derive(Debug, Eq, FromXml, PartialEq)]
229#[xml(rename = "none", ns(EPP_XMLNS))]
230pub struct No;
231
232/// Type corresponding to `<stated>` in the EPP greeting XML
233#[derive(Debug, Eq, FromXml, PartialEq)]
234#[xml(rename = "stated", ns(EPP_XMLNS))]
235pub struct Stated;
236
237/// Type corresponding to possible `<retention>` type values
238#[derive(Debug, Eq, FromXml, PartialEq)]
239#[xml(forward, rename = "retention", ns(EPP_XMLNS))]
240pub enum RetentionType {
241    /// Data for the `<business>` tag
242    Business(Business),
243    /// Data for the `<indefinite>` tag
244    Indefinite(Indefinite),
245    /// Data for the `<legal>` tag
246    Legal(Legal),
247    /// Data for the `<none>` tag
248    None(No),
249    /// Data for the `<stated>` tag
250    Stated(Stated),
251}
252
253#[derive(Debug, Eq, FromXml, PartialEq)]
254#[xml(rename = "retention", ns(EPP_XMLNS))]
255pub struct Retention {
256    inner: RetentionType,
257}
258
259/// Type corresponding to `<statement>` in the EPP greeting XML (pending more compliant implementation)
260#[derive(Debug, Eq, FromXml, PartialEq)]
261#[xml(rename = "statement", ns(EPP_XMLNS))]
262pub struct Statement {
263    /// Data for the `<purpose>` tag
264    pub purpose: Purpose,
265    /// Data for the `<recipient>` tag
266    pub recipient: Recipient,
267    /// Data for the `<retention>` tag
268    pub retention: Retention,
269}
270
271/// Type corresponding to `<absolute>` value in the EPP greeting XML
272#[derive(Debug, Eq, FromXml, PartialEq)]
273#[xml(rename = "absolute", ns(EPP_XMLNS))]
274pub struct Absolute(String);
275
276/// Type corresponding to `<relative>` value in the EPP greeting XML
277#[derive(Debug, Eq, FromXml, PartialEq)]
278#[xml(rename = "relative", ns(EPP_XMLNS))]
279pub struct Relative(String);
280
281/// Type corresponding to possible `<expiry>` type values
282#[derive(Debug, Eq, FromXml, PartialEq)]
283#[xml(forward)]
284pub enum ExpiryType {
285    /// Data for the `<absolute>` tag
286    Absolute(Absolute),
287    /// Data for the `<relative>` tag
288    Relative(Relative),
289}
290
291/// Type corresponding to possible `<expiry>` type values
292#[derive(Debug, Eq, FromXml, PartialEq)]
293#[xml(rename = "expiry", ns(EPP_XMLNS))]
294pub struct Expiry {
295    inner: ExpiryType,
296}
297
298/// Type corresponding to `<dcp>` in the EPP greeting XML
299#[derive(Debug, Eq, FromXml, PartialEq)]
300#[xml(rename = "dcp", ns(EPP_XMLNS))]
301pub struct Dcp {
302    /// Data for the `<access>` tag
303    pub access: Access,
304    /// Data for the `<statement>` tags
305    pub statement: Vec<Statement>,
306    /// Data for the `<expiry>` tag
307    pub expiry: Option<Expiry>,
308}
309
310/// Type corresponding to the `<greeting>` tag in the EPP greeting XML
311#[derive(Debug, Eq, FromXml, PartialEq)]
312#[xml(ns(EPP_XMLNS), rename = "greeting", rename_all = "lowercase")]
313pub struct Greeting {
314    /// The service ID
315    #[xml(rename = "svID")]
316    pub service_id: String,
317    /// The date from the EPP server
318    #[xml(rename = "svDate")]
319    pub service_date: DateTime<Utc>,
320    /// Data under the `<svcMenu>` element
321    pub svc_menu: ServiceMenu,
322    /// Data under the `<dcp>` element
323    pub dcp: Dcp,
324}
325
326#[cfg(test)]
327mod tests {
328    use chrono::{TimeZone, Utc};
329
330    use super::{ExpiryType, Greeting, Hello, Relative};
331    use crate::tests::get_xml;
332    use crate::xml;
333
334    #[test]
335    fn hello() {
336        let xml = get_xml("request/hello.xml").unwrap();
337        let serialized = xml::serialize(Hello).unwrap();
338
339        assert_eq!(xml, serialized);
340    }
341
342    #[test]
343    fn greeting() {
344        let xml = get_xml("response/greeting.xml").unwrap();
345        let object = xml::deserialize::<Greeting>(xml.as_str()).unwrap();
346
347        assert_eq!(object.service_id, "ISPAPI EPP Server");
348        assert_eq!(
349            object.service_date,
350            Utc.with_ymd_and_hms(2021, 7, 25, 14, 51, 17).unwrap()
351        );
352        assert_eq!(object.svc_menu.version, "1.0");
353        assert_eq!(object.svc_menu.langs.first().unwrap(), "en");
354        assert_eq!(object.svc_menu.services.obj_uris.len(), 4);
355        assert_eq!(object.svc_menu.services.svc_ext.unwrap().ext_uris.len(), 5);
356        assert_eq!(object.dcp.statement.len(), 2);
357        assert_eq!(
358            object.dcp.expiry.unwrap().inner,
359            ExpiryType::Relative(Relative("P1M".into()))
360        );
361    }
362
363    #[test]
364    fn greeting_multi_lang() {
365        let xml = get_xml("response/greeting_multi_lang.xml").unwrap();
366        let object = xml::deserialize::<Greeting>(xml.as_str()).unwrap();
367
368        assert_eq!(object.service_id, "ISPAPI EPP Server");
369        assert_eq!(
370            object.service_date,
371            Utc.with_ymd_and_hms(2021, 7, 25, 14, 51, 17).unwrap()
372        );
373        assert_eq!(object.svc_menu.version, "1.0");
374        assert_eq!(object.svc_menu.langs.first().unwrap(), "en");
375        assert_eq!(object.svc_menu.langs.get(1).unwrap(), "fr");
376        assert_eq!(object.svc_menu.services.obj_uris.len(), 4);
377        assert_eq!(object.svc_menu.services.svc_ext.unwrap().ext_uris.len(), 5);
378        assert_eq!(object.dcp.statement.len(), 2);
379        assert_eq!(
380            object.dcp.expiry.unwrap().inner,
381            ExpiryType::Relative(Relative("P1M".into()))
382        );
383    }
384
385    #[test]
386    fn greeting_no_access() {
387        let xml = get_xml("response/greeting_dcp_none.xml").unwrap();
388        let object = xml::deserialize::<Greeting>(xml.as_str()).unwrap();
389
390        assert_eq!(object.service_id, "ISPAPI EPP Server");
391        assert_eq!(
392            object.service_date,
393            Utc.with_ymd_and_hms(2021, 7, 25, 14, 51, 17).unwrap()
394        );
395        assert_eq!(object.svc_menu.version, "1.0");
396        assert_eq!(object.svc_menu.langs.first().unwrap(), "en");
397        assert_eq!(object.svc_menu.services.obj_uris.len(), 4);
398        assert_eq!(object.svc_menu.services.svc_ext.unwrap().ext_uris.len(), 5);
399        assert_eq!(object.dcp.statement.len(), 2);
400        assert!(matches!(
401            object.dcp.access.inner,
402            super::AccessType::NoAccess(_)
403        ));
404        assert_eq!(
405            object.dcp.expiry.unwrap().inner,
406            ExpiryType::Relative(Relative("P1M".into()))
407        );
408    }
409}