Debian Codegraph @

src/response/autnum.rs

raw
1 //! RDAP Autonomous System Number.
2 use std::collections::HashSet;
3
4 use crate::prelude::ContentExtensions;
5
6 use {
7 crate::prelude::{Common, Extension, ObjectCommon},
8 serde::{Deserialize, Serialize},
9 };
10
11 use super::{
12 CommonFields, Entity, Event, GetSelfLink, Notice, Numberish, ObjectCommonFields, Port43,
13 Remark, SelfLink, Stringish, ToChild, ToResponse, to_opt_vec, types::Link,
14 };
15
16 /// Represents an RDAP [autnum](https://rdap.rcode3.com/protocol/object_classes.html#autnum) object response.
17 ///
18 /// Using the builder to construct this structure is recommended
19 /// as it will fill-in many of the mandatory fields.
20 /// The following is an example.
21 ///
22 /// ```rust
23 /// use icann_rdap_common::prelude::*;
24 ///
25 /// let autnum = Autnum::response_obj()
26 /// .autnum_range(700..710) // the range of autnums
27 /// .handle("AS700-1")
28 /// .status("active")
29 /// .build();
30 /// let c = serde_json::to_string_pretty(&autnum).unwrap();
31 /// eprintln!("{c}");
32 /// ```
33 /// This will produce the following.
34 ///
35 /// ```norust
36 /// {
37 /// "rdapConformance": [
38 /// "rdap_level_0"
39 /// ],
40 /// "objectClassName": "autnum",
41 /// "handle": "AS700-1",
42 /// "status": [
43 /// "active"
44 /// ],
45 /// "startAutnum": 700,
46 /// "endAutnum": 710
47 /// }
48 /// ```
49 ///
50 /// Use the getter functions to get the autnum data.
51 /// See [CommonFields] and [ObjectCommonFields] for common getter functions.
52 /// ```rust
53 /// # use icann_rdap_common::prelude::*;
54 /// # let autnum = Autnum::builder()
55 /// # .autnum_range(700..710) // the range of autnums
56 /// # .handle("AS700-1")
57 /// # .build();
58 /// let start_autnum = autnum.start_autnum();
59 /// let end_autnum = autnum.end_autnum();
60 /// let handle = autnum.handle();
61 /// ```
62 #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
63 pub struct Autnum {
64 #[serde(flatten)]
65 pub common: Common,
66
67 #[serde(flatten)]
68 pub object_common: ObjectCommon,
69
70 #[serde(rename = "startAutnum")]
71 #[serde(skip_serializing_if = "Option::is_none")]
72 pub start_autnum: Option<Numberish<u32>>,
73
74 #[serde(rename = "endAutnum")]
75 #[serde(skip_serializing_if = "Option::is_none")]
76 pub end_autnum: Option<Numberish<u32>>,
77
78 #[serde(skip_serializing_if = "Option::is_none")]
79 pub name: Option<Stringish>,
80
81 #[serde(rename = "type")]
82 #[serde(skip_serializing_if = "Option::is_none")]
83 pub autnum_type: Option<Stringish>,
84
85 #[serde(skip_serializing_if = "Option::is_none")]
86 pub country: Option<Stringish>,
87 }
88
89 #[buildstructor::buildstructor]
90 impl Autnum {
91 /// Builds a basic autnum object for use with embedding in other objects.
92 ///
93 /// ```rust
94 /// use icann_rdap_common::prelude::*;
95 ///
96 /// let autnum = Autnum::builder()
97 /// .autnum_range(700..710) //required for this builder
98 /// .handle("AS700-1")
99 /// .status("active")
100 /// .build();
101 /// ```
102 #[builder(visibility = "pub")]
103 fn new(
104 autnum_range: std::ops::Range<u32>,
105 handle: Option<String>,
106 remarks: Vec<Remark>,
107 links: Vec<Link>,
108 events: Vec<Event>,
109 statuses: Vec<String>,
110 port_43: Option<Port43>,
111 entities: Vec<Entity>,
112 country: Option<String>,
113 autnum_type: Option<String>,
114 name: Option<String>,
115 redacted: Option<Vec<crate::response::redacted::Redacted>>,
116 ) -> Self {
117 Self {
118 common: Common::builder().build(),
119 object_common: ObjectCommon::autnum()
120 .and_handle(handle.map(|s| s.into()) as Option<Stringish>)
121 .and_remarks(to_opt_vec(remarks))
122 .and_links(to_opt_vec(links))
123 .and_events(to_opt_vec(events))
124 .status(statuses)
125 .and_port_43(port_43)
126 .and_entities(to_opt_vec(entities))
127 .and_redacted(redacted)
128 .build(),
129 start_autnum: Some(Numberish::<u32>::from(autnum_range.start)),
130 end_autnum: Some(Numberish::<u32>::from(autnum_range.end)),
131 name: name.map(|s| s.into()),
132 autnum_type: autnum_type.map(|s| s.into()),
133 country: country.map(|s| s.into()),
134 }
135 }
136
137 /// Builds an autnum object as a response.
138 ///
139 /// ```rust
140 /// use icann_rdap_common::prelude::*;
141 ///
142 /// let autnum = Autnum::response_obj()
143 /// .autnum_range(700..710) //required for this builder
144 /// .handle("AS700-1")
145 /// .status("active")
146 /// .extension(ExtensionId::NroRdapProfile0.as_ref())
147 /// .notice(Notice::builder().title("test").build())
148 /// .build();
149 /// ```
150 #[builder(entry = "response_obj", visibility = "pub")]
151 fn new_response_obj(
152 autnum_range: std::ops::Range<u32>,
153 handle: Option<String>,
154 remarks: Vec<Remark>,
155 links: Vec<Link>,
156 events: Vec<Event>,
157 statuses: Vec<String>,
158 port_43: Option<Port43>,
159 entities: Vec<Entity>,
160 notices: Vec<Notice>,
161 country: Option<String>,
162 autnum_type: Option<String>,
163 name: Option<String>,
164 extensions: Vec<Extension>,
165 redacted: Option<Vec<crate::response::redacted::Redacted>>,
166 ) -> Self {
167 let common = Common::level0()
168 .extensions(extensions)
169 .and_notices(to_opt_vec(notices))
170 .build();
171 let mut autnum = Autnum::builder()
172 .autnum_range(autnum_range)
173 .and_handle(handle)
174 .remarks(remarks)
175 .links(links)
176 .events(events)
177 .statuses(statuses)
178 .and_port_43(port_43)
179 .entities(entities)
180 .and_country(country)
181 .and_autnum_type(autnum_type)
182 .and_name(name)
183 .and_redacted(redacted)
184 .build();
185 autnum.common = common;
186 autnum
187 }
188
189 /// Returns the starting ASN of the range.
190 pub fn start_autnum(&self) -> Option<u32> {
191 self.start_autnum.as_ref().and_then(|n| n.as_u32())
192 }
193
194 /// Returns the ending ASN of the range.
195 pub fn end_autnum(&self) -> Option<u32> {
196 self.end_autnum.as_ref().and_then(|n| n.as_u32())
197 }
198
199 /// Returns the name of the ASN.
200 pub fn name(&self) -> Option<&str> {
201 self.name.as_deref()
202 }
203
204 /// Returns the type of the ASN.
205 pub fn autnum_type(&self) -> Option<&str> {
206 self.autnum_type.as_deref()
207 }
208
209 /// Returns the country of the ASN.
210 pub fn country(&self) -> Option<&str> {
211 self.country.as_deref()
212 }
213 }
214
215 impl ToResponse for Autnum {
216 fn to_response(self) -> super::RdapResponse {
217 super::RdapResponse::Autnum(Box::new(self))
218 }
219 }
220
221 impl GetSelfLink for Autnum {
222 fn self_link(&self) -> Option<&Link> {
223 self.object_common.self_link()
224 }
225 }
226
227 impl SelfLink for Autnum {
228 fn with_self_link(mut self, link: Link) -> Self {
229 self.object_common = self.object_common.with_self_link(link);
230 self
231 }
232 }
233
234 impl ToChild for Autnum {
235 fn to_child(mut self) -> Self {
236 self.common = Common {
237 rdap_conformance: None,
238 notices: None,
239 };
240 self
241 }
242 }
243
244 impl CommonFields for Autnum {
245 fn common(&self) -> &Common {
246 &self.common
247 }
248 }
249
250 impl ObjectCommonFields for Autnum {
251 fn object_common(&self) -> &ObjectCommon {
252 &self.object_common
253 }
254 }
255
256 impl ContentExtensions for Autnum {
257 fn content_extensions(&self) -> std::collections::HashSet<super::ExtensionId> {
258 let mut exts = HashSet::new();
259 exts.extend(self.common().content_extensions());
260 exts.extend(self.object_common().content_extensions());
261 exts
262 }
263 }
264
265 #[cfg(test)]
266 #[allow(non_snake_case)]
267 mod tests {
268 use super::Autnum;
269
270 #[test]
271 fn GIVEN_autnum_WHEN_deserialize_THEN_success() {
272 // GIVEN
273 let expected = r#"
274 {
275 "objectClassName" : "autnum",
276 "handle" : "XXXX-RIR",
277 "startAutnum" : 65536,
278 "endAutnum" : 65541,
279 "name": "AS-RTR-1",
280 "type" : "DIRECT ALLOCATION",
281 "status" : [ "active" ],
282 "country": "AU",
283 "remarks" :
284 [
285 {
286 "description" :
287 [
288 "She sells sea shells down by the sea shore.",
289 "Originally written by Terry Sullivan."
290 ]
291 }
292 ],
293 "links" :
294 [
295 {
296 "value" : "https://example.net/autnum/65537",
297 "rel" : "self",
298 "href" : "https://example.net/autnum/65537",
299 "type" : "application/rdap+json"
300 }
301 ],
302 "events" :
303
304 [
305 {
306 "eventAction" : "registration",
307 "eventDate" : "1990-12-31T23:59:59Z"
308 },
309 {
310 "eventAction" : "last changed",
311 "eventDate" : "1991-12-31T23:59:59Z"
312 }
313 ],
314 "entities" :
315 [
316 {
317 "objectClassName" : "entity",
318 "handle" : "XXXX",
319 "vcardArray":[
320 "vcard",
321 [
322 ["version", {}, "text", "4.0"],
323 ["fn", {}, "text", "Joe User"],
324 ["kind", {}, "text", "individual"],
325 ["lang", {
326 "pref":"1"
327 }, "language-tag", "fr"],
328 ["lang", {
329 "pref":"2"
330 }, "language-tag", "en"],
331 ["org", {
332 "type":"work"
333 }, "text", "Example"],
334 ["title", {}, "text", "Research Scientist"],
335 ["role", {}, "text", "Project Lead"],
336 ["adr",
337 { "type":"work" },
338 "text",
339 [
340 "",
341 "Suite 1234",
342 "4321 Rue Somewhere",
343 "Quebec",
344 "QC",
345 "G1V 2M2",
346 "Canada"
347 ]
348 ],
349 ["tel",
350 { "type":["work", "voice"], "pref":"1" },
351 "uri", "tel:+1-555-555-1234;ext=102"
352 ],
353 ["email",
354 { "type":"work" },
355 "text", "[email protected]"
356 ]
357 ]
358 ],
359 "roles" : [ "registrant" ],
360 "remarks" :
361 [
362 {
363 "description" :
364 [
365 "She sells sea shells down by the sea shore.",
366 "Originally written by Terry Sullivan."
367 ]
368 }
369 ],
370 "links" :
371 [
372 {
373 "value" : "https://example.net/entity/XXXX",
374 "rel" : "self",
375 "href" : "https://example.net/entity/XXXX",
376 "type" : "application/rdap+json"
377 }
378 ],
379 "events" :
380 [
381 {
382 "eventAction" : "registration",
383 "eventDate" : "1990-12-31T23:59:59Z"
384 },
385 {
386 "eventAction" : "last changed",
387 "eventDate" : "1991-12-31T23:59:59Z"
388 }
389 ]
390 }
391 ]
392 }
393 "#;
394
395 // WHEN
396 let actual = serde_json::from_str::<Autnum>(expected);
397
398 // THEN
399 let actual = actual.unwrap();
400 assert_eq!(actual.object_common.object_class_name, "autnum");
401 assert!(actual.object_common.handle.is_some());
402 assert!(actual.start_autnum.is_some());
403 assert!(actual.end_autnum.is_some());
404 assert!(actual.name.is_some());
405 assert!(actual.autnum_type.is_some());
406 assert!(actual.object_common.status.is_some());
407 assert!(actual.country.is_some());
408 assert!(actual.object_common.remarks.is_some());
409 assert!(actual.object_common.links.is_some());
410 assert!(actual.object_common.events.is_some());
411 assert!(actual.object_common.entities.is_some());
412 }
413 }