| 1 |
//! RFC 9537. |
| 2 |
//! |
| 3 |
//! Using the builders to create redactions is recommended. |
| 4 |
//! The following is an example: |
| 5 |
//! |
| 6 |
//! ```rust |
| 7 |
//! use icann_rdap_common::prelude::*; |
| 8 |
//! use icann_rdap_common::response::redacted::*; |
| 9 |
//! |
| 10 |
//! let r_name = Name::builder().type_field("Tech Email").build(); |
| 11 |
//! let redacted = Redacted::builder() |
| 12 |
//! .name(r_name) |
| 13 |
//! .build(); |
| 14 |
//! |
| 15 |
//! let domain = Domain::builder() |
| 16 |
//! .ldh_name("foo.example.com") |
| 17 |
//! .redacted(vec![redacted]) |
| 18 |
//! .build(); |
| 19 |
//! ``` |
| 20 |
//! |
| 21 |
//! To get the data out of redactions, using the getters is recommended. |
| 22 |
//! |
| 23 |
//! ```rust |
| 24 |
//! use icann_rdap_common::response::redacted::*; |
| 25 |
//! |
| 26 |
//! let r_name = Name::builder().type_field("Tech Email").build(); |
| 27 |
//! let redacted = Redacted::builder() |
| 28 |
//! .name(r_name) |
| 29 |
//! .build(); |
| 30 |
//! |
| 31 |
//! // get the data from the redaction. |
| 32 |
//! let name_type = redacted.name().type_field(); |
| 33 |
//! ``` |
| 34 |
//! |
| 35 |
use { |
| 36 |
crate::prelude::RdapResponse, |
| 37 |
buildstructor::Builder, |
| 38 |
serde::{Deserialize, Serialize}, |
| 39 |
std::{any::TypeId, fmt}, |
| 40 |
}; |
| 41 |
|
| 42 |
use crate::check::Checks; |
| 43 |
|
| 44 |
/// Redacted registered name. |
| 45 |
#[derive(Builder, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 46 |
pub struct Name { |
| 47 |
#[serde(rename = "description")] |
| 48 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 49 |
pub description: Option<String>, |
| 50 |
|
| 51 |
#[serde(rename = "type")] |
| 52 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 53 |
pub type_field: Option<String>, |
| 54 |
} |
| 55 |
|
| 56 |
impl Name { |
| 57 |
/// Get the description. |
| 58 |
pub fn description(&self) -> Option<&str> { |
| 59 |
self.description.as_deref() |
| 60 |
} |
| 61 |
|
| 62 |
/// Get the redaction type. |
| 63 |
pub fn type_field(&self) -> Option<&str> { |
| 64 |
self.type_field.as_deref() |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/// Redaction reason. |
| 69 |
#[derive(Builder, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)] |
| 70 |
pub struct Reason { |
| 71 |
#[serde(rename = "description")] |
| 72 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 73 |
pub description: Option<String>, |
| 74 |
|
| 75 |
#[serde(rename = "type")] |
| 76 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 77 |
pub type_field: Option<String>, |
| 78 |
} |
| 79 |
|
| 80 |
impl Reason { |
| 81 |
/// Get the description. |
| 82 |
pub fn description(&self) -> Option<&str> { |
| 83 |
self.description.as_deref() |
| 84 |
} |
| 85 |
|
| 86 |
/// Get the redaction type. |
| 87 |
pub fn type_field(&self) -> Option<&str> { |
| 88 |
self.type_field.as_deref() |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
impl std::fmt::Display for Reason { |
| 93 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 94 |
let output = self.description.clone().unwrap_or_default(); |
| 95 |
write!(f, "{}", output) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 100 |
#[serde(rename_all = "camelCase")] |
| 101 |
#[derive(Default)] |
| 102 |
pub enum Method { |
| 103 |
#[default] |
| 104 |
Removal, |
| 105 |
EmptyValue, |
| 106 |
PartialValue, |
| 107 |
ReplacementValue, |
| 108 |
} |
| 109 |
|
| 110 |
/// RFC 9537 redaction structure. |
| 111 |
#[derive(Builder, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 112 |
pub struct Redacted { |
| 113 |
#[serde[rename = "name"]] |
| 114 |
pub name: Name, |
| 115 |
|
| 116 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 117 |
#[serde(rename = "reason")] |
| 118 |
pub reason: Option<Reason>, |
| 119 |
|
| 120 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 121 |
#[serde(rename = "prePath")] |
| 122 |
pub pre_path: Option<String>, |
| 123 |
|
| 124 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 125 |
#[serde(rename = "postPath")] |
| 126 |
pub post_path: Option<String>, |
| 127 |
|
| 128 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 129 |
#[serde(rename = "pathLang")] |
| 130 |
pub path_lang: Option<String>, |
| 131 |
|
| 132 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 133 |
#[serde(rename = "replacementPath")] |
| 134 |
pub replacement_path: Option<String>, |
| 135 |
|
| 136 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 137 |
#[serde(rename = "method")] |
| 138 |
pub method: Option<Method>, |
| 139 |
} |
| 140 |
|
| 141 |
impl Default for Name { |
| 142 |
fn default() -> Self { |
| 143 |
Self { |
| 144 |
description: Some(String::default()), |
| 145 |
type_field: None, |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
impl fmt::Display for Method { |
| 151 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 152 |
match self { |
| 153 |
Self::Removal => write!(f, "Removal"), |
| 154 |
Self::EmptyValue => write!(f, "EmptyValue"), |
| 155 |
Self::PartialValue => write!(f, "PartialValue"), |
| 156 |
Self::ReplacementValue => write!(f, "ReplacementValue"), |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
impl Redacted { |
| 162 |
/// Get the name. |
| 163 |
pub fn name(&self) -> &Name { |
| 164 |
&self.name |
| 165 |
} |
| 166 |
|
| 167 |
/// Get the reason. |
| 168 |
pub fn reason(&self) -> Option<&Reason> { |
| 169 |
self.reason.as_ref() |
| 170 |
} |
| 171 |
|
| 172 |
/// Get the prePath. |
| 173 |
pub fn pre_path(&self) -> Option<&str> { |
| 174 |
self.pre_path.as_deref() |
| 175 |
} |
| 176 |
|
| 177 |
/// Get the postPath. |
| 178 |
pub fn post_path(&self) -> Option<&str> { |
| 179 |
self.post_path.as_deref() |
| 180 |
} |
| 181 |
|
| 182 |
/// Get the replacementPath. |
| 183 |
pub fn replacement_path(&self) -> Option<&str> { |
| 184 |
self.replacement_path.as_deref() |
| 185 |
} |
| 186 |
|
| 187 |
/// Get the pathLang. |
| 188 |
pub fn path_lang(&self) -> Option<&str> { |
| 189 |
self.path_lang.as_deref() |
| 190 |
} |
| 191 |
|
| 192 |
/// Get the method. |
| 193 |
pub fn method(&self) -> Option<&Method> { |
| 194 |
self.method.as_ref() |
| 195 |
} |
| 196 |
|
| 197 |
/// Get the checks from Redactions. |
| 198 |
pub fn get_checks(&self, _check_params: crate::check::CheckParams<'_>) -> crate::check::Checks { |
| 199 |
Checks { |
| 200 |
rdap_struct: crate::check::RdapStructure::Redacted, |
| 201 |
index: None, |
| 202 |
items: vec![], |
| 203 |
sub_checks: vec![], |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/// Get the type. |
| 208 |
pub fn get_type(&self) -> std::any::TypeId { |
| 209 |
TypeId::of::<Self>() |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
static EMPTY_REDACTED_ARRAY: [Redacted; 0] = []; |
| 214 |
|
| 215 |
/// Get the redactions for an [RdapResponse]. |
| 216 |
pub fn redactions(rdap: &RdapResponse) -> &[Redacted] { |
| 217 |
match rdap { |
| 218 |
RdapResponse::Entity(entity) => { |
| 219 |
entity.object_common.redacted.as_deref().unwrap_or_default() |
| 220 |
} |
| 221 |
RdapResponse::Domain(domain) => { |
| 222 |
domain.object_common.redacted.as_deref().unwrap_or_default() |
| 223 |
} |
| 224 |
RdapResponse::Nameserver(nameserver) => nameserver |
| 225 |
.object_common |
| 226 |
.redacted |
| 227 |
.as_deref() |
| 228 |
.unwrap_or_default(), |
| 229 |
RdapResponse::Autnum(autnum) => { |
| 230 |
autnum.object_common.redacted.as_deref().unwrap_or_default() |
| 231 |
} |
| 232 |
RdapResponse::Network(network) => network |
| 233 |
.object_common |
| 234 |
.redacted |
| 235 |
.as_deref() |
| 236 |
.unwrap_or_default(), |
| 237 |
_ => { |
| 238 |
// RFC 9537 is very underspecified when it comes to anything |
| 239 |
// other than the object classes. |
| 240 |
&EMPTY_REDACTED_ARRAY |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
#[cfg(test)] |
| 246 |
mod tests { |
| 247 |
use super::*; |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn test_redaction_builder() { |
| 251 |
// GIVEN |
| 252 |
let name = Name { |
| 253 |
description: Some("Registry Domain ID".to_string()), |
| 254 |
type_field: None, |
| 255 |
}; |
| 256 |
|
| 257 |
// WHEN |
| 258 |
let redacted = Redacted::builder() |
| 259 |
.name(name) |
| 260 |
.reason(Reason::default()) |
| 261 |
.pre_path("$.handle".to_string()) |
| 262 |
.post_path("$.entities[?(@.roles[0]=='registrant'".to_string()) |
| 263 |
.path_lang("jsonpath".to_string()) |
| 264 |
.replacement_path( |
| 265 |
"$.entities[?(@.roles[0]=='registrant')].vcardArray[1][?(@[0]=='contact-uri')]" |
| 266 |
.to_string(), |
| 267 |
) |
| 268 |
.method(Method::Removal) |
| 269 |
.build(); |
| 270 |
|
| 271 |
// THEN |
| 272 |
assert_eq!( |
| 273 |
redacted.name.description, |
| 274 |
Some("Registry Domain ID".to_string()) |
| 275 |
); |
| 276 |
assert_eq!(redacted.pre_path, Some("$.handle".to_string())); |
| 277 |
assert_eq!( |
| 278 |
redacted.post_path, |
| 279 |
Some("$.entities[?(@.roles[0]=='registrant'".to_string()) |
| 280 |
); |
| 281 |
assert_eq!(redacted.path_lang, Some("jsonpath".to_string())); |
| 282 |
assert_eq!( |
| 283 |
redacted.replacement_path, |
| 284 |
Some( |
| 285 |
"$.entities[?(@.roles[0]=='registrant')].vcardArray[1][?(@[0]=='contact-uri')]" |
| 286 |
.to_string() |
| 287 |
) |
| 288 |
); |
| 289 |
assert_eq!(redacted.method, Some(Method::Removal)); |
| 290 |
} |
| 291 |
|
| 292 |
#[test] |
| 293 |
fn test_redaction_deserialization() { |
| 294 |
// GIVEN |
| 295 |
let expected = r#" |
| 296 |
{ |
| 297 |
"name": { |
| 298 |
"type": "Registry Domain ID" |
| 299 |
}, |
| 300 |
"prePath": "$.handle", |
| 301 |
"pathLang": "jsonpath", |
| 302 |
"postPath": "$.entities[?(@.roles[0]=='registrant'", |
| 303 |
"replacementPath": "$.entities[?(@.roles[0]=='registrant')].vcardArray[1][?(@[0]=='contact-uri')]", |
| 304 |
"method": "removal", |
| 305 |
"reason": { |
| 306 |
"description": "Server policy" |
| 307 |
} |
| 308 |
} |
| 309 |
"#; |
| 310 |
|
| 311 |
// in this one we swap the two fields |
| 312 |
let name = Name { |
| 313 |
type_field: Some("Registry Domain ID".to_string()), |
| 314 |
description: None, |
| 315 |
}; |
| 316 |
|
| 317 |
let reason: Reason = Reason { |
| 318 |
description: Some("Server policy".to_string()), |
| 319 |
type_field: None, |
| 320 |
}; |
| 321 |
|
| 322 |
// WHEN |
| 323 |
// use the builder for most of the fields but not all |
| 324 |
let mut sample_redact: Redacted = Redacted::builder() |
| 325 |
.name(name) |
| 326 |
.pre_path("$.handle".to_string()) |
| 327 |
.path_lang("jsonpath".to_string()) |
| 328 |
.post_path("$.entities[?(@.roles[0]=='registrant'".to_string()) |
| 329 |
.replacement_path( |
| 330 |
"$.entities[?(@.roles[0]=='registrant')].vcardArray[1][?(@[0]=='contact-uri')]" |
| 331 |
.to_string(), |
| 332 |
) |
| 333 |
.build(); |
| 334 |
|
| 335 |
// also make sure we can set the rest |
| 336 |
sample_redact.method = Some(Method::Removal); |
| 337 |
sample_redact.reason = Some(reason); |
| 338 |
|
| 339 |
let actual: Result<Redacted, serde_json::Error> = |
| 340 |
serde_json::from_str::<Redacted>(expected); |
| 341 |
|
| 342 |
// THEN |
| 343 |
let actual: Redacted = actual.unwrap(); |
| 344 |
assert_eq!(actual, sample_redact); // sanity check |
| 345 |
assert_eq!( |
| 346 |
actual.name.type_field, |
| 347 |
Some("Registry Domain ID".to_string()) |
| 348 |
); |
| 349 |
assert_eq!(actual.pre_path, Some("$.handle".to_string())); |
| 350 |
assert_eq!( |
| 351 |
actual.post_path, |
| 352 |
Some("$.entities[?(@.roles[0]=='registrant'".to_string()) |
| 353 |
); |
| 354 |
assert_eq!(actual.path_lang, Some("jsonpath".to_string())); |
| 355 |
assert_eq!( |
| 356 |
actual.replacement_path, |
| 357 |
Some( |
| 358 |
"$.entities[?(@.roles[0]=='registrant')].vcardArray[1][?(@[0]=='contact-uri')]" |
| 359 |
.to_string() |
| 360 |
) |
| 361 |
); |
| 362 |
assert_eq!(actual.method, Some(Method::Removal)); |
| 363 |
} |
| 364 |
} |