1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
// These functions are only used if the API uses Nullable properties, so allow them to be // dead code. #![allow(dead_code)] #[cfg(feature = "serdejson")] use serde::de::Error as SerdeError; #[cfg(feature = "serdejson")] use serde::de::{Deserialize, DeserializeOwned, Deserializer}; #[cfg(feature = "serdejson")] use serde::ser::{Serialize, Serializer}; use std::mem; /// The Nullable type. Represents a value which may be specified as null on an API. /// Note that this is distinct from a value that is optional and not present! /// /// Nullable implements many of the same methods as the Option type (map, unwrap, etc). #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] pub enum Nullable<T> { /// Null value Null, /// Value is present Present(T), } impl<T> Nullable<T> { ///////////////////////////////////////////////////////////////////////// // Querying the contained values ///////////////////////////////////////////////////////////////////////// /// Returns `true` if the Nullable is a `Present` value. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let x: Nullable<u32> = Nullable::Present(2); /// assert_eq!(x.is_present(), true); /// /// let x: Nullable<u32> = Nullable::Null; /// assert_eq!(x.is_present(), false); /// ``` #[inline] pub fn is_present(&self) -> bool { match *self { Nullable::Present(_) => true, Nullable::Null => false, } } /// Returns `true` if the Nullable is a `Null` value. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let x: Nullable<u32> = Nullable::Present(2); /// assert_eq!(x.is_null(), false); /// /// let x: Nullable<u32> = Nullable::Null; /// assert_eq!(x.is_null(), true); /// ``` #[inline] pub fn is_null(&self) -> bool { !self.is_present() } ///////////////////////////////////////////////////////////////////////// // Adapter for working with references ///////////////////////////////////////////////////////////////////////// /// Converts from `Nullable<T>` to `Nullable<&T>`. /// /// # Examples /// /// Convert an `Nullable<`[`String`]`>` into a `Nullable<`[`usize`]`>`, preserving the original. /// The [`map`] method takes the `self` argument by value, consuming the original, /// so this technique uses `as_ref` to first take a `Nullable` to a reference /// to the value inside the original. /// /// [`map`]: enum.Nullable.html#method.map /// [`String`]: ../../std/string/struct.String.html /// [`usize`]: ../../std/primitive.usize.html /// /// ``` /// # use ::swagger::Nullable; /// /// let num_as_str: Nullable<String> = Nullable::Present("10".to_string()); /// // First, cast `Nullable<String>` to `Nullable<&String>` with `as_ref`, /// // then consume *that* with `map`, leaving `num_as_str` on the stack. /// let num_as_int: Nullable<usize> = num_as_str.as_ref().map(|n| n.len()); /// println!("still can print num_as_str: {:?}", num_as_str); /// ``` #[inline] pub fn as_ref(&self) -> Nullable<&T> { match *self { Nullable::Present(ref x) => Nullable::Present(x), Nullable::Null => Nullable::Null, } } /// Converts from `Nullable<T>` to `Nullable<&mut T>`. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let mut x = Nullable::Present(2); /// match x.as_mut() { /// Nullable::Present(v) => *v = 42, /// Nullable::Null => {}, /// } /// assert_eq!(x, Nullable::Present(42)); /// ``` #[inline] pub fn as_mut(&mut self) -> Nullable<&mut T> { match *self { Nullable::Present(ref mut x) => Nullable::Present(x), Nullable::Null => Nullable::Null, } } ///////////////////////////////////////////////////////////////////////// // Getting to contained values ///////////////////////////////////////////////////////////////////////// /// Unwraps a Nullable, yielding the content of a `Nullable::Present`. /// /// # Panics /// /// Panics if the value is a [`Nullable::Null`] with a custom panic message provided by /// `msg`. /// /// [`Nullable::Null`]: #variant.Null /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let x = Nullable::Present("value"); /// assert_eq!(x.expect("the world is ending"), "value"); /// ``` /// /// ```{.should_panic} /// # use ::swagger::Nullable; /// /// let x: Nullable<&str> = Nullable::Null; /// x.expect("the world is ending"); // panics with `the world is ending` /// ``` #[inline] pub fn expect(self, msg: &str) -> T { match self { Nullable::Present(val) => val, Nullable::Null => expect_failed(msg), } } /// Moves the value `v` out of the `Nullable<T>` if it is `Nullable::Present(v)`. /// /// In general, because this function may panic, its use is discouraged. /// Instead, prefer to use pattern matching and handle the `Nullable::Null` /// case explicitly. /// /// # Panics /// /// Panics if the self value equals [`Nullable::Null`]. /// /// [`Nullable::Null`]: #variant.Null /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let x = Nullable::Present("air"); /// assert_eq!(x.unwrap(), "air"); /// ``` /// /// ```{.should_panic} /// # use ::swagger::Nullable; /// /// let x: Nullable<&str> = Nullable::Null; /// assert_eq!(x.unwrap(), "air"); // fails /// ``` #[inline] pub fn unwrap(self) -> T { match self { Nullable::Present(val) => val, Nullable::Null => panic!("called `Nullable::unwrap()` on a `Nullable::Null` value"), } } /// Returns the contained value or a default. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// assert_eq!(Nullable::Present("car").unwrap_or("bike"), "car"); /// assert_eq!(Nullable::Null.unwrap_or("bike"), "bike"); /// ``` #[inline] pub fn unwrap_or(self, def: T) -> T { match self { Nullable::Present(x) => x, Nullable::Null => def, } } /// Returns the contained value or computes it from a closure. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let k = 10; /// assert_eq!(Nullable::Present(4).unwrap_or_else(|| 2 * k), 4); /// assert_eq!(Nullable::Null.unwrap_or_else(|| 2 * k), 20); /// ``` #[inline] pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T { match self { Nullable::Present(x) => x, Nullable::Null => f(), } } ///////////////////////////////////////////////////////////////////////// // Transforming contained values ///////////////////////////////////////////////////////////////////////// /// Maps a `Nullable<T>` to `Nullable<U>` by applying a function to a contained value. /// /// # Examples /// /// Convert a `Nullable<`[`String`]`>` into a `Nullable<`[`usize`]`>`, consuming the original: /// /// [`String`]: ../../std/string/struct.String.html /// [`usize`]: ../../std/primitive.usize.html /// /// ``` /// # use ::swagger::Nullable; /// /// let maybe_some_string = Nullable::Present(String::from("Hello, World!")); /// // `Nullable::map` takes self *by value*, consuming `maybe_some_string` /// let maybe_some_len = maybe_some_string.map(|s| s.len()); /// /// assert_eq!(maybe_some_len, Nullable::Present(13)); /// ``` #[inline] pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Nullable<U> { match self { Nullable::Present(x) => Nullable::Present(f(x)), Nullable::Null => Nullable::Null, } } /// Applies a function to the contained value (if any), /// or returns a `default` (if not). /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let x = Nullable::Present("foo"); /// assert_eq!(x.map_or(42, |v| v.len()), 3); /// /// let x: Nullable<&str> = Nullable::Null; /// assert_eq!(x.map_or(42, |v| v.len()), 42); /// ``` #[inline] pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U { match self { Nullable::Present(t) => f(t), Nullable::Null => default, } } /// Applies a function to the contained value (if any), /// or computes a `default` (if not). /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let k = 21; /// /// let x = Nullable::Present("foo"); /// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3); /// /// let x: Nullable<&str> = Nullable::Null; /// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42); /// ``` #[inline] pub fn map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U { match self { Nullable::Present(t) => f(t), Nullable::Null => default(), } } /// Transforms the `Nullable<T>` into a [`Result<T, E>`], mapping `Nullable::Present(v)` to /// [`Ok(v)`] and `Nullable::Null` to [`Err(err)`][Err]. /// /// [`Result<T, E>`]: ../../std/result/enum.Result.html /// [`Ok(v)`]: ../../std/result/enum.Result.html#variant.Ok /// [Err]: ../../std/result/enum.Result.html#variant.Err /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let x = Nullable::Present("foo"); /// assert_eq!(x.ok_or(0), Ok("foo")); /// /// let x: Nullable<&str> = Nullable::Null; /// assert_eq!(x.ok_or(0), Err(0)); /// ``` #[inline] pub fn ok_or<E>(self, err: E) -> Result<T, E> { match self { Nullable::Present(v) => Ok(v), Nullable::Null => Err(err), } } /// Transforms the `Nullable<T>` into a [`Result<T, E>`], mapping `Nullable::Present(v)` to /// [`Ok(v)`] and `Nullable::Null` to [`Err(err())`][Err]. /// /// [`Result<T, E>`]: ../../std/result/enum.Result.html /// [`Ok(v)`]: ../../std/result/enum.Result.html#variant.Ok /// [Err]: ../../std/result/enum.Result.html#variant.Err /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let x = Nullable::Present("foo"); /// assert_eq!(x.ok_or_else(|| 0), Ok("foo")); /// /// let x: Nullable<&str> = Nullable::Null; /// assert_eq!(x.ok_or_else(|| 0), Err(0)); /// ``` #[inline] pub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E> { match self { Nullable::Present(v) => Ok(v), Nullable::Null => Err(err()), } } ///////////////////////////////////////////////////////////////////////// // Boolean operations on the values, eager and lazy ///////////////////////////////////////////////////////////////////////// /// Returns `Nullable::Null` if the Nullable is `Nullable::Null`, otherwise returns `optb`. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let x = Nullable::Present(2); /// let y: Nullable<&str> = Nullable::Null; /// assert_eq!(x.and(y), Nullable::Null); /// /// let x: Nullable<u32> = Nullable::Null; /// let y = Nullable::Present("foo"); /// assert_eq!(x.and(y), Nullable::Null); /// /// let x = Nullable::Present(2); /// let y = Nullable::Present("foo"); /// assert_eq!(x.and(y), Nullable::Present("foo")); /// /// let x: Nullable<u32> = Nullable::Null; /// let y: Nullable<&str> = Nullable::Null; /// assert_eq!(x.and(y), Nullable::Null); /// ``` #[inline] pub fn and<U>(self, optb: Nullable<U>) -> Nullable<U> { match self { Nullable::Present(_) => optb, Nullable::Null => Nullable::Null, } } /// Returns `Nullable::Null` if the Nullable is `Nullable::Null`, otherwise calls `f` with the /// wrapped value and returns the result. /// /// Some languages call this operation flatmap. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// fn sq(x: u32) -> Nullable<u32> { Nullable::Present(x * x) } /// fn nope(_: u32) -> Nullable<u32> { Nullable::Null } /// /// assert_eq!(Nullable::Present(2).and_then(sq).and_then(sq), Nullable::Present(16)); /// assert_eq!(Nullable::Present(2).and_then(sq).and_then(nope), Nullable::Null); /// assert_eq!(Nullable::Present(2).and_then(nope).and_then(sq), Nullable::Null); /// assert_eq!(Nullable::Null.and_then(sq).and_then(sq), Nullable::Null); /// ``` #[inline] pub fn and_then<U, F: FnOnce(T) -> Nullable<U>>(self, f: F) -> Nullable<U> { match self { Nullable::Present(x) => f(x), Nullable::Null => Nullable::Null, } } /// Returns the Nullable if it contains a value, otherwise returns `optb`. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let x = Nullable::Present(2); /// let y = Nullable::Null; /// assert_eq!(x.or(y), Nullable::Present(2)); /// /// let x = Nullable::Null; /// let y = Nullable::Present(100); /// assert_eq!(x.or(y), Nullable::Present(100)); /// /// let x = Nullable::Present(2); /// let y = Nullable::Present(100); /// assert_eq!(x.or(y), Nullable::Present(2)); /// /// let x: Nullable<u32> = Nullable::Null; /// let y = Nullable::Null; /// assert_eq!(x.or(y), Nullable::Null); /// ``` #[inline] pub fn or(self, optb: Nullable<T>) -> Nullable<T> { match self { Nullable::Present(_) => self, Nullable::Null => optb, } } /// Returns the Nullable if it contains a value, otherwise calls `f` and /// returns the result. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// fn nobody() -> Nullable<&'static str> { Nullable::Null } /// fn vikings() -> Nullable<&'static str> { Nullable::Present("vikings") } /// /// assert_eq!(Nullable::Present("barbarians").or_else(vikings), /// Nullable::Present("barbarians")); /// assert_eq!(Nullable::Null.or_else(vikings), Nullable::Present("vikings")); /// assert_eq!(Nullable::Null.or_else(nobody), Nullable::Null); /// ``` #[inline] pub fn or_else<F: FnOnce() -> Nullable<T>>(self, f: F) -> Nullable<T> { match self { Nullable::Present(_) => self, Nullable::Null => f(), } } ///////////////////////////////////////////////////////////////////////// // Misc ///////////////////////////////////////////////////////////////////////// /// Takes the value out of the Nullable, leaving a `Nullable::Null` in its place. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let mut x = Nullable::Present(2); /// x.take(); /// assert_eq!(x, Nullable::Null); /// /// let mut x: Nullable<u32> = Nullable::Null; /// x.take(); /// assert_eq!(x, Nullable::Null); /// ``` #[inline] pub fn take(&mut self) -> Nullable<T> { mem::replace(self, Nullable::Null) } } impl<'a, T: Clone> Nullable<&'a T> { /// Maps an `Nullable<&T>` to an `Nullable<T>` by cloning the contents of the /// Nullable. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let x = 12; /// let opt_x = Nullable::Present(&x); /// assert_eq!(opt_x, Nullable::Present(&12)); /// let cloned = opt_x.cloned(); /// assert_eq!(cloned, Nullable::Present(12)); /// ``` pub fn cloned(self) -> Nullable<T> { self.map(|t| t.clone()) } } impl<T: Default> Nullable<T> { /// Returns the contained value or a default /// /// Consumes the `self` argument then, if `Nullable::Present`, returns the contained /// value, otherwise if `Nullable::Null`, returns the default value for that /// type. /// /// # Examples /// /// ``` /// # use ::swagger::Nullable; /// /// let x = Nullable::Present(42); /// assert_eq!(42, x.unwrap_or_default()); /// /// let y: Nullable<i32> = Nullable::Null; /// assert_eq!(0, y.unwrap_or_default()); /// ``` #[inline] pub fn unwrap_or_default(self) -> T { match self { Nullable::Present(x) => x, Nullable::Null => Default::default(), } } } // This is a separate function to reduce the code size of .expect() itself. #[inline(never)] #[cold] fn expect_failed(msg: &str) -> ! { panic!("{}", msg) } ///////////////////////////////////////////////////////////////////////////// // Trait implementations ///////////////////////////////////////////////////////////////////////////// impl<T> Default for Nullable<T> { /// Returns None. #[inline] fn default() -> Nullable<T> { Nullable::Null } } impl<T> From<T> for Nullable<T> { fn from(val: T) -> Nullable<T> { Nullable::Present(val) } } #[cfg(feature = "serdejson")] impl<T> Serialize for Nullable<T> where T: Serialize, { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { match *self { Nullable::Present(ref inner) => serializer.serialize_some(&inner), Nullable::Null => serializer.serialize_none(), } } } #[cfg(feature = "serdejson")] impl<'de, T> Deserialize<'de> for Nullable<T> where T: DeserializeOwned, { fn deserialize<D>(deserializer: D) -> Result<Nullable<T>, D::Error> where D: Deserializer<'de>, { // In order to deserialize a required, but nullable, value, we first have to check whether // the value is present at all. To do this, we deserialize to a serde_json::Value, which // fails if the value is missing, or gives serde_json::Value::Null if the value is present. // If that succeeds as null, we can easily return a Null. // If that succeeds as some value, we deserialize that value and return a Present. // If that errors, we return the error. let presence: Result<::serde_json::Value, _> = ::serde::Deserialize::deserialize(deserializer); match presence { Ok(::serde_json::Value::Null) => Ok(Nullable::Null), Ok(some_value) => ::serde_json::from_value(some_value) .map(Nullable::Present) .map_err(SerdeError::custom), Err(x) => Err(x), } } } /// Serde helper function to create a default `Option<Nullable<T>>` while /// deserializing pub fn default_optional_nullable<T>() -> Option<Nullable<T>> { None } /// Serde helper function to deserialize into an `Option<Nullable<T>>` #[cfg(feature = "serdejson")] pub fn deserialize_optional_nullable<'de, D, T>( deserializer: D, ) -> Result<Option<Nullable<T>>, D::Error> where D: Deserializer<'de>, T: Deserialize<'de>, { Option::<T>::deserialize(deserializer).map(|val| match val { Some(inner) => Some(Nullable::Present(inner)), None => Some(Nullable::Null), }) } #[cfg(test)] #[cfg(feature = "serdejson")] mod serde_tests { use super::*; // Set up: #[derive(Clone, Debug, Deserialize, Serialize)] struct NullableStringStruct { item: Nullable<String>, } #[derive(Clone, Debug, Deserialize, Serialize)] struct OptionalNullableStringStruct { #[serde(deserialize_with = "deserialize_optional_nullable")] #[serde(default = "default_optional_nullable")] #[serde(skip_serializing_if = "Option::is_none")] item: Option<Nullable<String>>, } #[derive(Clone, Debug, Deserialize, Serialize)] struct NullableObjectStruct { item: Nullable<NullableStringStruct>, } #[derive(Clone, Debug, Deserialize, Serialize)] struct OptionalNullableObjectStruct { item: Option<Nullable<NullableStringStruct>>, } // Helper: macro_rules! round_trip { ($type:ty, $string:expr) => { println!("Original: {:?}", $string); let json: ::serde_json::Value = ::serde_json::from_str($string).expect("Deserialization to JSON Value failed"); println!("JSON Value: {:?}", json); let thing: $type = ::serde_json::from_value(json.clone()) .expect("Deserialization to struct failed"); println!("Struct: {:?}", thing); let json_redux: ::serde_json::Value = ::serde_json::to_value(thing.clone()) .expect("Reserialization to JSON Value failed"); println!("JSON Redux: {:?}", json_redux); let string_redux = ::serde_json::to_string(&thing).expect("Reserialziation to JSON String failed"); println!("String Redux: {:?}", string_redux); assert_eq!( $string, string_redux, "Original did not match after round trip" ); }; } // The tests: #[test] fn missing_optionalnullable_value() { let string = "{}"; round_trip!(OptionalNullableStringStruct, string); } #[test] fn null_optionalnullable_value() { let string = "{\"item\":null}"; round_trip!(OptionalNullableStringStruct, string); } #[test] fn string_optionalnullable_value() { let string = "{\"item\":\"abc\"}"; round_trip!(OptionalNullableStringStruct, string); } #[test] fn object_optionalnullable_value() { let string = "{\"item\":{\"item\":\"abc\"}}"; round_trip!(OptionalNullableObjectStruct, string); } #[test] #[should_panic] fn missing_nullable_value() { let string = "{}"; round_trip!(NullableStringStruct, string); } #[test] fn null_nullable_value() { let string = "{\"item\":null}"; round_trip!(NullableStringStruct, string); } #[test] fn string_nullable_value() { let string = "{\"item\":\"abc\"}"; round_trip!(NullableStringStruct, string); } #[test] fn object_nullable_value() { let string = "{\"item\":{\"item\":\"abc\"}}"; round_trip!(NullableObjectStruct, string); } }