| 1 |
//! Common data structures, etc... |
| 2 |
use std::{collections::HashSet, ops::DerefMut}; |
| 3 |
|
| 4 |
use serde::{Deserialize, Serialize}; |
| 5 |
|
| 6 |
use crate::check::StringCheck; |
| 7 |
use crate::prelude::ContentExtensions; |
| 8 |
|
| 9 |
use super::lenient::{Stringish, VectorStringish}; |
| 10 |
|
| 11 |
/// Represents an RDAP extension identifier. |
| 12 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 13 |
pub struct Extension(pub String); |
| 14 |
|
| 15 |
impl From<&str> for Extension { |
| 16 |
fn from(value: &str) -> Self { |
| 17 |
Self(value.to_string()) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
impl std::ops::Deref for Extension { |
| 22 |
type Target = String; |
| 23 |
|
| 24 |
fn deref(&self) -> &Self::Target { |
| 25 |
&self.0 |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/// The RDAP conformance array. |
| 30 |
/// |
| 31 |
/// This is a vec of [Extension] specifically to be able to handle one or more |
| 32 |
/// unknown extension ids. Known extension identifiers are enumerated by [crate::prelude::ExtensionId]. |
| 33 |
pub type RdapConformance = Vec<Extension>; |
| 34 |
|
| 35 |
/// HrefLang, either a string or an array of strings. |
| 36 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 37 |
#[serde(untagged)] |
| 38 |
pub enum HrefLang { |
| 39 |
Langs(Vec<String>), |
| 40 |
Lang(String), |
| 41 |
} |
| 42 |
|
| 43 |
/// An array of RDAP link structures. |
| 44 |
pub type Links = Vec<Link>; |
| 45 |
|
| 46 |
/// Represents and RDAP link structure. |
| 47 |
/// |
| 48 |
/// This structure allows `value`, `rel`, and `href` to be |
| 49 |
/// optional to be tolerant of misbehaving servers, |
| 50 |
/// but those are fields required by RFC 9083. |
| 51 |
/// |
| 52 |
/// To create an RFC valid structure, use the builder |
| 53 |
/// which will not allow omission of required fields. |
| 54 |
/// |
| 55 |
/// ```rust |
| 56 |
/// use icann_rdap_common::prelude::*; |
| 57 |
/// |
| 58 |
/// let link = Link::builder() |
| 59 |
/// .value("https://example.com/domains?domain=foo.*") |
| 60 |
/// .rel("related") |
| 61 |
/// .href("https://example.com/domain/foo.example") |
| 62 |
/// .hreflang("ch") |
| 63 |
/// .title("Related Object") |
| 64 |
/// .media("print") |
| 65 |
/// .media_type("application/rdap+json") |
| 66 |
/// .build(); |
| 67 |
/// ``` |
| 68 |
/// |
| 69 |
/// Note also that this structure allows for `hreflang` to |
| 70 |
/// be either a single string or an array of strings. However, |
| 71 |
/// the builder will always construct an array of strings. |
| 72 |
/// |
| 73 |
/// Use the getter functions to get the data. |
| 74 |
/// ```rust |
| 75 |
/// # use icann_rdap_common::prelude::*; |
| 76 |
/// # let link = Link::builder() |
| 77 |
/// # .value("https://example.com/domains?domain=foo.*") |
| 78 |
/// # .rel("related") |
| 79 |
/// # .href("https://example.com/domain/foo.example") |
| 80 |
/// # .hreflang("ch") |
| 81 |
/// # .title("Related Object") |
| 82 |
/// # .media("print") |
| 83 |
/// # .media_type("application/rdap+json") |
| 84 |
/// # .build(); |
| 85 |
/// let href = link.href(); |
| 86 |
/// ``` |
| 87 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 88 |
pub struct Link { |
| 89 |
/// Represents the value part of a link in an RDAP response. |
| 90 |
/// According to RFC 9083, this field is required |
| 91 |
/// but many servers do not return it as it was |
| 92 |
/// optional in RFC 7483. |
| 93 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 94 |
pub value: Option<String>, |
| 95 |
|
| 96 |
/// Represents the relationship of a link in an RDAP response. |
| 97 |
/// According to RFC 9083, this field is required |
| 98 |
/// but many servers do not return it as it was |
| 99 |
/// optional in RFC 7483. |
| 100 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 101 |
pub rel: Option<String>, |
| 102 |
|
| 103 |
/// This is required by RDAP, both RFC 7043 and 9083, |
| 104 |
/// but is optional because some servers do the wrong thing. |
| 105 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 106 |
pub href: Option<String>, |
| 107 |
|
| 108 |
/// This can either be a string or an array of strings. |
| 109 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 110 |
pub hreflang: Option<HrefLang>, |
| 111 |
|
| 112 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 113 |
pub title: Option<String>, |
| 114 |
|
| 115 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 116 |
pub media: Option<String>, |
| 117 |
|
| 118 |
#[serde(rename = "type")] |
| 119 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 120 |
pub media_type: Option<String>, |
| 121 |
} |
| 122 |
|
| 123 |
#[buildstructor::buildstructor] |
| 124 |
impl Link { |
| 125 |
/// True if the link `rel` property is equal to the given value. |
| 126 |
pub fn is_relation(&self, rel: &str) -> bool { |
| 127 |
let Some(link_rel) = &self.rel else { |
| 128 |
return false; |
| 129 |
}; |
| 130 |
link_rel == rel |
| 131 |
} |
| 132 |
|
| 133 |
/// Builds an RDAP link. |
| 134 |
/// |
| 135 |
/// To create an RFC valid structure, use the builder |
| 136 |
/// which will not allow omission of required fields. |
| 137 |
/// |
| 138 |
/// ```rust |
| 139 |
/// use icann_rdap_common::prelude::*; |
| 140 |
/// |
| 141 |
/// let link = Link::builder() |
| 142 |
/// .value("https://example.com/domains?domain=foo.*") //required |
| 143 |
/// .rel("related") //required |
| 144 |
/// .href("https://example.com/domain/foo.example") //required |
| 145 |
/// .hreflang("ch") |
| 146 |
/// .title("Related Object") |
| 147 |
/// .media("print") |
| 148 |
/// .media_type("application/rdap+json") |
| 149 |
/// .build(); |
| 150 |
/// ``` |
| 151 |
#[builder(visibility = "pub")] |
| 152 |
fn new( |
| 153 |
value: String, |
| 154 |
href: String, |
| 155 |
rel: String, |
| 156 |
hreflang: Option<String>, |
| 157 |
title: Option<String>, |
| 158 |
media: Option<String>, |
| 159 |
media_type: Option<String>, |
| 160 |
) -> Self { |
| 161 |
let hreflang = hreflang.map(HrefLang::Lang); |
| 162 |
Self { |
| 163 |
value: Some(value), |
| 164 |
rel: Some(rel), |
| 165 |
href: Some(href), |
| 166 |
hreflang, |
| 167 |
title, |
| 168 |
media, |
| 169 |
media_type, |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/// Builds a potentially illegal RDAP link. |
| 174 |
#[builder(entry = "illegal", visibility = "pub(crate)")] |
| 175 |
#[allow(dead_code)] |
| 176 |
fn new_illegal( |
| 177 |
value: Option<String>, |
| 178 |
href: Option<String>, |
| 179 |
rel: Option<String>, |
| 180 |
hreflang: Option<String>, |
| 181 |
title: Option<String>, |
| 182 |
media: Option<String>, |
| 183 |
media_type: Option<String>, |
| 184 |
) -> Self { |
| 185 |
let hreflang = hreflang.map(HrefLang::Lang); |
| 186 |
Self { |
| 187 |
value, |
| 188 |
rel, |
| 189 |
href, |
| 190 |
hreflang, |
| 191 |
title, |
| 192 |
media, |
| 193 |
media_type, |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/// Returns the value of the link. |
| 198 |
pub fn value(&self) -> Option<&str> { |
| 199 |
self.value.as_deref() |
| 200 |
} |
| 201 |
|
| 202 |
/// Returns the relationship of the link. |
| 203 |
pub fn rel(&self) -> Option<&str> { |
| 204 |
self.rel.as_deref() |
| 205 |
} |
| 206 |
|
| 207 |
/// Returns the target URL of the link. |
| 208 |
pub fn href(&self) -> Option<&str> { |
| 209 |
self.href.as_deref() |
| 210 |
} |
| 211 |
|
| 212 |
/// Returns the language(s) of the linked resource. |
| 213 |
pub fn hreflang(&self) -> Option<&HrefLang> { |
| 214 |
self.hreflang.as_ref() |
| 215 |
} |
| 216 |
|
| 217 |
/// Returns the title of the link. |
| 218 |
pub fn title(&self) -> Option<&str> { |
| 219 |
self.title.as_deref() |
| 220 |
} |
| 221 |
|
| 222 |
/// Returns the media type for which the link is designed. |
| 223 |
pub fn media(&self) -> Option<&str> { |
| 224 |
self.media.as_deref() |
| 225 |
} |
| 226 |
|
| 227 |
/// Returns the media type of the linked resource. |
| 228 |
pub fn media_type(&self) -> Option<&str> { |
| 229 |
self.media_type.as_deref() |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/// An array of notices. |
| 234 |
pub type Notices = Vec<Notice>; |
| 235 |
|
| 236 |
/// Represents an RDAP Notice. |
| 237 |
/// |
| 238 |
/// It is recommended to use builder to construct an RFC valid |
| 239 |
/// structure. |
| 240 |
/// |
| 241 |
/// ```rust |
| 242 |
/// use icann_rdap_common::prelude::*; |
| 243 |
/// |
| 244 |
/// let link = Link::builder() |
| 245 |
/// .value("https://example.com/domains/foo.example") |
| 246 |
/// .rel("about") |
| 247 |
/// .href("https://example.com/tou.html") |
| 248 |
/// .hreflang("en") |
| 249 |
/// .title("ToU Link") |
| 250 |
/// .media_type("text/html") |
| 251 |
/// .build(); |
| 252 |
/// |
| 253 |
/// let notice = Notice::builder() |
| 254 |
/// .title("Terms of Use") |
| 255 |
/// .description_entry("Please read our terms of use.") |
| 256 |
/// .description_entry("TOS can be found in the link.") |
| 257 |
/// .link(link) |
| 258 |
/// .build(); |
| 259 |
/// ``` |
| 260 |
/// |
| 261 |
/// Use the getter functions to get the data. |
| 262 |
/// ```rust |
| 263 |
/// # use icann_rdap_common::prelude::*; |
| 264 |
/// # let notice = Notice::builder() |
| 265 |
/// # .title("Terms of Use") |
| 266 |
/// # .description_entry("Please read our terms of use.") |
| 267 |
/// # .description_entry("TOS can be found in the link.") |
| 268 |
/// # .build(); |
| 269 |
/// let title = notice.title(); |
| 270 |
/// ``` |
| 271 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 272 |
pub struct Notice(pub NoticeOrRemark); |
| 273 |
|
| 274 |
#[buildstructor::buildstructor] |
| 275 |
impl Notice { |
| 276 |
/// Builds an RDAP notice. |
| 277 |
#[builder(visibility = "pub")] |
| 278 |
fn new( |
| 279 |
title: Option<String>, |
| 280 |
description: Vec<String>, |
| 281 |
links: Vec<Link>, |
| 282 |
nr_type: Option<String>, |
| 283 |
simple_redaction_keys: Option<Vec<String>>, |
| 284 |
) -> Self { |
| 285 |
let nr = NoticeOrRemark::builder() |
| 286 |
.description(description) |
| 287 |
.and_title(title) |
| 288 |
.links(links) |
| 289 |
.and_nr_type(nr_type) |
| 290 |
.and_simple_redaction_keys(simple_redaction_keys) |
| 291 |
.build(); |
| 292 |
Self(nr) |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
impl std::ops::Deref for Notice { |
| 297 |
type Target = NoticeOrRemark; |
| 298 |
|
| 299 |
fn deref(&self) -> &Self::Target { |
| 300 |
&self.0 |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
impl ContentExtensions for Notice { |
| 305 |
fn content_extensions(&self) -> HashSet<super::ExtensionId> { |
| 306 |
self.0.content_extensions() |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
/// An array of remarks. |
| 311 |
pub type Remarks = Vec<Remark>; |
| 312 |
|
| 313 |
/// Represents an RDAP Remark. |
| 314 |
/// |
| 315 |
/// It is recommended to use builder to construct an RFC valid |
| 316 |
/// structure. |
| 317 |
/// |
| 318 |
/// ```rust |
| 319 |
/// use icann_rdap_common::prelude::*; |
| 320 |
/// |
| 321 |
/// let link = Link::builder() |
| 322 |
/// .value("https://example.com/domains/foo.example") |
| 323 |
/// .rel("about") |
| 324 |
/// .href("https://example.com/tou.html") |
| 325 |
/// .hreflang("en") |
| 326 |
/// .title("ToU Link") |
| 327 |
/// .media_type("text/html") |
| 328 |
/// .build(); |
| 329 |
/// |
| 330 |
/// let remark = Remark::builder() |
| 331 |
/// .title("Terms of Use") |
| 332 |
/// .description_entry("Please read our terms of use.") |
| 333 |
/// .description_entry("TOS can be found in the link.") |
| 334 |
/// .link(link) |
| 335 |
/// .build(); |
| 336 |
/// ``` |
| 337 |
/// |
| 338 |
/// Use the getter functions to get the data. |
| 339 |
/// ```rust |
| 340 |
/// # use icann_rdap_common::prelude::*; |
| 341 |
/// # let remark = Remark::builder() |
| 342 |
/// # .title("Terms of Use") |
| 343 |
/// # .description_entry("Please read our terms of use.") |
| 344 |
/// # .description_entry("TOS can be found in the link.") |
| 345 |
/// # .build(); |
| 346 |
/// let title = remark.title(); |
| 347 |
/// ``` |
| 348 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 349 |
pub struct Remark(pub NoticeOrRemark); |
| 350 |
|
| 351 |
impl DerefMut for Remark { |
| 352 |
fn deref_mut(&mut self) -> &mut Self::Target { |
| 353 |
&mut self.0 |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
#[buildstructor::buildstructor] |
| 358 |
impl Remark { |
| 359 |
/// Builds an RDAP notice. |
| 360 |
#[builder(visibility = "pub")] |
| 361 |
fn new( |
| 362 |
title: Option<String>, |
| 363 |
description: Vec<String>, |
| 364 |
links: Vec<Link>, |
| 365 |
nr_type: Option<String>, |
| 366 |
simple_redaction_keys: Option<Vec<String>>, |
| 367 |
) -> Self { |
| 368 |
let nr = NoticeOrRemark::builder() |
| 369 |
.description(description) |
| 370 |
.and_title(title) |
| 371 |
.links(links) |
| 372 |
.and_nr_type(nr_type) |
| 373 |
.and_simple_redaction_keys(simple_redaction_keys) |
| 374 |
.build(); |
| 375 |
Self(nr) |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
impl std::ops::Deref for Remark { |
| 380 |
type Target = NoticeOrRemark; |
| 381 |
|
| 382 |
fn deref(&self) -> &Self::Target { |
| 383 |
&self.0 |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
impl ContentExtensions for Remark { |
| 388 |
fn content_extensions(&self) -> HashSet<super::ExtensionId> { |
| 389 |
self.0.content_extensions() |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
/// Represents an RDAP Notice or Remark (they are the same thing in RDAP). |
| 394 |
/// |
| 395 |
/// It is probably easier to use [Notice] or [Remark] directly. |
| 396 |
/// |
| 397 |
/// RFC 9083 requires that `description` be required, but some servers |
| 398 |
/// do not follow this rule. Therefore, this structure allows `description` |
| 399 |
/// to be optional. It is recommended to use builder to construct an RFC valid |
| 400 |
/// structure. |
| 401 |
/// |
| 402 |
/// ```rust |
| 403 |
/// use icann_rdap_common::prelude::*; |
| 404 |
/// |
| 405 |
/// let link = Link::builder() |
| 406 |
/// .value("https://example.com/domains/foo.example") |
| 407 |
/// .rel("about") |
| 408 |
/// .href("https://example.com/tou.html") |
| 409 |
/// .hreflang("en") |
| 410 |
/// .title("ToU Link") |
| 411 |
/// .media_type("text/html") |
| 412 |
/// .build(); |
| 413 |
/// |
| 414 |
/// let nr = NoticeOrRemark::builder() |
| 415 |
/// .title("Terms of Use") |
| 416 |
/// .description_entry("Please read our terms of use.") |
| 417 |
/// .description_entry("TOS can be found in the link.") |
| 418 |
/// .links(vec![link]) |
| 419 |
/// .build(); |
| 420 |
/// ``` |
| 421 |
/// |
| 422 |
/// Use the getter functions to get the data. |
| 423 |
/// ```rust |
| 424 |
/// # use icann_rdap_common::prelude::*; |
| 425 |
/// # let nr = NoticeOrRemark::builder() |
| 426 |
/// # .title("Terms of Use") |
| 427 |
/// # .description_entry("Please read our terms of use.") |
| 428 |
/// # .description_entry("TOS can be found in the link.") |
| 429 |
/// # .build(); |
| 430 |
/// let title = nr.title(); |
| 431 |
/// ``` |
| 432 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 433 |
pub struct NoticeOrRemark { |
| 434 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 435 |
pub title: Option<String>, |
| 436 |
|
| 437 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 438 |
pub description: Option<VectorStringish>, |
| 439 |
|
| 440 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 441 |
pub links: Option<Links>, |
| 442 |
|
| 443 |
/// Description `type` as is found in the IANA registry. |
| 444 |
#[serde(rename = "type")] |
| 445 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 446 |
pub nr_type: Option<String>, |
| 447 |
|
| 448 |
/// Redaction key from the simple redaction extension. |
| 449 |
#[serde(rename = "simpleRedaction_keys")] |
| 450 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 451 |
pub simple_redaction_keys: Option<Vec<String>>, |
| 452 |
} |
| 453 |
|
| 454 |
#[buildstructor::buildstructor] |
| 455 |
impl NoticeOrRemark { |
| 456 |
/// Builds an RDAP notice/remark. |
| 457 |
#[builder(visibility = "pub")] |
| 458 |
fn new( |
| 459 |
title: Option<String>, |
| 460 |
description: Vec<String>, |
| 461 |
links: Vec<Link>, |
| 462 |
nr_type: Option<String>, |
| 463 |
simple_redaction_keys: Option<Vec<String>>, |
| 464 |
) -> Self { |
| 465 |
Self { |
| 466 |
title, |
| 467 |
description: Some(VectorStringish::from(description)), |
| 468 |
links: (!links.is_empty()).then_some(links), |
| 469 |
nr_type, |
| 470 |
simple_redaction_keys, |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
/// Builds an illegal RDAP notice/remark. |
| 475 |
#[builder(entry = "illegal", visibility = "pub(crate)")] |
| 476 |
#[allow(dead_code)] |
| 477 |
fn new_illegal( |
| 478 |
title: Option<String>, |
| 479 |
description: Option<Vec<String>>, |
| 480 |
links: Option<Vec<Link>>, |
| 481 |
nr_type: Option<String>, |
| 482 |
simple_redaction_keys: Option<Vec<String>>, |
| 483 |
) -> Self { |
| 484 |
let d = description |
| 485 |
.is_some() |
| 486 |
.then_some(VectorStringish::from(description.unwrap())); |
| 487 |
Self { |
| 488 |
title, |
| 489 |
description: d, |
| 490 |
links, |
| 491 |
nr_type, |
| 492 |
simple_redaction_keys, |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
/// Returns the title of the notice/remark. |
| 497 |
pub fn title(&self) -> Option<&str> { |
| 498 |
self.title.as_deref() |
| 499 |
} |
| 500 |
|
| 501 |
/// Returns the description of the notice/remark. |
| 502 |
pub fn description(&self) -> &[String] { |
| 503 |
self.description |
| 504 |
.as_ref() |
| 505 |
.map(|v| v.vec().as_ref()) |
| 506 |
.unwrap_or_default() |
| 507 |
} |
| 508 |
|
| 509 |
/// Returns the description where lines that are |
| 510 |
/// not sentences are consolidated into paragraphs. |
| 511 |
/// |
| 512 |
/// Paragraph boundaries are created by: |
| 513 |
/// - Lines ending with a period (complete sentences) |
| 514 |
/// - Lines that are entirely uppercase (headers/acronyms) |
| 515 |
/// - Empty or whitespace-only lines (explicit breaks) |
| 516 |
/// - ASCII table lines (pipe-delimited or border lines) — each gets its own paragraph |
| 517 |
/// - Merge limits: paragraphs exceeding 500 characters or 10 merged lines are split |
| 518 |
pub fn description_as_pgs(&self) -> Vec<String> { |
| 519 |
const MAX_PG_LEN: usize = 500; |
| 520 |
const MAX_MERGED_LINES: usize = 10; |
| 521 |
|
| 522 |
let mut pgs = vec![]; |
| 523 |
let mut acc_line = String::new(); |
| 524 |
let mut merged_line_count: usize = 0; |
| 525 |
|
| 526 |
for line in self.description() { |
| 527 |
let trimmed = line.trim(); |
| 528 |
|
| 529 |
// Empty/whitespace-only lines act as explicit paragraph breaks |
| 530 |
if trimmed.is_empty() { |
| 531 |
if !acc_line.is_empty() { |
| 532 |
if !acc_line.is_whitespace_or_empty() { |
| 533 |
pgs.push(acc_line.clone()); |
| 534 |
} |
| 535 |
acc_line.clear(); |
| 536 |
merged_line_count = 0; |
| 537 |
} |
| 538 |
continue; |
| 539 |
} |
| 540 |
|
| 541 |
// ASCII table lines get their own paragraph |
| 542 |
if trimmed.is_ascii_table_line() { |
| 543 |
// Flush any accumulated text first |
| 544 |
if !acc_line.is_empty() { |
| 545 |
if !acc_line.is_whitespace_or_empty() { |
| 546 |
pgs.push(acc_line.clone()); |
| 547 |
} |
| 548 |
acc_line.clear(); |
| 549 |
merged_line_count = 0; |
| 550 |
} |
| 551 |
let cleaned = trimmed.trim_start_matches('|').trim_end_matches('|').trim(); |
| 552 |
if cleaned.chars().all(|c| matches!(c, '+' | '-' | '=')) && cleaned.len() >= 10 { |
| 553 |
let first_char = cleaned.chars().next().unwrap(); |
| 554 |
pgs.push(format!("{0}{0}{0}...", first_char)); |
| 555 |
} else { |
| 556 |
pgs.push(cleaned.to_string()); |
| 557 |
} |
| 558 |
continue; |
| 559 |
} |
| 560 |
|
| 561 |
// Check if we need to split due to merge limits before adding |
| 562 |
if !acc_line.is_empty() { |
| 563 |
let new_len = acc_line.len() + trimmed.len(); |
| 564 |
if new_len > MAX_PG_LEN || merged_line_count >= MAX_MERGED_LINES { |
| 565 |
// Try to split at the last period |
| 566 |
if let Some(split_pos) = acc_line.rfind('.') { |
| 567 |
let before = acc_line[..=split_pos].to_string(); |
| 568 |
let after = acc_line[split_pos + 1..].trim().to_string(); |
| 569 |
if !before.is_whitespace_or_empty() { |
| 570 |
pgs.push(before); |
| 571 |
} |
| 572 |
acc_line = after; |
| 573 |
merged_line_count = 1; |
| 574 |
} else { |
| 575 |
// No period found, truncate at max length |
| 576 |
if !acc_line.is_whitespace_or_empty() { |
| 577 |
pgs.push(acc_line.clone()); |
| 578 |
} |
| 579 |
acc_line.clear(); |
| 580 |
merged_line_count = 0; |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
// Accumulate the line |
| 586 |
if !acc_line.is_empty() { |
| 587 |
acc_line.push(' '); |
| 588 |
} |
| 589 |
acc_line.push_str(trimmed); |
| 590 |
merged_line_count += 1; |
| 591 |
|
| 592 |
// Check for sentence boundary |
| 593 |
if trimmed.ends_with('.') || trimmed.to_ascii_uppercase().eq(trimmed) { |
| 594 |
if !acc_line.is_whitespace_or_empty() { |
| 595 |
pgs.push(acc_line.clone()); |
| 596 |
} |
| 597 |
acc_line.clear(); |
| 598 |
merged_line_count = 0; |
| 599 |
} |
| 600 |
} |
| 601 |
if !acc_line.is_empty() && !acc_line.is_whitespace_or_empty() { |
| 602 |
pgs.push(acc_line); |
| 603 |
} |
| 604 |
pgs |
| 605 |
} |
| 606 |
|
| 607 |
/// Returns the links associated with the notice/remark. |
| 608 |
pub fn links(&self) -> &[Link] { |
| 609 |
self.links.as_deref().unwrap_or_default() |
| 610 |
} |
| 611 |
|
| 612 |
/// Returns the `type` of the notice or remark. |
| 613 |
/// |
| 614 |
/// These values are suppose to come from the IANA RDAP registry. |
| 615 |
pub fn nr_type(&self) -> Option<&str> { |
| 616 |
self.nr_type.as_deref() |
| 617 |
} |
| 618 |
|
| 619 |
/// Returns the simple redactions keys. |
| 620 |
pub fn simple_redaction_keys(&self) -> &[String] { |
| 621 |
self.simple_redaction_keys.as_deref().unwrap_or_default() |
| 622 |
} |
| 623 |
|
| 624 |
/// Returns true if a key is a simple redaction key for this notice or remark. |
| 625 |
pub fn has_simple_redaction_key(&self, key: &str) -> bool { |
| 626 |
self.simple_redaction_keys().iter().any(|k| k.eq(key)) |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
/// Conversion for collection of notices. |
| 631 |
pub trait ToNotices { |
| 632 |
/// Convert to a collection of notices. |
| 633 |
fn to_notices(self) -> Vec<Notice>; |
| 634 |
/// Convert to a collection if some; otherwise, none. |
| 635 |
fn to_opt_notices(self) -> Option<Vec<Notice>>; |
| 636 |
} |
| 637 |
|
| 638 |
impl ToNotices for &[NoticeOrRemark] { |
| 639 |
fn to_notices(self) -> Vec<Notice> { |
| 640 |
self.iter().map(|n| Notice(n.clone())).collect::<Notices>() |
| 641 |
} |
| 642 |
|
| 643 |
fn to_opt_notices(self) -> Option<Vec<Notice>> { |
| 644 |
let notices = self.to_notices(); |
| 645 |
(!notices.is_empty()).then_some(notices) |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
impl ToNotices for Vec<NoticeOrRemark> { |
| 650 |
fn to_notices(self) -> Vec<Notice> { |
| 651 |
self.into_iter().map(Notice).collect::<Notices>() |
| 652 |
} |
| 653 |
|
| 654 |
fn to_opt_notices(self) -> Option<Vec<Notice>> { |
| 655 |
let notices = self.to_notices(); |
| 656 |
(!notices.is_empty()).then_some(notices) |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
/// Conversion for collection of remarks. |
| 661 |
pub trait ToRemarks { |
| 662 |
/// Convert to a collection of remarks. |
| 663 |
fn to_remarks(self) -> Vec<Remark>; |
| 664 |
/// Convert to a collection if some; otherwise, none. |
| 665 |
fn to_opt_remarks(self) -> Option<Vec<Remark>>; |
| 666 |
} |
| 667 |
|
| 668 |
impl ToRemarks for &[NoticeOrRemark] { |
| 669 |
fn to_remarks(self) -> Vec<Remark> { |
| 670 |
self.iter().map(|n| Remark(n.clone())).collect::<Remarks>() |
| 671 |
} |
| 672 |
|
| 673 |
fn to_opt_remarks(self) -> Option<Vec<Remark>> { |
| 674 |
let remarks = self.to_remarks(); |
| 675 |
(!remarks.is_empty()).then_some(remarks) |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
impl ToRemarks for Vec<NoticeOrRemark> { |
| 680 |
fn to_remarks(self) -> Vec<Remark> { |
| 681 |
self.into_iter().map(Remark).collect::<Remarks>() |
| 682 |
} |
| 683 |
|
| 684 |
fn to_opt_remarks(self) -> Option<Vec<Remark>> { |
| 685 |
let remarks = self.to_remarks(); |
| 686 |
(!remarks.is_empty()).then_some(remarks) |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
impl ContentExtensions for NoticeOrRemark { |
| 691 |
fn content_extensions(&self) -> std::collections::HashSet<super::ExtensionId> { |
| 692 |
if self.simple_redaction_keys.is_some() { |
| 693 |
HashSet::from([super::ExtensionId::SimpleRedaction]) |
| 694 |
} else { |
| 695 |
HashSet::new() |
| 696 |
} |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
/// An array of events. |
| 701 |
pub type Events = Vec<Event>; |
| 702 |
|
| 703 |
/// Represents an RDAP event. |
| 704 |
/// |
| 705 |
/// RFC 9083 requires `eventAction` (event_action) and `eventDate` (event_date), but |
| 706 |
/// this structure allows those to be optional to be able to parse responses from |
| 707 |
/// servers that do not strictly obey the RFC. |
| 708 |
/// |
| 709 |
/// Use of the builder to construct an RFC valid structure is recommended. |
| 710 |
/// |
| 711 |
/// ```rust |
| 712 |
/// use icann_rdap_common::prelude::*; |
| 713 |
/// |
| 714 |
/// let link = Link::builder() |
| 715 |
/// .value("https://example.com/domains/foo.example") |
| 716 |
/// .rel("about") |
| 717 |
/// .href("https://example.com/registration-duration.html") |
| 718 |
/// .hreflang("en") |
| 719 |
/// .title("Domain Validity Period") |
| 720 |
/// .media_type("text/html") |
| 721 |
/// .build(); |
| 722 |
/// |
| 723 |
/// let event = Event::builder() |
| 724 |
/// .event_action("expiration") |
| 725 |
/// .event_date("1990-12-31T23:59:59Z") |
| 726 |
/// .links(vec![link]) |
| 727 |
/// .build(); |
| 728 |
/// ``` |
| 729 |
/// |
| 730 |
/// NOTE: `event_date` is to be an RFC 3339 valid date and time. |
| 731 |
/// The builder does not enforce RFC 3339 validity. |
| 732 |
/// |
| 733 |
/// Use the getter functions to get the data. |
| 734 |
/// ```rust |
| 735 |
/// # use icann_rdap_common::prelude::*; |
| 736 |
/// # let event = Event::builder() |
| 737 |
/// # .event_action("expiration") |
| 738 |
/// # .event_date("1990-12-31T23:59:59Z") |
| 739 |
/// # .build(); |
| 740 |
/// let event_date = event.event_date(); |
| 741 |
/// ``` |
| 742 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 743 |
pub struct Event { |
| 744 |
/// This value is required by RFC 9083 (and 7483), |
| 745 |
/// however some servers don't include it. Therefore |
| 746 |
/// it is optional here to be compatible with these |
| 747 |
/// types of non-compliant servers. |
| 748 |
#[serde(rename = "eventAction")] |
| 749 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 750 |
pub event_action: Option<String>, |
| 751 |
|
| 752 |
#[serde(rename = "eventActor")] |
| 753 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 754 |
pub event_actor: Option<String>, |
| 755 |
|
| 756 |
/// This value is required by RFC 9083 (and 7483), |
| 757 |
/// however some servers don't include it. Therefore |
| 758 |
/// it is optional here to be compatible with these |
| 759 |
/// types of non-compliant servers. |
| 760 |
#[serde(rename = "eventDate")] |
| 761 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 762 |
pub event_date: Option<String>, |
| 763 |
|
| 764 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 765 |
pub links: Option<Links>, |
| 766 |
} |
| 767 |
|
| 768 |
#[buildstructor::buildstructor] |
| 769 |
impl Event { |
| 770 |
/// Builds an Event. |
| 771 |
/// |
| 772 |
/// Use of the builder to construct an RFC valid structure is recommended. |
| 773 |
/// |
| 774 |
/// ```rust |
| 775 |
/// use icann_rdap_common::prelude::*; |
| 776 |
/// |
| 777 |
/// let event = Event::builder() |
| 778 |
/// .event_action("expiration") //required |
| 779 |
/// .event_date("1990-12-31T23:59:59Z") //required |
| 780 |
/// .event_actor("FOO") |
| 781 |
/// .build(); |
| 782 |
/// ``` |
| 783 |
#[builder(visibility = "pub")] |
| 784 |
fn new( |
| 785 |
event_action: String, |
| 786 |
event_date: String, |
| 787 |
event_actor: Option<String>, |
| 788 |
links: Option<Links>, |
| 789 |
) -> Self { |
| 790 |
Self { |
| 791 |
event_action: Some(event_action), |
| 792 |
event_actor, |
| 793 |
event_date: Some(event_date), |
| 794 |
links, |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
#[builder(entry = "illegal", visibility = "pub(crate)")] |
| 799 |
#[allow(dead_code)] |
| 800 |
fn new_illegal( |
| 801 |
event_action: Option<String>, |
| 802 |
event_date: Option<String>, |
| 803 |
event_actor: Option<String>, |
| 804 |
links: Option<Links>, |
| 805 |
) -> Self { |
| 806 |
Self { |
| 807 |
event_action, |
| 808 |
event_actor, |
| 809 |
event_date, |
| 810 |
links, |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
/// Returns the action associated with the event. |
| 815 |
pub fn event_action(&self) -> Option<&str> { |
| 816 |
self.event_action.as_deref() |
| 817 |
} |
| 818 |
|
| 819 |
/// Returns the actor associated with the event. |
| 820 |
pub fn event_actor(&self) -> Option<&str> { |
| 821 |
self.event_actor.as_deref() |
| 822 |
} |
| 823 |
|
| 824 |
/// Returns the date and time of the event. |
| 825 |
pub fn event_date(&self) -> Option<&str> { |
| 826 |
self.event_date.as_deref() |
| 827 |
} |
| 828 |
|
| 829 |
/// Returns the links associated with the event. |
| 830 |
pub fn links(&self) -> &[Link] { |
| 831 |
self.links.as_deref().unwrap_or_default() |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
/// An RDAP port53 type. |
| 836 |
pub type Port43 = String; |
| 837 |
|
| 838 |
/// An array of RDAP public IDs. |
| 839 |
pub type PublicIds = Vec<PublicId>; |
| 840 |
|
| 841 |
/// An RDAP Public ID. |
| 842 |
/// |
| 843 |
/// RFC 9083 requires `type` (id_type) and `identifier`, but |
| 844 |
/// this structure allows those to be optional to be able to parse responses from |
| 845 |
/// servers that do not strictly obey the RFC. |
| 846 |
/// |
| 847 |
/// Use of the builder to construct an RFC valid structure is recommended. |
| 848 |
/// |
| 849 |
/// ```rust |
| 850 |
/// use icann_rdap_common::prelude::*; |
| 851 |
/// |
| 852 |
/// let public_id = PublicId::builder() |
| 853 |
/// .id_type("IANA Registrar ID") |
| 854 |
/// .identifier("1990") |
| 855 |
/// .build(); |
| 856 |
/// ``` |
| 857 |
/// |
| 858 |
/// Use the getter functions to get the data. |
| 859 |
/// ```rust |
| 860 |
/// # use icann_rdap_common::prelude::*; |
| 861 |
/// # let public_id = PublicId::builder() |
| 862 |
/// # .id_type("IANA Registrar ID") |
| 863 |
/// # .identifier("1990") |
| 864 |
/// # .build(); |
| 865 |
/// let id_type = public_id.id_type(); |
| 866 |
/// ``` |
| 867 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 868 |
pub struct PublicId { |
| 869 |
/// This are mandatory per RFC 9083. |
| 870 |
#[serde(rename = "type")] |
| 871 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 872 |
pub id_type: Option<Stringish>, |
| 873 |
|
| 874 |
/// This are mandatory per RFC 9083. |
| 875 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 876 |
pub identifier: Option<Stringish>, |
| 877 |
} |
| 878 |
|
| 879 |
#[buildstructor::buildstructor] |
| 880 |
impl PublicId { |
| 881 |
/// Builds a public ID. |
| 882 |
#[builder(visibility = "pub")] |
| 883 |
fn new(id_type: String, identifier: String) -> Self { |
| 884 |
Self { |
| 885 |
id_type: Some(id_type.into()), |
| 886 |
identifier: Some(identifier.into()), |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
/// Builds an illegal public ID. |
| 891 |
#[builder(entry = "illegal", visibility = "pub(crate)")] |
| 892 |
#[allow(dead_code)] |
| 893 |
fn new_illegal(id_type: Option<String>, identifier: Option<String>) -> Self { |
| 894 |
Self { |
| 895 |
id_type: id_type.map(|s| s.into()), |
| 896 |
identifier: identifier.map(|s| s.into()), |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
/// Returns the type of the public ID. |
| 901 |
pub fn id_type(&self) -> Option<&str> { |
| 902 |
self.id_type.as_deref() |
| 903 |
} |
| 904 |
|
| 905 |
/// Returns the identifier of the public ID. |
| 906 |
pub fn identifier(&self) -> Option<&str> { |
| 907 |
self.identifier.as_deref() |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
#[cfg(test)] |
| 912 |
mod tests { |
| 913 |
use crate::{ |
| 914 |
prelude::ObjectCommon, |
| 915 |
response::types::{Extension, Notice, Notices, RdapConformance, Remark, Remarks}, |
| 916 |
}; |
| 917 |
|
| 918 |
use super::{Event, Link, Links, NoticeOrRemark, PublicId}; |
| 919 |
|
| 920 |
#[test] |
| 921 |
fn test_rdap_conformance_serialize() { |
| 922 |
// GIVEN rdap conformance |
| 923 |
let rdap_conformance: RdapConformance = |
| 924 |
vec![Extension("foo".to_string()), Extension("bar".to_string())]; |
| 925 |
|
| 926 |
// WHEN serialized |
| 927 |
let actual = serde_json::to_string(&rdap_conformance).unwrap(); |
| 928 |
|
| 929 |
// THEN expect array of strings |
| 930 |
let expected = r#"["foo","bar"]"#; |
| 931 |
assert_eq!(actual, expected); |
| 932 |
} |
| 933 |
|
| 934 |
#[test] |
| 935 |
fn test_an_array_of_links_deserialize() { |
| 936 |
// GIVEN array of links |
| 937 |
let expected = r#" |
| 938 |
[ |
| 939 |
{ |
| 940 |
"value" : "https://1.example.com/context_uri", |
| 941 |
"rel" : "self", |
| 942 |
"href" : "https://1.example.com/target_uri", |
| 943 |
"hreflang" : [ "en", "ch" ], |
| 944 |
"title" : "title1", |
| 945 |
"media" : "screen", |
| 946 |
"type" : "application/json" |
| 947 |
}, |
| 948 |
{ |
| 949 |
"value" : "https://2.example.com/context_uri", |
| 950 |
"rel" : "self", |
| 951 |
"href" : "https://2.example.com/target_uri", |
| 952 |
"hreflang" : [ "en", "ch" ], |
| 953 |
"title" : "title2", |
| 954 |
"media" : "screen", |
| 955 |
"type" : "application/json" |
| 956 |
} |
| 957 |
] |
| 958 |
"#; |
| 959 |
|
| 960 |
// WHEN deserialize |
| 961 |
let links = serde_json::from_str::<Links>(expected); |
| 962 |
|
| 963 |
// THEN data is correct |
| 964 |
let actual = links.unwrap(); |
| 965 |
assert_eq!(actual.len(), 2); |
| 966 |
let actual_1 = actual.first().unwrap(); |
| 967 |
let actual_2 = actual.last().unwrap(); |
| 968 |
assert_eq!( |
| 969 |
actual_1.value.as_ref().unwrap(), |
| 970 |
"https://1.example.com/context_uri" |
| 971 |
); |
| 972 |
assert_eq!( |
| 973 |
actual_2.value.as_ref().unwrap(), |
| 974 |
"https://2.example.com/context_uri" |
| 975 |
); |
| 976 |
assert_eq!( |
| 977 |
actual_1.href.as_ref().unwrap(), |
| 978 |
"https://1.example.com/target_uri" |
| 979 |
); |
| 980 |
assert_eq!( |
| 981 |
actual_2.href.as_ref().unwrap(), |
| 982 |
"https://2.example.com/target_uri" |
| 983 |
); |
| 984 |
assert_eq!(actual_1.title.as_ref().unwrap(), "title1"); |
| 985 |
assert_eq!(actual_2.title.as_ref().unwrap(), "title2"); |
| 986 |
assert_eq!(actual_1.media_type.as_ref().unwrap(), "application/json"); |
| 987 |
assert_eq!(actual_2.media_type.as_ref().unwrap(), "application/json"); |
| 988 |
} |
| 989 |
|
| 990 |
#[test] |
| 991 |
fn test_an_array_of_links_with_one_lang() { |
| 992 |
// GIVEN array of links with one lang |
| 993 |
let expected = r#" |
| 994 |
[ |
| 995 |
{ |
| 996 |
"value" : "https://1.example.com/context_uri", |
| 997 |
"rel" : "self", |
| 998 |
"href" : "https://1.example.com/target_uri", |
| 999 |
"hreflang" : "en", |
| 1000 |
"title" : "title1", |
| 1001 |
"media" : "screen", |
| 1002 |
"type" : "application/json" |
| 1003 |
}, |
| 1004 |
{ |
| 1005 |
"value" : "https://2.example.com/context_uri", |
| 1006 |
"rel" : "self", |
| 1007 |
"href" : "https://2.example.com/target_uri", |
| 1008 |
"hreflang" : "ch", |
| 1009 |
"title" : "title2", |
| 1010 |
"media" : "screen", |
| 1011 |
"type" : "application/json" |
| 1012 |
} |
| 1013 |
] |
| 1014 |
"#; |
| 1015 |
|
| 1016 |
// WHEN deserialize |
| 1017 |
let links = serde_json::from_str::<Links>(expected); |
| 1018 |
|
| 1019 |
// THEN data is accurate |
| 1020 |
let actual = links.unwrap(); |
| 1021 |
assert_eq!(actual.len(), 2); |
| 1022 |
let actual_1 = actual.first().unwrap(); |
| 1023 |
let actual_2 = actual.last().unwrap(); |
| 1024 |
assert_eq!( |
| 1025 |
actual_1.value.as_ref().unwrap(), |
| 1026 |
"https://1.example.com/context_uri" |
| 1027 |
); |
| 1028 |
assert_eq!( |
| 1029 |
actual_2.value.as_ref().unwrap(), |
| 1030 |
"https://2.example.com/context_uri" |
| 1031 |
); |
| 1032 |
assert_eq!( |
| 1033 |
actual_1.href.as_ref().unwrap(), |
| 1034 |
"https://1.example.com/target_uri" |
| 1035 |
); |
| 1036 |
assert_eq!( |
| 1037 |
actual_2.href.as_ref().unwrap(), |
| 1038 |
"https://2.example.com/target_uri" |
| 1039 |
); |
| 1040 |
assert_eq!(actual_1.title.as_ref().unwrap(), "title1"); |
| 1041 |
assert_eq!(actual_2.title.as_ref().unwrap(), "title2"); |
| 1042 |
assert_eq!(actual_1.media_type.as_ref().unwrap(), "application/json"); |
| 1043 |
assert_eq!(actual_2.media_type.as_ref().unwrap(), "application/json"); |
| 1044 |
} |
| 1045 |
|
| 1046 |
#[test] |
| 1047 |
fn test_a_notice_or_remark_deserialize() { |
| 1048 |
// GIVEN notice or remark |
| 1049 |
let expected = r#" |
| 1050 |
{ |
| 1051 |
"title" : "Terms of Use", |
| 1052 |
"description" : |
| 1053 |
[ |
| 1054 |
"Service subject to The Registry of the Moon's TOS.", |
| 1055 |
"Copyright (c) 2020 LunarNIC" |
| 1056 |
], |
| 1057 |
"links" : |
| 1058 |
[ |
| 1059 |
{ |
| 1060 |
"value" : "https://example.net/entity/XXXX", |
| 1061 |
"rel" : "alternate", |
| 1062 |
"type" : "text/html", |
| 1063 |
"href" : "https://www.example.com/terms_of_use.html" |
| 1064 |
} |
| 1065 |
] |
| 1066 |
} |
| 1067 |
"#; |
| 1068 |
|
| 1069 |
// WHEN deserialize |
| 1070 |
let actual = serde_json::from_str::<NoticeOrRemark>(expected); |
| 1071 |
|
| 1072 |
// THEN data is accurate |
| 1073 |
let actual = actual.unwrap(); |
| 1074 |
actual.title.as_ref().unwrap(); |
| 1075 |
let description: Vec<String> = actual.description.expect("must have description").into(); |
| 1076 |
assert_eq!(description.len(), 2); |
| 1077 |
actual.links.unwrap(); |
| 1078 |
} |
| 1079 |
|
| 1080 |
#[test] |
| 1081 |
fn test_notices_serialize() { |
| 1082 |
// GIVEN notices |
| 1083 |
let notices: Notices = vec![ |
| 1084 |
Notice( |
| 1085 |
NoticeOrRemark::builder() |
| 1086 |
.description(vec!["foo".to_string()]) |
| 1087 |
.build(), |
| 1088 |
), |
| 1089 |
Notice( |
| 1090 |
NoticeOrRemark::builder() |
| 1091 |
.description(vec!["bar".to_string()]) |
| 1092 |
.build(), |
| 1093 |
), |
| 1094 |
]; |
| 1095 |
|
| 1096 |
// WHEN deserialize |
| 1097 |
let actual = serde_json::to_string(¬ices).unwrap(); |
| 1098 |
|
| 1099 |
// THEN then array of notices |
| 1100 |
let expected = r#"[{"description":["foo"]},{"description":["bar"]}]"#; |
| 1101 |
assert_eq!(actual, expected); |
| 1102 |
} |
| 1103 |
|
| 1104 |
#[test] |
| 1105 |
fn test_remarks_serialize() { |
| 1106 |
// GIVEN remarks |
| 1107 |
let remarks: Remarks = vec![ |
| 1108 |
Remark( |
| 1109 |
NoticeOrRemark::builder() |
| 1110 |
.description(vec!["foo".to_string()]) |
| 1111 |
.build(), |
| 1112 |
), |
| 1113 |
Remark( |
| 1114 |
NoticeOrRemark::builder() |
| 1115 |
.description(vec!["bar".to_string()]) |
| 1116 |
.build(), |
| 1117 |
), |
| 1118 |
]; |
| 1119 |
|
| 1120 |
// WHEN serialize |
| 1121 |
let actual = serde_json::to_string(&remarks).unwrap(); |
| 1122 |
|
| 1123 |
// THEN array of remarks |
| 1124 |
let expected = r#"[{"description":["foo"]},{"description":["bar"]}]"#; |
| 1125 |
assert_eq!(actual, expected); |
| 1126 |
} |
| 1127 |
|
| 1128 |
#[test] |
| 1129 |
fn test_an_event_deserialize() { |
| 1130 |
// GIVEN an event |
| 1131 |
let expected = r#" |
| 1132 |
{ |
| 1133 |
"eventAction" : "last changed", |
| 1134 |
"eventActor" : "OTHERID-LUNARNIC", |
| 1135 |
"eventDate" : "1991-12-31T23:59:59Z" |
| 1136 |
} |
| 1137 |
"#; |
| 1138 |
|
| 1139 |
// WHEN deserialize |
| 1140 |
let actual = serde_json::from_str::<Event>(expected); |
| 1141 |
|
| 1142 |
// THEN success |
| 1143 |
let actual = actual.unwrap(); |
| 1144 |
actual.event_actor.as_ref().unwrap(); |
| 1145 |
} |
| 1146 |
|
| 1147 |
#[test] |
| 1148 |
fn test_a_public_id_deserialize() { |
| 1149 |
// GIVEN public id |
| 1150 |
let expected = r#" |
| 1151 |
{ |
| 1152 |
"type":"IANA Registrar ID", |
| 1153 |
"identifier":"1" |
| 1154 |
} |
| 1155 |
"#; |
| 1156 |
|
| 1157 |
// WHEN deserialize |
| 1158 |
let actual = serde_json::from_str::<PublicId>(expected); |
| 1159 |
|
| 1160 |
// THEN |
| 1161 |
let _actual = actual.unwrap(); |
| 1162 |
} |
| 1163 |
|
| 1164 |
#[test] |
| 1165 |
fn test_set_self_link() { |
| 1166 |
// GIVEN no self links |
| 1167 |
let mut oc = ObjectCommon::domain() |
| 1168 |
.links(vec![ |
| 1169 |
Link::builder() |
| 1170 |
.href("http://bar.example") |
| 1171 |
.value("http://bar.example") |
| 1172 |
.rel("unknown") |
| 1173 |
.build(), |
| 1174 |
]) |
| 1175 |
.build(); |
| 1176 |
|
| 1177 |
// WHEN set self link |
| 1178 |
oc = oc.with_self_link( |
| 1179 |
Link::builder() |
| 1180 |
.href("http://foo.example") |
| 1181 |
.value("http://foo.example") |
| 1182 |
.rel("unknown") |
| 1183 |
.build(), |
| 1184 |
); |
| 1185 |
|
| 1186 |
// THEN it is the only one |
| 1187 |
assert_eq!( |
| 1188 |
oc.links |
| 1189 |
.expect("links are empty") |
| 1190 |
.iter() |
| 1191 |
.filter(|link| link.is_relation("self")) |
| 1192 |
.count(), |
| 1193 |
1 |
| 1194 |
); |
| 1195 |
} |
| 1196 |
|
| 1197 |
#[test] |
| 1198 |
fn test_set_self_link_on_no_links() { |
| 1199 |
// GIVEN no links |
| 1200 |
let mut oc = ObjectCommon::domain().build(); |
| 1201 |
|
| 1202 |
// WHEN set self link |
| 1203 |
oc = oc.with_self_link( |
| 1204 |
Link::builder() |
| 1205 |
.href("http://foo.example") |
| 1206 |
.value("http://foo.example") |
| 1207 |
.rel("unknown") |
| 1208 |
.build(), |
| 1209 |
); |
| 1210 |
|
| 1211 |
// THEN then it is the only one |
| 1212 |
assert_eq!( |
| 1213 |
oc.links |
| 1214 |
.expect("links are empty") |
| 1215 |
.iter() |
| 1216 |
.filter(|link| link.is_relation("self")) |
| 1217 |
.count(), |
| 1218 |
1 |
| 1219 |
); |
| 1220 |
} |
| 1221 |
|
| 1222 |
#[test] |
| 1223 |
fn test_set_self_link_when_one_exists() { |
| 1224 |
// GIVEN one self link |
| 1225 |
let mut oc = ObjectCommon::domain() |
| 1226 |
.links(vec![ |
| 1227 |
Link::builder() |
| 1228 |
.href("http://bar.example") |
| 1229 |
.value("http://bar.example") |
| 1230 |
.rel("self") |
| 1231 |
.build(), |
| 1232 |
]) |
| 1233 |
.build(); |
| 1234 |
|
| 1235 |
// WHEN set self link |
| 1236 |
oc = oc.with_self_link( |
| 1237 |
Link::builder() |
| 1238 |
.href("http://foo.example") |
| 1239 |
.value("http://foo.example") |
| 1240 |
.rel("unknown") |
| 1241 |
.build(), |
| 1242 |
); |
| 1243 |
|
| 1244 |
// THEN |
| 1245 |
// new link is in |
| 1246 |
assert_eq!( |
| 1247 |
oc.links |
| 1248 |
.as_ref() |
| 1249 |
.expect("links are empty") |
| 1250 |
.iter() |
| 1251 |
.filter(|link| link.is_relation("self") |
| 1252 |
&& link.href.as_ref().unwrap() == "http://foo.example") |
| 1253 |
.count(), |
| 1254 |
1 |
| 1255 |
); |
| 1256 |
// all self links count == 1 |
| 1257 |
assert_eq!( |
| 1258 |
oc.links |
| 1259 |
.as_ref() |
| 1260 |
.expect("links are empty") |
| 1261 |
.iter() |
| 1262 |
.filter(|link| link.is_relation("self")) |
| 1263 |
.count(), |
| 1264 |
1 |
| 1265 |
); |
| 1266 |
} |
| 1267 |
|
| 1268 |
#[test] |
| 1269 |
fn test_description_as_pgs() { |
| 1270 |
// GIVEN notice |
| 1271 |
let notice = Notice::builder() |
| 1272 |
.description_entry("This is a test") |
| 1273 |
.description_entry("that should be consolidated.") |
| 1274 |
.description_entry("SEPARATE LINE") |
| 1275 |
.description_entry("Another line.") |
| 1276 |
.build(); |
| 1277 |
|
| 1278 |
// WHEN converted to pgs |
| 1279 |
let actual = notice.description_as_pgs(); |
| 1280 |
|
| 1281 |
// THEN |
| 1282 |
assert_eq!( |
| 1283 |
actual.first().unwrap(), |
| 1284 |
"This is a test that should be consolidated." |
| 1285 |
); |
| 1286 |
assert_eq!(actual.get(1).unwrap(), "SEPARATE LINE"); |
| 1287 |
assert_eq!(actual.get(2).unwrap(), "Another line."); |
| 1288 |
} |
| 1289 |
|
| 1290 |
#[test] |
| 1291 |
fn test_description_as_pgs_empty_line_breaks() { |
| 1292 |
// GIVEN |
| 1293 |
let notice = Notice::builder() |
| 1294 |
.description_entry("First paragraph.") |
| 1295 |
.description_entry("") |
| 1296 |
.description_entry("Second paragraph.") |
| 1297 |
.build(); |
| 1298 |
|
| 1299 |
// WHEN |
| 1300 |
let actual = notice.description_as_pgs(); |
| 1301 |
|
| 1302 |
// THEN |
| 1303 |
assert_eq!(actual.len(), 2); |
| 1304 |
assert_eq!(actual[0], "First paragraph."); |
| 1305 |
assert_eq!(actual[1], "Second paragraph."); |
| 1306 |
} |
| 1307 |
|
| 1308 |
#[test] |
| 1309 |
fn test_description_as_pgs_ascii_table_lines() { |
| 1310 |
// GIVEN |
| 1311 |
let notice = Notice::builder() |
| 1312 |
.description_entry("Peering info:") |
| 1313 |
.description_entry("++++++++++++++++++++++++++++++++++++++++++++++++") |
| 1314 |
.description_entry("| AS3333 RIPE-NCC-AS |") |
| 1315 |
.description_entry("| RIPE NCC |") |
| 1316 |
.description_entry("++++++++++++++++++++++++++++++++++++++++++++++++") |
| 1317 |
.description_entry("Contact us at [email protected].") |
| 1318 |
.build(); |
| 1319 |
|
| 1320 |
// WHEN |
| 1321 |
let actual = notice.description_as_pgs(); |
| 1322 |
|
| 1323 |
// THEN |
| 1324 |
assert_eq!(actual[0], "Peering info:"); |
| 1325 |
assert_eq!(actual[1], "+++..."); |
| 1326 |
assert_eq!(actual[2], "AS3333 RIPE-NCC-AS"); |
| 1327 |
assert_eq!(actual[3], "RIPE NCC"); |
| 1328 |
assert_eq!(actual[4], "+++..."); |
| 1329 |
assert_eq!(actual[5], "Contact us at [email protected]."); |
| 1330 |
} |
| 1331 |
|
| 1332 |
#[test] |
| 1333 |
fn test_description_as_pgs_merge_limit_by_length() { |
| 1334 |
// GIVEN |
| 1335 |
let notice = Notice::builder() |
| 1336 |
.description_entry("short line one") |
| 1337 |
.description_entry("short line two") |
| 1338 |
.description_entry("short line three") |
| 1339 |
.description_entry("short line four") |
| 1340 |
.description_entry("short line five") |
| 1341 |
.description_entry("short line six") |
| 1342 |
.description_entry("short line seven") |
| 1343 |
.description_entry("short line eight") |
| 1344 |
.description_entry("short line nine") |
| 1345 |
.description_entry("short line ten") |
| 1346 |
.description_entry("short line eleven") |
| 1347 |
.description_entry("that ends with a period.") |
| 1348 |
.build(); |
| 1349 |
|
| 1350 |
// WHEN |
| 1351 |
let actual = notice.description_as_pgs(); |
| 1352 |
|
| 1353 |
// THEN |
| 1354 |
assert!( |
| 1355 |
actual.len() >= 2, |
| 1356 |
"Should split into at least 2 paragraphs, got {}", |
| 1357 |
actual.len() |
| 1358 |
); |
| 1359 |
let first_pg = &actual[0]; |
| 1360 |
assert!( |
| 1361 |
first_pg.len() <= 500 || first_pg.ends_with('.'), |
| 1362 |
"First paragraph should be <= 500 chars or end with period, got len={}", |
| 1363 |
first_pg.len() |
| 1364 |
); |
| 1365 |
} |
| 1366 |
|
| 1367 |
#[test] |
| 1368 |
fn test_description_as_pgs_merge_limit_by_line_count() { |
| 1369 |
// GIVEN |
| 1370 |
let mut builder = Notice::builder(); |
| 1371 |
for i in 0..15 { |
| 1372 |
builder = builder.description_entry(format!("line number {}", i)); |
| 1373 |
} |
| 1374 |
builder = builder.description_entry("final line with period."); |
| 1375 |
let notice = builder.build(); |
| 1376 |
|
| 1377 |
// WHEN |
| 1378 |
let actual = notice.description_as_pgs(); |
| 1379 |
|
| 1380 |
// THEN |
| 1381 |
assert!( |
| 1382 |
actual.len() >= 2, |
| 1383 |
"Should split into at least 2 paragraphs after 10 merged lines, got {}", |
| 1384 |
actual.len() |
| 1385 |
); |
| 1386 |
} |
| 1387 |
|
| 1388 |
#[test] |
| 1389 |
fn test_description_as_pgs_ripe_peering_style() { |
| 1390 |
// GIVEN |
| 1391 |
let mut builder = Notice::builder(); |
| 1392 |
builder = builder |
| 1393 |
.description_entry("Reseaux IP Europeens Network Coordination Centre (RIPE NCC)"); |
| 1394 |
builder = builder.description_entry(""); |
| 1395 |
builder = |
| 1396 |
builder.description_entry("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); |
| 1397 |
builder = builder.description_entry("| AS3333 RIPE-NCC-AS |"); |
| 1398 |
builder = builder.description_entry("| RIPE NCC (Network Coordination Centre) |"); |
| 1399 |
builder = builder.description_entry("| |"); |
| 1400 |
builder = |
| 1401 |
builder.description_entry("| RIPE NCC (AS3333) operates an open peering policy: |"); |
| 1402 |
builder = |
| 1403 |
builder.description_entry("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); |
| 1404 |
builder = builder.description_entry("| PRIVATE-TRANSIT-V4 |"); |
| 1405 |
builder = builder.description_entry("| Private transit peering |"); |
| 1406 |
builder = builder.description_entry("| TeliaSonera |"); |
| 1407 |
let notice = builder.build(); |
| 1408 |
|
| 1409 |
// WHEN |
| 1410 |
let actual = notice.description_as_pgs(); |
| 1411 |
|
| 1412 |
// THEN |
| 1413 |
assert!( |
| 1414 |
actual.len() >= 3, |
| 1415 |
"Should produce multiple paragraphs, got {}", |
| 1416 |
actual.len() |
| 1417 |
); |
| 1418 |
assert_eq!( |
| 1419 |
actual[0], |
| 1420 |
"Reseaux IP Europeens Network Coordination Centre (RIPE NCC)" |
| 1421 |
); |
| 1422 |
assert!(actual.iter().any(|p| p == "+++...")); |
| 1423 |
assert!(actual.iter().any(|p| p.contains("AS3333"))); |
| 1424 |
} |
| 1425 |
|
| 1426 |
#[test] |
| 1427 |
fn test_description_as_pgs_no_description() { |
| 1428 |
// GIVEN |
| 1429 |
let notice = Notice::builder().build(); |
| 1430 |
|
| 1431 |
// WHEN |
| 1432 |
let actual = notice.description_as_pgs(); |
| 1433 |
|
| 1434 |
// THEN |
| 1435 |
assert!(actual.is_empty()); |
| 1436 |
} |
| 1437 |
|
| 1438 |
#[test] |
| 1439 |
fn test_description_as_pgs_all_table_lines() { |
| 1440 |
// GIVEN |
| 1441 |
let notice = Notice::builder() |
| 1442 |
.description_entry("++++++++++++++++++++++++++++++++++++++++++++++++") |
| 1443 |
.description_entry("| Header 1 | Header 2 |") |
| 1444 |
.description_entry("++++++++++++++++++++++++++++++++++++++++++++++++") |
| 1445 |
.description_entry("| Data 1 | Data 2 |") |
| 1446 |
.description_entry("++++++++++++++++++++++++++++++++++++++++++++++++") |
| 1447 |
.build(); |
| 1448 |
|
| 1449 |
// WHEN |
| 1450 |
let actual = notice.description_as_pgs(); |
| 1451 |
|
| 1452 |
// THEN |
| 1453 |
assert_eq!(actual.len(), 5); |
| 1454 |
assert_eq!(actual[0], "+++..."); |
| 1455 |
assert_eq!(actual[1], "Header 1 | Header 2"); |
| 1456 |
assert_eq!(actual[2], "+++..."); |
| 1457 |
assert_eq!(actual[3], "Data 1 | Data 2"); |
| 1458 |
assert_eq!(actual[4], "+++..."); |
| 1459 |
} |
| 1460 |
} |