| 1 |
use std::collections::HashSet; |
| 2 |
|
| 3 |
use serde::{Deserialize, Serialize}; |
| 4 |
|
| 5 |
use crate::prelude::ContentExtensions; |
| 6 |
|
| 7 |
use super::{ |
| 8 |
Entity, Event, Events, Link, Links, Port43, Remark, Remarks, Stringish, VectorStringish, |
| 9 |
redacted::Redacted, to_opt_vectorstringish, |
| 10 |
}; |
| 11 |
|
| 12 |
/// Holds those types that are common in all object classes. |
| 13 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 14 |
pub struct ObjectCommon { |
| 15 |
#[serde(rename = "objectClassName")] |
| 16 |
pub object_class_name: String, |
| 17 |
|
| 18 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 19 |
pub handle: Option<Stringish>, |
| 20 |
|
| 21 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 22 |
pub remarks: Option<Remarks>, |
| 23 |
|
| 24 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 25 |
pub links: Option<Links>, |
| 26 |
|
| 27 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 28 |
pub events: Option<Events>, |
| 29 |
|
| 30 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 31 |
pub status: Option<VectorStringish>, |
| 32 |
|
| 33 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 34 |
#[serde(rename = "port43")] |
| 35 |
pub port_43: Option<Port43>, |
| 36 |
|
| 37 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 38 |
pub entities: Option<Vec<Entity>>, |
| 39 |
|
| 40 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 41 |
pub redacted: Option<Vec<Redacted>>, |
| 42 |
} |
| 43 |
|
| 44 |
#[buildstructor::buildstructor] |
| 45 |
impl ObjectCommon { |
| 46 |
/// Builds [ObjectCommon] for a [crate::response::domain::Domain]. |
| 47 |
#[builder(entry = "domain", visibility = "pub(crate)")] |
| 48 |
fn new_domain( |
| 49 |
handle: Option<String>, |
| 50 |
remarks: Option<Remarks>, |
| 51 |
links: Option<Links>, |
| 52 |
events: Option<Events>, |
| 53 |
status: Option<Vec<String>>, |
| 54 |
port_43: Option<Port43>, |
| 55 |
entities: Option<Vec<Entity>>, |
| 56 |
redacted: Option<Vec<Redacted>>, |
| 57 |
) -> Self { |
| 58 |
Self { |
| 59 |
object_class_name: "domain".to_string(), |
| 60 |
handle: handle.map(|s| s.into()), |
| 61 |
remarks, |
| 62 |
links, |
| 63 |
events, |
| 64 |
status: to_opt_vectorstringish(status.unwrap_or_default()), |
| 65 |
port_43, |
| 66 |
entities, |
| 67 |
redacted, |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/// Builds [ObjectCommon] for a [crate::response::network::Network]. |
| 72 |
#[builder(entry = "ip_network", visibility = "pub(crate)")] |
| 73 |
fn new_ip_network( |
| 74 |
handle: Option<String>, |
| 75 |
remarks: Option<Remarks>, |
| 76 |
links: Option<Links>, |
| 77 |
events: Option<Events>, |
| 78 |
status: Option<Vec<String>>, |
| 79 |
port_43: Option<Port43>, |
| 80 |
entities: Option<Vec<Entity>>, |
| 81 |
redacted: Option<Vec<Redacted>>, |
| 82 |
) -> Self { |
| 83 |
Self { |
| 84 |
object_class_name: "ip network".to_string(), |
| 85 |
handle: handle.map(|s| s.into()), |
| 86 |
remarks, |
| 87 |
links, |
| 88 |
events, |
| 89 |
status: to_opt_vectorstringish(status.unwrap_or_default()), |
| 90 |
port_43, |
| 91 |
entities, |
| 92 |
redacted, |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/// Builds an [ObjectCommon] for an [crate::response::autnum::Autnum]. |
| 97 |
#[builder(entry = "autnum", visibility = "pub(crate)")] |
| 98 |
fn new_autnum( |
| 99 |
handle: Option<String>, |
| 100 |
remarks: Option<Remarks>, |
| 101 |
links: Option<Links>, |
| 102 |
events: Option<Events>, |
| 103 |
status: Option<Vec<String>>, |
| 104 |
port_43: Option<Port43>, |
| 105 |
entities: Option<Vec<Entity>>, |
| 106 |
redacted: Option<Vec<Redacted>>, |
| 107 |
) -> Self { |
| 108 |
Self { |
| 109 |
object_class_name: "autnum".to_string(), |
| 110 |
handle: handle.map(|s| s.into()), |
| 111 |
remarks, |
| 112 |
links, |
| 113 |
events, |
| 114 |
status: to_opt_vectorstringish(status.unwrap_or_default()), |
| 115 |
port_43, |
| 116 |
entities, |
| 117 |
redacted, |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/// Builds an [ObjectCommon] for a [crate::response::nameserver::Nameserver]. |
| 122 |
#[builder(entry = "nameserver", visibility = "pub(crate)")] |
| 123 |
fn new_nameserver( |
| 124 |
handle: Option<String>, |
| 125 |
remarks: Option<Remarks>, |
| 126 |
links: Option<Links>, |
| 127 |
events: Option<Events>, |
| 128 |
status: Option<Vec<String>>, |
| 129 |
port_43: Option<Port43>, |
| 130 |
entities: Option<Vec<Entity>>, |
| 131 |
redacted: Option<Vec<Redacted>>, |
| 132 |
) -> Self { |
| 133 |
Self { |
| 134 |
object_class_name: "nameserver".to_string(), |
| 135 |
handle: handle.map(|s| s.into()), |
| 136 |
remarks, |
| 137 |
links, |
| 138 |
events, |
| 139 |
status: to_opt_vectorstringish(status.unwrap_or_default()), |
| 140 |
port_43, |
| 141 |
entities, |
| 142 |
redacted, |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/// Builds an [ObjectCommon] for an [crate::response::entity::Entity]. |
| 147 |
#[builder(entry = "entity", visibility = "pub(crate)")] |
| 148 |
fn new_entity( |
| 149 |
handle: Option<String>, |
| 150 |
remarks: Option<Remarks>, |
| 151 |
links: Option<Links>, |
| 152 |
events: Option<Events>, |
| 153 |
status: Option<Vec<String>>, |
| 154 |
port_43: Option<Port43>, |
| 155 |
entities: Option<Vec<Entity>>, |
| 156 |
redacted: Option<Vec<Redacted>>, |
| 157 |
) -> Self { |
| 158 |
Self { |
| 159 |
object_class_name: "entity".to_string(), |
| 160 |
handle: handle.map(|s| s.into()), |
| 161 |
remarks, |
| 162 |
links, |
| 163 |
events, |
| 164 |
status: to_opt_vectorstringish(status.unwrap_or_default()), |
| 165 |
port_43, |
| 166 |
entities, |
| 167 |
redacted, |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/// This will remove all other self links and place the provided link |
| 172 |
/// into the Links. This method will also set the "rel" attribute |
| 173 |
/// to "self" on the provided link. |
| 174 |
pub fn with_self_link(mut self, mut link: Link) -> Self { |
| 175 |
link.rel = Some("self".to_string()); |
| 176 |
if let Some(links) = self.links { |
| 177 |
let mut new_links = links |
| 178 |
.into_iter() |
| 179 |
.filter(|link| !link.is_relation("self")) |
| 180 |
.collect::<Vec<Link>>(); |
| 181 |
new_links.push(link); |
| 182 |
self.links = Some(new_links); |
| 183 |
} else { |
| 184 |
self.links = Some(vec![link]); |
| 185 |
} |
| 186 |
self |
| 187 |
} |
| 188 |
|
| 189 |
/// Get the link with a `rel` of "self". |
| 190 |
pub fn self_link(&self) -> Option<&Link> { |
| 191 |
if let Some(links) = &self.links { |
| 192 |
links.iter().find(|link| link.is_relation("self")) |
| 193 |
} else { |
| 194 |
None |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/// Gets the first entity by the given role. |
| 199 |
/// |
| 200 |
/// Use [crate::response::EntityRole] to get registered role names. |
| 201 |
pub fn entity_by_role(&self, role: &str) -> Option<&Entity> { |
| 202 |
self.entities |
| 203 |
.as_deref() |
| 204 |
.unwrap_or_default() |
| 205 |
.iter() |
| 206 |
.find(|e| e.roles().iter().any(|r| r.eq(role))) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/// Convenience methods for fields in [ObjectCommon]. |
| 211 |
pub trait ObjectCommonFields { |
| 212 |
/// Getter for [ObjectCommon]. |
| 213 |
fn object_common(&self) -> &ObjectCommon; |
| 214 |
|
| 215 |
/// Returns the object class name. |
| 216 |
fn object_class_name(&self) -> &str { |
| 217 |
&self.object_common().object_class_name |
| 218 |
} |
| 219 |
|
| 220 |
/// Returns the handle, if present. |
| 221 |
fn handle(&self) -> Option<&str> { |
| 222 |
self.object_common().handle.as_deref() |
| 223 |
} |
| 224 |
|
| 225 |
/// Returns the port 43 information, if present. |
| 226 |
fn port_43(&self) -> Option<&Port43> { |
| 227 |
self.object_common().port_43.as_ref() |
| 228 |
} |
| 229 |
|
| 230 |
/// Getter for list of [Remark]s. |
| 231 |
fn remarks(&self) -> &[Remark] { |
| 232 |
self.object_common().remarks.as_deref().unwrap_or_default() |
| 233 |
} |
| 234 |
|
| 235 |
/// Getter for list of [Link]s. |
| 236 |
fn links(&self) -> &[Link] { |
| 237 |
self.object_common().links.as_deref().unwrap_or_default() |
| 238 |
} |
| 239 |
|
| 240 |
/// Getter for list of [Event]s. |
| 241 |
fn events(&self) -> &[Event] { |
| 242 |
self.object_common().events.as_deref().unwrap_or_default() |
| 243 |
} |
| 244 |
|
| 245 |
/// Getter for status. |
| 246 |
fn status(&self) -> &[String] { |
| 247 |
self.object_common() |
| 248 |
.status |
| 249 |
.as_ref() |
| 250 |
.map(|v| v.vec().as_ref()) |
| 251 |
.unwrap_or_default() |
| 252 |
} |
| 253 |
|
| 254 |
/// Getter for list of [Entity]. |
| 255 |
fn entities(&self) -> &[Entity] { |
| 256 |
self.object_common().entities.as_deref().unwrap_or_default() |
| 257 |
} |
| 258 |
|
| 259 |
/// Gets the first entity by the given role. |
| 260 |
/// |
| 261 |
/// See [ObjectCommon::entity_by_role]. |
| 262 |
fn entity_by_role(&self, role: &str) -> Option<&Entity> { |
| 263 |
self.object_common().entity_by_role(role) |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
impl ContentExtensions for ObjectCommon { |
| 268 |
fn content_extensions(&self) -> std::collections::HashSet<super::ExtensionId> { |
| 269 |
let mut exts = HashSet::new(); |
| 270 |
self.remarks |
| 271 |
.as_deref() |
| 272 |
.unwrap_or_default() |
| 273 |
.iter() |
| 274 |
.for_each(|remark| { |
| 275 |
exts.extend(remark.content_extensions()); |
| 276 |
}); |
| 277 |
if self.redacted.is_some() { |
| 278 |
exts.insert(super::ExtensionId::Redacted); |
| 279 |
} |
| 280 |
exts |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
#[cfg(test)] |
| 285 |
mod tests { |
| 286 |
use super::*; |
| 287 |
|
| 288 |
fn create_test_entity(handle: &str, roles: Vec<&str>) -> Entity { |
| 289 |
Entity::builder() |
| 290 |
.handle(handle) |
| 291 |
.roles(roles.into_iter().map(|s| s.to_string()).collect()) |
| 292 |
.build() |
| 293 |
} |
| 294 |
|
| 295 |
#[test] |
| 296 |
fn test_get_entity_by_role_found() { |
| 297 |
// GIVEN |
| 298 |
let entity1 = create_test_entity("entity1", vec!["registrant", "administrative"]); |
| 299 |
let entity2 = create_test_entity("entity2", vec!["registrar", "technical"]); |
| 300 |
let entity3 = create_test_entity("entity3", vec!["abuse"]); |
| 301 |
|
| 302 |
let obj_common = ObjectCommon::entity() |
| 303 |
.and_entities(Some(vec![entity1, entity2, entity3])) |
| 304 |
.build(); |
| 305 |
|
| 306 |
// WHEN |
| 307 |
let result = obj_common.entity_by_role("registrar"); |
| 308 |
|
| 309 |
// THEN |
| 310 |
assert!(result.is_some()); |
| 311 |
assert_eq!(result.unwrap().handle(), Some("entity2")); |
| 312 |
} |
| 313 |
|
| 314 |
#[test] |
| 315 |
fn test_get_entity_by_role_not_found() { |
| 316 |
// GIVEN |
| 317 |
let entity1 = create_test_entity("entity1", vec!["registrant", "administrative"]); |
| 318 |
let entity2 = create_test_entity("entity2", vec!["registrar", "technical"]); |
| 319 |
|
| 320 |
let obj_common = ObjectCommon::entity() |
| 321 |
.and_entities(Some(vec![entity1, entity2])) |
| 322 |
.build(); |
| 323 |
|
| 324 |
// WHEN |
| 325 |
let result = obj_common.entity_by_role("abuse"); |
| 326 |
|
| 327 |
// THEN |
| 328 |
assert!(result.is_none()); |
| 329 |
} |
| 330 |
|
| 331 |
#[test] |
| 332 |
fn test_get_entity_by_role_no_entities() { |
| 333 |
// GIVEN |
| 334 |
let obj_common = ObjectCommon::entity().build(); |
| 335 |
|
| 336 |
// WHEN |
| 337 |
let result = obj_common.entity_by_role("registrant"); |
| 338 |
|
| 339 |
// THEN |
| 340 |
assert!(result.is_none()); |
| 341 |
} |
| 342 |
|
| 343 |
#[test] |
| 344 |
fn test_get_entity_by_role_empty_entities() { |
| 345 |
// GIVEN |
| 346 |
let obj_common = ObjectCommon::entity().and_entities(Some(vec![])).build(); |
| 347 |
|
| 348 |
// WHEN |
| 349 |
let result = obj_common.entity_by_role("registrant"); |
| 350 |
|
| 351 |
// THEN |
| 352 |
assert!(result.is_none()); |
| 353 |
} |
| 354 |
|
| 355 |
#[test] |
| 356 |
fn test_get_entity_by_role_multiple_matches_returns_first() { |
| 357 |
// GIVEN |
| 358 |
let entity1 = create_test_entity("entity1", vec!["registrar", "technical"]); |
| 359 |
let entity2 = create_test_entity("entity2", vec!["registrar", "administrative"]); |
| 360 |
let entity3 = create_test_entity("entity3", vec!["abuse"]); |
| 361 |
|
| 362 |
let obj_common = ObjectCommon::entity() |
| 363 |
.and_entities(Some(vec![entity1, entity2, entity3])) |
| 364 |
.build(); |
| 365 |
|
| 366 |
// WHEN |
| 367 |
let result = obj_common.entity_by_role("registrar"); |
| 368 |
|
| 369 |
// THEN |
| 370 |
assert!(result.is_some()); |
| 371 |
assert_eq!(result.unwrap().handle(), Some("entity1")); |
| 372 |
} |
| 373 |
|
| 374 |
#[test] |
| 375 |
fn test_get_entity_by_role_entity_with_no_roles() { |
| 376 |
// GIVEN |
| 377 |
let entity1 = create_test_entity("entity1", vec![]); |
| 378 |
let entity2 = create_test_entity("entity2", vec!["registrar"]); |
| 379 |
|
| 380 |
let obj_common = ObjectCommon::entity() |
| 381 |
.and_entities(Some(vec![entity1, entity2])) |
| 382 |
.build(); |
| 383 |
|
| 384 |
// WHEN |
| 385 |
let result = obj_common.entity_by_role("registrar"); |
| 386 |
|
| 387 |
// THEN |
| 388 |
assert!(result.is_some()); |
| 389 |
assert_eq!(result.unwrap().handle(), Some("entity2")); |
| 390 |
} |
| 391 |
|
| 392 |
#[test] |
| 393 |
fn test_get_entity_by_role_case_sensitive() { |
| 394 |
// GIVEN |
| 395 |
let entity1 = create_test_entity("entity1", vec!["Registrar"]); |
| 396 |
let entity2 = create_test_entity("entity2", vec!["registrar"]); |
| 397 |
|
| 398 |
let obj_common = ObjectCommon::entity() |
| 399 |
.and_entities(Some(vec![entity1, entity2])) |
| 400 |
.build(); |
| 401 |
|
| 402 |
// WHEN |
| 403 |
let result_lower = obj_common.entity_by_role("registrar"); |
| 404 |
let result_upper = obj_common.entity_by_role("Registrar"); |
| 405 |
|
| 406 |
// THEN |
| 407 |
assert!(result_lower.is_some()); |
| 408 |
assert_eq!(result_lower.unwrap().handle(), Some("entity2")); |
| 409 |
|
| 410 |
assert!(result_upper.is_some()); |
| 411 |
assert_eq!(result_upper.unwrap().handle(), Some("entity1")); |
| 412 |
} |
| 413 |
|
| 414 |
#[test] |
| 415 |
fn test_get_entity_by_role_with_domain_object() { |
| 416 |
// GIVEN |
| 417 |
let entity1 = create_test_entity("entity1", vec!["registrant"]); |
| 418 |
let entity2 = create_test_entity("entity2", vec!["registrar"]); |
| 419 |
|
| 420 |
let obj_common = ObjectCommon::domain() |
| 421 |
.and_entities(Some(vec![entity1, entity2])) |
| 422 |
.build(); |
| 423 |
|
| 424 |
// WHEN |
| 425 |
let result = obj_common.entity_by_role("registrant"); |
| 426 |
|
| 427 |
// THEN |
| 428 |
assert!(result.is_some()); |
| 429 |
assert_eq!(result.unwrap().handle(), Some("entity1")); |
| 430 |
} |
| 431 |
} |