| 1 |
use std::{collections::HashSet, str::FromStr}; |
| 2 |
|
| 3 |
use serde::{Deserialize, Serialize}; |
| 4 |
|
| 5 |
use crate::prelude::ContentExtensions; |
| 6 |
|
| 7 |
use super::{Extension, ExtensionId, Notice, Notices, RdapConformance}; |
| 8 |
|
| 9 |
/// Holds those types that are common in all responses. |
| 10 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 11 |
pub struct Common { |
| 12 |
#[serde(rename = "rdapConformance")] |
| 13 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 14 |
pub rdap_conformance: Option<RdapConformance>, |
| 15 |
|
| 16 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 17 |
pub notices: Option<Notices>, |
| 18 |
} |
| 19 |
|
| 20 |
#[buildstructor::buildstructor] |
| 21 |
impl Common { |
| 22 |
#[builder(entry = "level0", visibility = "pub(crate)")] |
| 23 |
fn new_level0(mut extensions: Vec<Extension>, notices: Option<Vec<Notice>>) -> Self { |
| 24 |
let mut standard_extensions = vec![ExtensionId::RdapLevel0.to_extension()]; |
| 25 |
extensions.append(&mut standard_extensions); |
| 26 |
Self { |
| 27 |
rdap_conformance: Some(extensions), |
| 28 |
notices, |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
#[builder(visibility = "pub(crate)")] |
| 33 |
fn new() -> Self { |
| 34 |
Self { |
| 35 |
rdap_conformance: None, |
| 36 |
notices: None, |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/// Convenience methods for fields in [Common]. |
| 42 |
pub trait CommonFields { |
| 43 |
/// Getter for [Common]. |
| 44 |
fn common(&self) -> &Common; |
| 45 |
|
| 46 |
/// Getter for the list of RDAP extensions. |
| 47 |
/// |
| 48 |
/// In valid RDAP, this only appears on the top most object. |
| 49 |
fn extensions(&self) -> &[Extension] { |
| 50 |
self.common() |
| 51 |
.rdap_conformance |
| 52 |
.as_deref() |
| 53 |
.unwrap_or_default() |
| 54 |
} |
| 55 |
|
| 56 |
/// Getter for the Notices. |
| 57 |
/// |
| 58 |
/// In valid RDAP, this only appears on the top most object. |
| 59 |
fn notices(&self) -> &[Notice] { |
| 60 |
self.common().notices.as_deref().unwrap_or_default() |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl ContentExtensions for Common { |
| 65 |
fn content_extensions(&self) -> std::collections::HashSet<super::ExtensionId> { |
| 66 |
let mut exts = HashSet::new(); |
| 67 |
self.notices |
| 68 |
.as_deref() |
| 69 |
.unwrap_or_default() |
| 70 |
.iter() |
| 71 |
.for_each(|remark| { |
| 72 |
exts.extend(remark.content_extensions()); |
| 73 |
}); |
| 74 |
self.rdap_conformance |
| 75 |
.as_deref() |
| 76 |
.unwrap_or_default() |
| 77 |
.iter() |
| 78 |
.for_each(|e| { |
| 79 |
if let Ok(ext_id) = ExtensionId::from_str(e) { |
| 80 |
exts.insert(ext_id); |
| 81 |
} |
| 82 |
}); |
| 83 |
exts |
| 84 |
} |
| 85 |
} |