| 1 |
//! Types for more lenient processing of invalid RDAP |
| 2 |
|
| 3 |
use std::{borrow::Borrow, fmt::Display, marker::PhantomData, ops::Deref, str::FromStr}; |
| 4 |
|
| 5 |
use { |
| 6 |
serde::{Deserialize, Deserializer, Serialize, Serializer, de::Visitor}, |
| 7 |
serde_json::Number, |
| 8 |
}; |
| 9 |
|
| 10 |
use crate::check::StringListCheck; |
| 11 |
|
| 12 |
/// A type that is suppose to be a vector of strings. |
| 13 |
/// |
| 14 |
/// Provides a choice between a string or a vector of strings for deserialization. |
| 15 |
/// |
| 16 |
/// This type is provided to be lenient with misbehaving RDAP servers that |
| 17 |
/// serve a string when they are suppose to be serving an array of |
| 18 |
/// strings. |
| 19 |
/// |
| 20 |
/// Use one of the From methods for construction. |
| 21 |
/// ```rust |
| 22 |
/// use icann_rdap_common::prelude::*; |
| 23 |
/// |
| 24 |
/// let v = VectorStringish::from(vec!["one".to_string(), "two".to_string()]); |
| 25 |
/// |
| 26 |
/// // or |
| 27 |
/// |
| 28 |
/// let v = VectorStringish::from("one".to_string()); |
| 29 |
/// ```` |
| 30 |
#[derive(Serialize, Clone, Debug, PartialEq, Eq)] |
| 31 |
#[serde(transparent)] |
| 32 |
pub struct VectorStringish { |
| 33 |
vec: Vec<String>, |
| 34 |
#[serde(skip)] |
| 35 |
is_string: bool, |
| 36 |
} |
| 37 |
|
| 38 |
impl<'de> Deserialize<'de> for VectorStringish { |
| 39 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 40 |
where |
| 41 |
D: Deserializer<'de>, |
| 42 |
{ |
| 43 |
deserializer.deserialize_any(VectorStringishVisitor) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
struct VectorStringishVisitor; |
| 48 |
|
| 49 |
impl<'de> Visitor<'de> for VectorStringishVisitor { |
| 50 |
type Value = VectorStringish; |
| 51 |
|
| 52 |
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 53 |
formatter.write_str("expected an array of strings") |
| 54 |
} |
| 55 |
|
| 56 |
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
| 57 |
where |
| 58 |
E: serde::de::Error, |
| 59 |
{ |
| 60 |
Ok(VectorStringish { |
| 61 |
vec: vec![v.to_owned()], |
| 62 |
is_string: true, |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 67 |
where |
| 68 |
A: serde::de::SeqAccess<'de>, |
| 69 |
{ |
| 70 |
let mut v = vec![]; |
| 71 |
loop { |
| 72 |
let n = seq.next_element()?; |
| 73 |
if let Some(s) = n { |
| 74 |
v.push(s); |
| 75 |
} else { |
| 76 |
break; |
| 77 |
} |
| 78 |
} |
| 79 |
Ok(VectorStringish { |
| 80 |
vec: v, |
| 81 |
is_string: false, |
| 82 |
}) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
impl From<String> for VectorStringish { |
| 87 |
fn from(value: String) -> Self { |
| 88 |
Self { |
| 89 |
vec: vec![value], |
| 90 |
is_string: false, |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl From<&str> for VectorStringish { |
| 96 |
fn from(value: &str) -> Self { |
| 97 |
Self { |
| 98 |
vec: vec![value.to_owned()], |
| 99 |
is_string: false, |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl From<Vec<String>> for VectorStringish { |
| 105 |
fn from(value: Vec<String>) -> Self { |
| 106 |
Self { |
| 107 |
vec: value, |
| 108 |
is_string: false, |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
impl From<VectorStringish> for Vec<String> { |
| 114 |
fn from(value: VectorStringish) -> Self { |
| 115 |
value.vec |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
impl From<&VectorStringish> for Vec<String> { |
| 120 |
fn from(value: &VectorStringish) -> Self { |
| 121 |
value.vec.to_owned() |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
impl VectorStringish { |
| 126 |
/// Consumes and converts it to a `Vec<String>`. |
| 127 |
pub fn into_vec(self) -> Vec<String> { |
| 128 |
self.vec |
| 129 |
} |
| 130 |
|
| 131 |
/// Gets a reference to the underlying `Vec<String>`. |
| 132 |
pub fn vec(&self) -> &Vec<String> { |
| 133 |
&self.vec |
| 134 |
} |
| 135 |
|
| 136 |
/// Returns true if the deserialization was as a string. |
| 137 |
pub fn is_string(&self) -> bool { |
| 138 |
self.is_string |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
impl StringListCheck for VectorStringish { |
| 143 |
fn is_empty_or_any_empty_or_whitespace(&self) -> bool { |
| 144 |
self.vec().is_empty_or_any_empty_or_whitespace() |
| 145 |
} |
| 146 |
|
| 147 |
fn is_ldh_string_list(&self) -> bool { |
| 148 |
self.vec().is_ldh_string_list() |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/// Returns `Some(VectorStringish)` if the vector is not empty; otherwise, `None`. |
| 153 |
pub fn to_opt_vectorstringish(vec: Vec<String>) -> Option<VectorStringish> { |
| 154 |
(!vec.is_empty()).then_some(VectorStringish::from(vec)) |
| 155 |
} |
| 156 |
|
| 157 |
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)] |
| 158 |
#[serde(untagged)] |
| 159 |
enum StringishInner { |
| 160 |
/// Valid RDAP. |
| 161 |
String(String), |
| 162 |
|
| 163 |
/// Invalid RDAP. |
| 164 |
Bool(bool), |
| 165 |
|
| 166 |
/// Invalid RDAP. |
| 167 |
Number(Number), |
| 168 |
} |
| 169 |
|
| 170 |
/// A type that is suppose to be a string. |
| 171 |
/// |
| 172 |
/// This type is provided to be lenient with misbehaving RDAP servers that |
| 173 |
/// serve a boolean or number when they are suppose to be serving a string. |
| 174 |
/// |
| 175 |
/// Use one of the From methods for construction. |
| 176 |
/// ```rust |
| 177 |
/// use icann_rdap_common::prelude::*; |
| 178 |
/// |
| 179 |
/// let v = Stringish::from("one"); |
| 180 |
/// ```` |
| 181 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 182 |
pub struct Stringish { |
| 183 |
value: String, |
| 184 |
is_number: bool, |
| 185 |
is_bool: bool, |
| 186 |
} |
| 187 |
|
| 188 |
impl Serialize for Stringish { |
| 189 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 190 |
where |
| 191 |
S: Serializer, |
| 192 |
{ |
| 193 |
serializer.serialize_str(&self.value) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
impl<'de> Deserialize<'de> for Stringish { |
| 198 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 199 |
where |
| 200 |
D: Deserializer<'de>, |
| 201 |
{ |
| 202 |
let inner = StringishInner::deserialize(deserializer)?; |
| 203 |
let (value, is_number, is_bool) = match inner { |
| 204 |
StringishInner::String(s) => (s, false, false), |
| 205 |
StringishInner::Bool(b) => (b.to_string(), false, true), |
| 206 |
StringishInner::Number(n) => (n.to_string(), true, false), |
| 207 |
}; |
| 208 |
Ok(Stringish { |
| 209 |
value, |
| 210 |
is_number, |
| 211 |
is_bool, |
| 212 |
}) |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
impl From<String> for Stringish { |
| 217 |
fn from(value: String) -> Self { |
| 218 |
Self { |
| 219 |
value, |
| 220 |
is_number: false, |
| 221 |
is_bool: false, |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
impl From<&str> for Stringish { |
| 227 |
fn from(value: &str) -> Self { |
| 228 |
Self { |
| 229 |
value: value.to_owned(), |
| 230 |
is_number: false, |
| 231 |
is_bool: false, |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
impl From<Stringish> for String { |
| 237 |
fn from(value: Stringish) -> Self { |
| 238 |
value.value |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
impl Display for Stringish { |
| 243 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 244 |
write!(f, "{}", self.value) |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
impl Deref for Stringish { |
| 249 |
type Target = str; |
| 250 |
|
| 251 |
fn deref(&self) -> &Self::Target { |
| 252 |
&self.value |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
impl AsRef<str> for Stringish { |
| 257 |
fn as_ref(&self) -> &str { |
| 258 |
&self.value |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
impl Borrow<str> for Stringish { |
| 263 |
fn borrow(&self) -> &str { |
| 264 |
&self.value |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
impl PartialEq<str> for Stringish { |
| 269 |
fn eq(&self, other: &str) -> bool { |
| 270 |
self.value == other |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
impl PartialEq<&str> for Stringish { |
| 275 |
fn eq(&self, other: &&str) -> bool { |
| 276 |
self.value == *other |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
impl PartialEq<String> for Stringish { |
| 281 |
fn eq(&self, other: &String) -> bool { |
| 282 |
self.value == *other |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
impl Stringish { |
| 287 |
/// Returns true if the deserialization was as a number. |
| 288 |
pub fn is_number(&self) -> bool { |
| 289 |
self.is_number |
| 290 |
} |
| 291 |
|
| 292 |
/// Returns true if the deserialization was as a boolean. |
| 293 |
pub fn is_bool(&self) -> bool { |
| 294 |
self.is_bool |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 299 |
#[serde(untagged)] |
| 300 |
enum BoolishInner { |
| 301 |
/// Valid RDAP. |
| 302 |
Bool(bool), |
| 303 |
|
| 304 |
/// Invalid RDAP. |
| 305 |
String(String), |
| 306 |
} |
| 307 |
|
| 308 |
/// A type that is suppose to be a boolean. |
| 309 |
/// |
| 310 |
/// Provides a choice between a boolean or a string representation of a boolean for deserialization. |
| 311 |
/// |
| 312 |
/// This type is provided to be lenient with misbehaving RDAP servers that |
| 313 |
/// serve a string representation of a boolean when they are suppose to be serving a boolean |
| 314 |
/// |
| 315 |
/// Use one of the From methods for construction. |
| 316 |
/// ```rust |
| 317 |
/// use icann_rdap_common::prelude::*; |
| 318 |
/// |
| 319 |
/// let v = Boolish::from(true); |
| 320 |
/// ```` |
| 321 |
/// |
| 322 |
/// When converting from a string (as would happen with deserialization), |
| 323 |
/// the values "true", "t", "yes", and "y" (case-insensitive with whitespace trimmed) |
| 324 |
/// will be true, all other values will be false. |
| 325 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 326 |
#[serde(transparent)] |
| 327 |
pub struct Boolish { |
| 328 |
inner: BoolishInner, |
| 329 |
} |
| 330 |
|
| 331 |
impl From<bool> for Boolish { |
| 332 |
fn from(value: bool) -> Self { |
| 333 |
Self { |
| 334 |
inner: BoolishInner::Bool(value), |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
impl Display for Boolish { |
| 340 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 341 |
write!(f, "{}", self.into_bool()) |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
impl Boolish { |
| 346 |
/// Converts to a bool. |
| 347 |
pub fn into_bool(&self) -> bool { |
| 348 |
match &self.inner { |
| 349 |
BoolishInner::Bool(value) => *value, |
| 350 |
BoolishInner::String(value) => Boolish::is_true(value), |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
/// Returns true if the deserialization was as a string. |
| 355 |
pub fn is_string(&self) -> bool { |
| 356 |
match &self.inner { |
| 357 |
BoolishInner::Bool(_) => false, |
| 358 |
BoolishInner::String(_) => true, |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
fn is_true(value: &str) -> bool { |
| 363 |
let s = value.trim().to_lowercase(); |
| 364 |
s == "true" || s == "t" || s == "yes" || s == "y" |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 369 |
#[serde(untagged)] |
| 370 |
enum NumberishInner { |
| 371 |
/// Valid RDAP. |
| 372 |
Number(Number), |
| 373 |
|
| 374 |
/// Invalid RDAP. |
| 375 |
String(String), |
| 376 |
} |
| 377 |
|
| 378 |
/// A type that is suppose to be a number. |
| 379 |
/// |
| 380 |
/// Provides a choice between a number or a string representation of a number for deserialization. |
| 381 |
/// |
| 382 |
/// This type is provided to be lenient with misbehaving RDAP servers that |
| 383 |
/// serve a string representation of a number when they are suppose to be serving a number. |
| 384 |
/// |
| 385 |
/// Use the From methods for construction. |
| 386 |
/// ```rust |
| 387 |
/// use icann_rdap_common::prelude::*; |
| 388 |
/// |
| 389 |
/// let v = Numberish::from(123); |
| 390 |
/// ```` |
| 391 |
/// |
| 392 |
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] |
| 393 |
#[serde(transparent)] |
| 394 |
pub struct Numberish<T> { |
| 395 |
inner: NumberishInner, |
| 396 |
phantom: PhantomData<T>, |
| 397 |
} |
| 398 |
|
| 399 |
impl<T> From<T> for Numberish<T> |
| 400 |
where |
| 401 |
Number: From<T>, |
| 402 |
{ |
| 403 |
fn from(value: T) -> Self { |
| 404 |
Self { |
| 405 |
inner: NumberishInner::Number(Number::from(value)), |
| 406 |
phantom: PhantomData, |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
impl<T> Display for Numberish<T> |
| 412 |
where |
| 413 |
Number: From<T>, |
| 414 |
{ |
| 415 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 416 |
write!( |
| 417 |
f, |
| 418 |
"{}", |
| 419 |
self.as_u64() |
| 420 |
.map_or("RANGE_ERROR".to_string(), |u| u.to_string()) |
| 421 |
) |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
impl<T> Numberish<T> |
| 426 |
where |
| 427 |
Number: From<T>, |
| 428 |
{ |
| 429 |
/// Returns true if the deserialization was as a string. |
| 430 |
pub fn is_string(&self) -> bool { |
| 431 |
match &self.inner { |
| 432 |
NumberishInner::Number(_) => false, |
| 433 |
NumberishInner::String(_) => true, |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
/// If the `Number` is an integer, represent it as u64 if possible. Returns None otherwise. |
| 438 |
pub fn as_u64(&self) -> Option<u64> { |
| 439 |
match &self.inner { |
| 440 |
NumberishInner::Number(n) => n.as_u64(), |
| 441 |
NumberishInner::String(s) => Number::from_str(s).ok()?.as_u64(), |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
/// If the `Number` is an integer, represent it as u32 if possible. Returns None otherwise. |
| 446 |
pub fn as_u32(&self) -> Option<u32> { |
| 447 |
match &self.inner { |
| 448 |
NumberishInner::Number(n) => n.as_u64()?.try_into().ok(), |
| 449 |
NumberishInner::String(s) => Number::from_str(s).ok()?.as_u64()?.try_into().ok(), |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
/// If the `Number` is an integer, represent it as u16 if possible. Returns None otherwise. |
| 454 |
pub fn as_u16(&self) -> Option<u16> { |
| 455 |
match &self.inner { |
| 456 |
NumberishInner::Number(n) => n.as_u64()?.try_into().ok(), |
| 457 |
NumberishInner::String(s) => Number::from_str(s).ok()?.as_u64()?.try_into().ok(), |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
/// If the `Number` is an integer, represent it as u8 if possible. Returns None otherwise. |
| 462 |
pub fn as_u8(&self) -> Option<u8> { |
| 463 |
match &self.inner { |
| 464 |
NumberishInner::Number(n) => n.as_u64()?.try_into().ok(), |
| 465 |
NumberishInner::String(s) => Number::from_str(s).ok()?.as_u64()?.try_into().ok(), |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
#[cfg(test)] |
| 471 |
mod tests { |
| 472 |
use { |
| 473 |
super::*, |
| 474 |
serde_json::{from_str, to_string}, |
| 475 |
}; |
| 476 |
|
| 477 |
// |
| 478 |
// VectorStringish tests |
| 479 |
// |
| 480 |
|
| 481 |
#[test] |
| 482 |
fn test_vectorstringish_serialize_many() { |
| 483 |
// GIVEN |
| 484 |
let many = VectorStringish::from(vec!["one".to_string(), "two".to_string()]); |
| 485 |
|
| 486 |
// WHEN |
| 487 |
let serialized = to_string(&many).unwrap(); |
| 488 |
|
| 489 |
// THEN |
| 490 |
assert_eq!(serialized, r#"["one","two"]"#); |
| 491 |
} |
| 492 |
|
| 493 |
#[test] |
| 494 |
fn test_vectorstringish_serialize_one() { |
| 495 |
// GIVEN |
| 496 |
let one = VectorStringish::from("one".to_string()); |
| 497 |
|
| 498 |
// WHEN |
| 499 |
let serialized = to_string(&one).unwrap(); |
| 500 |
|
| 501 |
// THEN |
| 502 |
assert_eq!(serialized, r#"["one"]"#); |
| 503 |
} |
| 504 |
|
| 505 |
#[test] |
| 506 |
fn test_vectorstringish_deserialize_many() { |
| 507 |
// GIVEN |
| 508 |
let json_str = r#"["one","two"]"#; |
| 509 |
|
| 510 |
// WHEN |
| 511 |
let deserialized: VectorStringish = from_str(json_str).unwrap(); |
| 512 |
|
| 513 |
// THEN |
| 514 |
assert_eq!( |
| 515 |
deserialized.vec(), |
| 516 |
&vec!["one".to_string(), "two".to_string()] |
| 517 |
); |
| 518 |
|
| 519 |
// and THEN is not string |
| 520 |
assert!(!deserialized.is_string()) |
| 521 |
} |
| 522 |
|
| 523 |
#[test] |
| 524 |
fn test_vectorstringish_deserialize_one() { |
| 525 |
// GIVEN |
| 526 |
let json_str = r#""one""#; |
| 527 |
|
| 528 |
// WHEN |
| 529 |
let deserialized: VectorStringish = from_str(json_str).unwrap(); |
| 530 |
|
| 531 |
// THEN |
| 532 |
assert_eq!(deserialized.vec(), &vec!["one".to_string()]); |
| 533 |
|
| 534 |
// and THEN is string |
| 535 |
assert!(deserialized.is_string()) |
| 536 |
} |
| 537 |
|
| 538 |
// |
| 539 |
// Stringish tests |
| 540 |
// |
| 541 |
|
| 542 |
#[test] |
| 543 |
fn test_stringish_serialize() { |
| 544 |
// GIVEN |
| 545 |
let a_string = Stringish::from("one"); |
| 546 |
|
| 547 |
// WHEN |
| 548 |
let serialized = to_string(&a_string).unwrap(); |
| 549 |
|
| 550 |
// THEN |
| 551 |
assert_eq!(serialized, r#""one""#); |
| 552 |
} |
| 553 |
|
| 554 |
#[test] |
| 555 |
fn test_string_from_stringish() { |
| 556 |
// GIVEN |
| 557 |
let stringish = Stringish::from("hello"); |
| 558 |
|
| 559 |
// WHEN |
| 560 |
let s = String::from(stringish); |
| 561 |
|
| 562 |
// THEN |
| 563 |
assert_eq!(s, "hello"); |
| 564 |
} |
| 565 |
|
| 566 |
#[test] |
| 567 |
fn test_stringish_deref() { |
| 568 |
// GIVEN |
| 569 |
let stringish = Stringish::from("hello"); |
| 570 |
|
| 571 |
// WHEN |
| 572 |
let s: &str = &stringish; |
| 573 |
|
| 574 |
// THEN |
| 575 |
assert_eq!(s, "hello"); |
| 576 |
assert_eq!(stringish.len(), 5); |
| 577 |
} |
| 578 |
|
| 579 |
#[test] |
| 580 |
fn test_stringish_more_traits() { |
| 581 |
// GIVEN |
| 582 |
let stringish = Stringish::from("hello"); |
| 583 |
|
| 584 |
// THEN AsRef |
| 585 |
assert_eq!(stringish.as_ref(), "hello"); |
| 586 |
|
| 587 |
// THEN Borrow |
| 588 |
assert_eq!(stringish.borrow() as &str, "hello"); |
| 589 |
|
| 590 |
// THEN PartialEq |
| 591 |
assert!(stringish == "hello"); |
| 592 |
assert!(stringish == *"hello"); |
| 593 |
assert!(stringish == *"hello"); |
| 594 |
} |
| 595 |
|
| 596 |
#[test] |
| 597 |
fn test_stringish_serialize_number() { |
| 598 |
// GIVEN |
| 599 |
let deserialized: Stringish = from_str("123").unwrap(); |
| 600 |
|
| 601 |
// WHEN |
| 602 |
let serialized = to_string(&deserialized).unwrap(); |
| 603 |
|
| 604 |
// THEN |
| 605 |
assert_eq!(serialized, r#""123""#); |
| 606 |
} |
| 607 |
|
| 608 |
#[test] |
| 609 |
fn test_stringish_serialize_bool() { |
| 610 |
// GIVEN |
| 611 |
let deserialized: Stringish = from_str("true").unwrap(); |
| 612 |
|
| 613 |
// WHEN |
| 614 |
let serialized = to_string(&deserialized).unwrap(); |
| 615 |
|
| 616 |
// THEN |
| 617 |
assert_eq!(serialized, r#""true""#); |
| 618 |
} |
| 619 |
|
| 620 |
#[test] |
| 621 |
fn test_stringish_deserialize_string() { |
| 622 |
// GIVEN |
| 623 |
let json_str = r#""one""#; |
| 624 |
|
| 625 |
// WHEN |
| 626 |
let deserialized: Stringish = from_str(json_str).unwrap(); |
| 627 |
|
| 628 |
// THEN |
| 629 |
assert_eq!(deserialized, Stringish::from("one")); |
| 630 |
|
| 631 |
// and THEN is not bool |
| 632 |
assert!(!deserialized.is_bool()); |
| 633 |
|
| 634 |
// and THEN is not number |
| 635 |
assert!(!deserialized.is_number()); |
| 636 |
} |
| 637 |
|
| 638 |
#[test] |
| 639 |
fn test_stringish_deserialize_bool() { |
| 640 |
// GIVEN |
| 641 |
let json_str = r#"true"#; |
| 642 |
|
| 643 |
// WHEN |
| 644 |
let deserialized: Stringish = from_str(json_str).unwrap(); |
| 645 |
|
| 646 |
// THEN |
| 647 |
assert_eq!(deserialized.to_string(), "true"); |
| 648 |
|
| 649 |
// and THEN is bool |
| 650 |
assert!(deserialized.is_bool()); |
| 651 |
|
| 652 |
// and THEN is not number |
| 653 |
assert!(!deserialized.is_number()); |
| 654 |
} |
| 655 |
|
| 656 |
#[test] |
| 657 |
fn test_stringish_deserialize_number() { |
| 658 |
// GIVEN |
| 659 |
let json_str = r#"1234"#; |
| 660 |
|
| 661 |
// WHEN |
| 662 |
let deserialized: Stringish = from_str(json_str).unwrap(); |
| 663 |
|
| 664 |
// THEN |
| 665 |
assert_eq!(deserialized.to_string(), "1234"); |
| 666 |
|
| 667 |
// and THEN is not bool |
| 668 |
assert!(!deserialized.is_bool()); |
| 669 |
|
| 670 |
// and THEN is number |
| 671 |
assert!(deserialized.is_number()); |
| 672 |
} |
| 673 |
|
| 674 |
// |
| 675 |
// Boolish tests |
| 676 |
// |
| 677 |
|
| 678 |
#[test] |
| 679 |
fn test_boolish_serialize_bool() { |
| 680 |
// GIVEN |
| 681 |
let b = Boolish::from(true); |
| 682 |
|
| 683 |
// WHEN |
| 684 |
let serialized = to_string(&b).unwrap(); |
| 685 |
|
| 686 |
// THEN |
| 687 |
assert_eq!(serialized, "true"); |
| 688 |
} |
| 689 |
|
| 690 |
#[test] |
| 691 |
fn test_boolish_deserialize_bool_true() { |
| 692 |
// GIVEN |
| 693 |
let json_str = "true"; |
| 694 |
|
| 695 |
// WHEN |
| 696 |
let deserialized: Boolish = from_str(json_str).unwrap(); |
| 697 |
|
| 698 |
// THEN |
| 699 |
assert!(deserialized.into_bool()); |
| 700 |
assert!(!deserialized.is_string()); |
| 701 |
} |
| 702 |
|
| 703 |
#[test] |
| 704 |
fn test_boolish_deserialize_bool_false() { |
| 705 |
// GIVEN |
| 706 |
let json_str = "false"; |
| 707 |
|
| 708 |
// WHEN |
| 709 |
let deserialized: Boolish = from_str(json_str).unwrap(); |
| 710 |
|
| 711 |
// THEN |
| 712 |
assert!(!deserialized.into_bool()); |
| 713 |
assert!(!deserialized.is_string()); |
| 714 |
} |
| 715 |
|
| 716 |
#[test] |
| 717 |
fn test_boolish_deserialize_string_true() { |
| 718 |
// GIVEN |
| 719 |
let json_str = r#""true""#; |
| 720 |
|
| 721 |
// WHEN |
| 722 |
let deserialized: Boolish = from_str(json_str).unwrap(); |
| 723 |
|
| 724 |
// THEN |
| 725 |
assert!(deserialized.into_bool()); |
| 726 |
assert!(deserialized.is_string()); |
| 727 |
} |
| 728 |
|
| 729 |
#[test] |
| 730 |
fn test_boolish_deserialize_string_false() { |
| 731 |
// GIVEN |
| 732 |
let json_str = r#""false""#; |
| 733 |
|
| 734 |
// WHEN |
| 735 |
let deserialized: Boolish = from_str(json_str).unwrap(); |
| 736 |
|
| 737 |
// THEN |
| 738 |
assert!(!deserialized.into_bool()); |
| 739 |
assert!(deserialized.is_string()); |
| 740 |
} |
| 741 |
|
| 742 |
#[test] |
| 743 |
fn test_boolish_is_true() { |
| 744 |
// GIVEN various true values |
| 745 |
let true_values = ["true", "t", "yes", "y", " True ", " T ", " Yes ", " Y "]; |
| 746 |
|
| 747 |
// THEN all are true |
| 748 |
for value in true_values { |
| 749 |
assert!(Boolish::is_true(value)); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
#[test] |
| 754 |
fn test_boolish_is_false() { |
| 755 |
// GIVEN various false values |
| 756 |
let false_values = ["false", "f", "no", "n", "False", "blah", "1", "0", ""]; |
| 757 |
|
| 758 |
// THEN all are false |
| 759 |
for value in false_values { |
| 760 |
assert!(!Boolish::is_true(value)); |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
#[test] |
| 765 |
fn test_boolish_from_bool() { |
| 766 |
assert!(Boolish::from(true).into_bool()); |
| 767 |
assert!(!Boolish::from(false).into_bool()); |
| 768 |
} |
| 769 |
|
| 770 |
// |
| 771 |
// Numberish Tests |
| 772 |
// |
| 773 |
|
| 774 |
#[test] |
| 775 |
fn test_numberish_serialize_number() { |
| 776 |
// GIVEN a Numberish from a number |
| 777 |
let n = Numberish::<u32>::from(123); |
| 778 |
|
| 779 |
// WHEN serialized |
| 780 |
let serialized = to_string(&n).unwrap(); |
| 781 |
|
| 782 |
// THEN it is the correct string |
| 783 |
assert_eq!(serialized, "123"); |
| 784 |
} |
| 785 |
|
| 786 |
#[test] |
| 787 |
fn test_numberish_deserialize_number() { |
| 788 |
// GIVEN a JSON string representing a number |
| 789 |
let json_str = "123"; |
| 790 |
|
| 791 |
// WHEN deserialized |
| 792 |
let deserialized: Numberish<u32> = from_str(json_str).unwrap(); |
| 793 |
|
| 794 |
// THEN the value is correct and it's not a string |
| 795 |
assert_eq!(deserialized.as_u32(), Some(123)); |
| 796 |
assert!(!deserialized.is_string()); |
| 797 |
} |
| 798 |
|
| 799 |
#[test] |
| 800 |
fn test_numberish_deserialize_string() { |
| 801 |
// GIVEN a JSON string representing a number as a string |
| 802 |
let json_str = r#""123""#; |
| 803 |
|
| 804 |
// WHEN deserialized |
| 805 |
let deserialized: Numberish<u32> = from_str(json_str).unwrap(); |
| 806 |
|
| 807 |
// THEN the value is correct and it's a string |
| 808 |
assert_eq!(deserialized.as_u32(), Some(123)); |
| 809 |
assert!(deserialized.is_string()); |
| 810 |
} |
| 811 |
|
| 812 |
#[test] |
| 813 |
fn test_numberish_as_u64_number() { |
| 814 |
// GIVEN a Numberish from a u64 |
| 815 |
let n = Numberish::from(123u64); |
| 816 |
|
| 817 |
// WHEN as_u64 is called |
| 818 |
let result = n.as_u64(); |
| 819 |
|
| 820 |
// THEN the result is Some(123) |
| 821 |
assert_eq!(result, Some(123)); |
| 822 |
} |
| 823 |
|
| 824 |
#[test] |
| 825 |
fn test_numberish_as_u64_string_invalid() { |
| 826 |
// GIVEN a Numberish from a string that does not represent a u64 |
| 827 |
let n = Numberish { |
| 828 |
inner: NumberishInner::String("abc".to_string()), |
| 829 |
phantom: PhantomData::<u64>, |
| 830 |
}; |
| 831 |
|
| 832 |
// WHEN as_u64 is called |
| 833 |
let result = n.as_u64(); |
| 834 |
|
| 835 |
// THEN the result is None |
| 836 |
assert_eq!(result, None); |
| 837 |
} |
| 838 |
|
| 839 |
#[test] |
| 840 |
fn test_numberish_as_smaller_types() { |
| 841 |
// GIVEN a valid number |
| 842 |
let n = Numberish::from(123u64); |
| 843 |
|
| 844 |
// THEN smaller type conversions work |
| 845 |
assert_eq!(n.as_u32(), Some(123)); |
| 846 |
assert_eq!(n.as_u16(), Some(123)); |
| 847 |
assert_eq!(n.as_u8(), Some(123)); |
| 848 |
|
| 849 |
// GIVEN a number too large |
| 850 |
let n = Numberish::from(u32::MAX as u64 + 1); |
| 851 |
|
| 852 |
// THEN smaller type conversions fail |
| 853 |
assert_eq!(n.as_u32(), None); |
| 854 |
assert_eq!(n.as_u16(), None); |
| 855 |
assert_eq!(n.as_u8(), None); |
| 856 |
|
| 857 |
// GIVEN a valid number string |
| 858 |
let n = Numberish { |
| 859 |
inner: NumberishInner::String("123".to_string()), |
| 860 |
phantom: PhantomData::<u64>, |
| 861 |
}; |
| 862 |
|
| 863 |
// THEN smaller type conversions work |
| 864 |
assert_eq!(n.as_u32(), Some(123)); |
| 865 |
assert_eq!(n.as_u16(), Some(123)); |
| 866 |
assert_eq!(n.as_u8(), Some(123)); |
| 867 |
|
| 868 |
// GIVEN a number string too large |
| 869 |
let n = Numberish { |
| 870 |
inner: NumberishInner::String((u32::MAX as u64 + 1).to_string()), |
| 871 |
phantom: PhantomData::<u64>, |
| 872 |
}; |
| 873 |
|
| 874 |
// THEN smaller type conversions fail |
| 875 |
assert_eq!(n.as_u32(), None); |
| 876 |
} |
| 877 |
|
| 878 |
#[test] |
| 879 |
fn test_numberish_display_number() { |
| 880 |
let n = Numberish::<u32>::from(123); |
| 881 |
assert_eq!(format!("{}", n), "123"); |
| 882 |
} |
| 883 |
|
| 884 |
#[test] |
| 885 |
fn test_numberish_display_string_valid() { |
| 886 |
let n = Numberish { |
| 887 |
inner: NumberishInner::String("123".to_string()), |
| 888 |
phantom: PhantomData::<u32>, |
| 889 |
}; |
| 890 |
assert_eq!(format!("{}", n), "123"); |
| 891 |
} |
| 892 |
|
| 893 |
#[test] |
| 894 |
fn test_numberish_display_string_invalid() { |
| 895 |
let n = Numberish { |
| 896 |
inner: NumberishInner::String("abc".to_string()), |
| 897 |
phantom: PhantomData::<u32>, |
| 898 |
}; |
| 899 |
assert_eq!(format!("{}", n), "RANGE_ERROR"); |
| 900 |
} |
| 901 |
} |