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 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
#![doc = include_str!("../README.md")]
mod enpow;
mod extract;
mod helper;
///////////////////////////////////////////////////////////////////////////////////////////////////
/// The `enpow` attribute attached to the target enum derives typical methods for working with
/// variants as known from `Result<T, E>` and `Option<T>`. It supports generics and variants of
/// every type, with named or unnamed fields or no fields attached. Variants with unnamed fields
/// get unwrapped into a tuple, while variants with named fields are transformed into an
/// automatically generated struct named after the enum and variant, i.e. `EnumVariant`. The
/// functions and struct generated inherit the visibility modifier of the target enum.
///
/// In parethesis, the following arguments to `enpow` can be used to specify which methods to
/// generate. Without any arguments, all methods will be generated. The method identifiers are
/// generated from the variant names turned into snake case.
///
/// - `Var`
/// * `fn <variant>(self) -> Option<<inner>>`
/// Returns the inner data, if the enum value is of the expected type, otherwise returns
/// `None`.
/// - `IsVar`
/// * `fn is_<variant>(&self) -> bool`
/// Returns `true`, if the enum value is of the expected type, otherwise returns `false`.
/// * `fn is_<variant>_and(&self, f: impl FnOnce(<ref_inner>) -> bool) -> bool`
/// Returns `true`, if the enum value is of the expected type and the given closure
/// evalutates to `true`, otherwise returns `false`.
/// - `VarAsRef`
/// * `fn <variant>_as_ref(&self) -> Option<<ref_inner>>`
/// Returns a reference to the inner data, if the enum value is of the expected type,
/// otherwise returns `None`.
/// * `fn <variant>_as_mut(&mut self) -> Option<<mut_inner>>`
/// Returns a mutable reference to the inner data, if the enum value is of the expected type,
/// otherwise returns `None`.
/// - `UnwrapVar`
/// * `fn unwrap_<variant>(self) -> <inner>`
/// Returns the inner data, if the enum value is of the expected type, otherwise panics.
/// * `fn unwrap_<variant>_as_ref(self) -> <inner>`
/// Returns a reference to the inner data, if the enum value is of the expected type,
/// otherwise panics.
/// * `fn unwrap_<variant>_as_mut(self) -> <inner>`
/// Returns a mutable reference to the inner data, if the enum value is of the expected type,
/// otherwise panics.
/// * `fn unwrap_<variant>_or(self, default: <inner>) -> <inner>`
/// Returns the inner data, if the enum value is of the expected type, otherwise returns the
/// given default value.
/// * `fn unwrap_<variant>_or_else(self, f: impl FnOnce(Self) -> <inner>) -> <inner>`
/// Returns the inner data, if the enum value is of the expected type, otherwise returns the
/// value that the given closure evaluated to.
/// - `ExpectVar`
/// * `fn expect_<variant>(self, msg: &str) -> <inner>`
/// Returns the inner data, if the enum is of the expected type, otherwise panics with the
/// given error message.
/// * `fn expect_<variant>_as_ref(self, msg: &str) -> <inner>`
/// Returns a reference to the inner data, if the enum is of the expected type, otherwise
/// panics with the given error message.
/// * `fn expect_<variant>_as_mut(self, msg: &str) -> <inner>`
/// Returns a mutable reference to the inner data, if the enum is of the expected type,
/// otherwise panics with the given error message.
/// - `All`
/// * Generates all methods mentioned.
///
/// This example will generate all methods.
/// ```rust
/// # use enpow::enpow;
///
/// #[enpow(All)]
/// #[enpow_derive(Debug, PartialEq)]
/// #[derive(Debug, PartialEq)]
/// enum IpAddress {
/// None,
/// V4(u8, u8, u8, u8),
/// V6(String),
/// Multi {
/// v4: (u8, u8, u8, u8),
/// v6: String,
/// },
/// }
///
/// // fn <variant>()
/// assert_eq!(IpAddress::V4(192, 168, 0, 1).v4(), Some((192, 168, 0, 1)));
/// assert_eq!(IpAddress::V6("::1".into()).v6(), Some("::1".into()));
/// assert_eq!(IpAddress::None.multi(), None);
///
/// // fn is_<variant>()
/// assert_eq!(IpAddress::None.is_none(), true);
/// assert_eq!(IpAddress::V6("::1".into()).is_v4(), false);
///
/// // fn is_<variant>_and()
/// assert_eq!(IpAddress::V4(192, 168, 0, 1).is_v4_and(|ip| *ip.0 == 192), true);
/// assert_eq!(IpAddress::V6("::1".into()).is_v6_and(|ip| *ip == "::"), false);
/// assert_eq!(IpAddress::None.is_v4_and(|_| true), false);
///
/// // fn <variant>_as_ref()
/// assert_eq!(IpAddress::V4(192, 168, 0, 1).v4_as_ref(), Some((&192, &168, &0, &1)));
/// assert_eq!(
/// IpAddress::Multi { v4: (0, 0, 0, 0), v6: "::".into() }.multi_as_ref(),
/// Some(IpAddressMultiRef { v4: &(0, 0, 0, 0), v6: &"::".into() })
/// );
/// assert_eq!(IpAddress::V6("::1".into()).none_as_ref(), None);
///
/// // fn <variant>_as_mut()
/// let mut ip = IpAddress::V4(192, 168, 0, 1);
/// if let Some(v4) = ip.v4_as_mut() {
/// *v4.3 = 2;
/// }
/// assert_eq!(ip, IpAddress::V4(192, 168, 0, 2));
///
/// // fn unwrap_<variant>()
/// assert_eq!(IpAddress::V6("::1".into()).unwrap_v6(), "::1".to_owned());
///
/// // fn unwrap_<variant>_as_ref()
/// assert_eq!(IpAddress::V4(192, 168, 0, 1).unwrap_v4_as_ref(), (&192, &168, &0, &1));
///
/// // fn unwrap_<variant>_as_mut()
/// let mut ip = IpAddress::V4(192, 168, 0, 1);
/// *ip.unwrap_v4_as_mut().3 = 2;
/// assert_eq!(ip, IpAddress::V4(192, 168, 0, 2));
///
/// // fn unwrap_<variant>_or()
/// assert_eq!(IpAddress::V6("::1".into()).unwrap_v6_or("::".into()), "::1".to_owned());
/// assert_eq!(IpAddress::V4(192, 168, 0, 2).unwrap_v6_or("::".into()), "::".to_owned());
///
/// // fn unwrap_<variant>_or_else()
/// assert_eq!(IpAddress::None.unwrap_v4_or_else(|_| (0, 0, 0, 0)), (0, 0, 0, 0));
/// assert_eq!(
/// IpAddress::V6("::1".into()).unwrap_v6_or_else(|_| unreachable!()),
/// "::1".to_owned()
/// );
///
/// // fn expect_<variant>()
/// assert_eq!(IpAddress::V4(192, 168, 0, 1).expect_v4("Expected V4"), (192, 168, 0, 1));
///
/// // fn unwrap_<variant>_as_ref()
/// assert_eq!(
/// IpAddress::V6("::1".into()).expect_v6_as_ref("Unexpected variant"),
/// &"::1".to_owned()
/// );
///
/// // fn unwrap_<variant>_as_mut()
/// let mut ip = IpAddress::V6("::".into());
/// ip.expect_v6_as_mut("Expected V6").push('1');
/// assert_eq!(ip, IpAddress::V6("::1".into()));
/// ```
///
/// <details>
/// <summary>ℹ️ Click to reveal generated code</summary>
///
/// ```rust
/// #[derive(Debug, PartialEq)]
/// enum IpAddress {
/// None,
/// V4(u8, u8, u8, u8),
/// V6(String),
/// Multi { v4: (u8, u8, u8, u8), v6: String },
/// }
///
/// #[allow(unused)]
/// #[derive(Debug, PartialEq)]
/// struct IpAddressMulti {
/// pub v4: (u8, u8, u8, u8),
/// pub v6: String,
/// }
///
/// #[allow(unused)]
/// #[derive(Debug, PartialEq)]
/// #[derive(Clone, Copy)]
/// struct IpAddressMultiRef<'ip_address_multi> {
/// pub v4: &'ip_address_multi (u8, u8, u8, u8),
/// pub v6: &'ip_address_multi String,
/// }
///
/// #[allow(unused)]
/// #[derive(Debug, PartialEq)]
/// struct IpAddressMultiMut<'ip_address_multi> {
/// pub v4: &'ip_address_multi mut (u8, u8, u8, u8),
/// pub v6: &'ip_address_multi mut String,
/// }
///
/// #[automatically_derived]
/// #[allow(unused)]
/// impl IpAddress {
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns `None`.
/// fn none(self) -> Option<()> {
/// match self {
/// IpAddress::None => Some(()),
/// _ => None,
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum value is of the expected type,
/// /// otherwise returns `None`.
/// fn none_as_ref(&self) -> Option<()> {
/// match self {
/// IpAddress::None => Some(()),
/// _ => None,
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum value is of the expected
/// /// type, otherwise returns `None`.
/// fn none_as_mut(&mut self) -> Option<()> {
/// match self {
/// IpAddress::None => Some(()),
/// _ => None,
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// panics.
/// fn unwrap_none(self) -> () {
/// match self {
/// IpAddress::None => (),
/// _ => panic!("Failed unwrapping to IpAddress::None. Unexpected variant"),
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum value is of the expected type,
/// /// otherwise panics.
/// fn unwrap_none_as_ref(&self) -> () {
/// match self {
/// IpAddress::None => (),
/// _ => panic!("Failed unwrapping to IpAddress::None. Unexpected variant"),
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum value is of the expected
/// /// type, otherwise panics.
/// fn unwrap_none_as_mut(&mut self) -> () {
/// match self {
/// IpAddress::None => (),
/// _ => panic!("Failed unwrapping to IpAddress::None. Unexpected variant"),
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns the given default value.
/// fn unwrap_none_or(self, default: ()) -> () {
/// match self {
/// IpAddress::None => (),
/// _ => default,
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns the value that the given closure evaluated to.
/// fn unwrap_none_or_else(self, f: impl FnOnce(Self) -> ()) -> () {
/// match self {
/// IpAddress::None => (),
/// some => f(some),
/// }
/// }
///
/// /// Returns the inner data, if the enum is of the expected type, otherwise panics with
/// /// the given error message.
/// fn expect_none(self, msg: &str) -> () {
/// match self {
/// IpAddress::None => (),
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum is of the expected type,
/// /// otherwise panics with the given error message.
/// fn expect_none_as_ref(&self, msg: &str) -> () {
/// match self {
/// IpAddress::None => (),
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum is of the expected type,
/// /// otherwise panics with the given error message.
/// fn expect_none_as_mut(&mut self, msg: &str) -> () {
/// match self {
/// IpAddress::None => (),
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns `true`, if the enum value is of the expected type, otherwise returns
/// /// `false`.
/// fn is_none(&self) -> bool {
/// match self {
/// IpAddress::None => true,
/// _ => false,
/// }
/// }
///
/// /// Returns `true`, if the enum value is of the expected type and the given closure
/// /// evalutates to `true`, otherwise returns `false`.
/// fn is_none_and(&self, f: impl FnOnce(()) -> bool) -> bool {
/// match self {
/// IpAddress::None => f(()),
/// _ => false,
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns `None`.
/// fn v4(self) -> Option<(u8, u8, u8, u8)> {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => Some((f0, f1, f2, f3)),
/// _ => None,
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum value is of the expected type,
/// /// otherwise returns `None`.
/// fn v4_as_ref(&self) -> Option<(&u8, &u8, &u8, &u8)> {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => Some((f0, f1, f2, f3)),
/// _ => None,
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum value is of the expected
/// /// type, otherwise returns `None`.
/// fn v4_as_mut(&mut self) -> Option<(&mut u8, &mut u8, &mut u8, &mut u8)> {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => Some((f0, f1, f2, f3)),
/// _ => None,
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// panics.
/// fn unwrap_v4(self) -> (u8, u8, u8, u8) {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => (f0, f1, f2, f3),
/// _ => panic!("Failed unwrapping to IpAddress::V4. Unexpected variant"),
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum value is of the expected type,
/// /// otherwise panics.
/// fn unwrap_v4_as_ref(&self) -> (&u8, &u8, &u8, &u8) {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => (f0, f1, f2, f3),
/// _ => panic!("Failed unwrapping to IpAddress::V4. Unexpected variant"),
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum value is of the expected
/// /// type, otherwise panics.
/// fn unwrap_v4_as_mut(&mut self) -> (&mut u8, &mut u8, &mut u8, &mut u8) {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => (f0, f1, f2, f3),
/// _ => panic!("Failed unwrapping to IpAddress::V4. Unexpected variant"),
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns the given default value.
/// fn unwrap_v4_or(self, default: (u8, u8, u8, u8)) -> (u8, u8, u8, u8) {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => (f0, f1, f2, f3),
/// _ => default,
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns the value that the given closure evaluated to.
/// fn unwrap_v4_or_else(
/// self,
/// f: impl FnOnce(Self) -> (u8, u8, u8, u8),
/// ) -> (u8, u8, u8, u8) {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => (f0, f1, f2, f3),
/// some => f(some),
/// }
/// }
///
/// /// Returns the inner data, if the enum is of the expected type, otherwise panics with
/// /// the given error message.
/// fn expect_v4(self, msg: &str) -> (u8, u8, u8, u8) {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => (f0, f1, f2, f3),
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum is of the expected type,
/// /// otherwise panics with the given error message.
/// fn expect_v4_as_ref(&self, msg: &str) -> (&u8, &u8, &u8, &u8) {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => (f0, f1, f2, f3),
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum is of the expected type,
/// /// otherwise panics with the given error message.
/// fn expect_v4_as_mut(&mut self, msg: &str) -> (&mut u8, &mut u8, &mut u8, &mut u8) {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => (f0, f1, f2, f3),
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns `true`, if the enum value is of the expected type, otherwise returns
/// /// `false`.
/// fn is_v4(&self) -> bool {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => true,
/// _ => false,
/// }
/// }
///
/// /// Returns `true`, if the enum value is of the expected type and the given closure
/// /// evalutates to `true`, otherwise returns `false`.
/// fn is_v4_and(&self, f: impl FnOnce((&u8, &u8, &u8, &u8)) -> bool) -> bool {
/// match self {
/// IpAddress::V4(f0, f1, f2, f3) => f((f0, f1, f2, f3)),
/// _ => false,
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns `None`.
/// fn v6(self) -> Option<String> {
/// match self {
/// IpAddress::V6(f0) => Some(f0),
/// _ => None,
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum value is of the expected type,
/// /// otherwise returns `None`.
/// fn v6_as_ref(&self) -> Option<&String> {
/// match self {
/// IpAddress::V6(f0) => Some(f0),
/// _ => None,
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum value is of the expected
/// /// type, otherwise returns `None`.
/// fn v6_as_mut(&mut self) -> Option<&mut String> {
/// match self {
/// IpAddress::V6(f0) => Some(f0),
/// _ => None,
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// panics.
/// fn unwrap_v6(self) -> String {
/// match self {
/// IpAddress::V6(f0) => f0,
/// _ => panic!("Failed unwrapping to IpAddress::V6. Unexpected variant"),
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum value is of the expected type,
/// /// otherwise panics.
/// fn unwrap_v6_as_ref(&self) -> &String {
/// match self {
/// IpAddress::V6(f0) => f0,
/// _ => panic!("Failed unwrapping to IpAddress::V6. Unexpected variant"),
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum value is of the expected
/// /// type, otherwise panics.
/// fn unwrap_v6_as_mut(&mut self) -> &mut String {
/// match self {
/// IpAddress::V6(f0) => f0,
/// _ => panic!("Failed unwrapping to IpAddress::V6. Unexpected variant"),
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns the given default value.
/// fn unwrap_v6_or(self, default: String) -> String {
/// match self {
/// IpAddress::V6(f0) => f0,
/// _ => default,
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns the value that the given closure evaluated to.
/// fn unwrap_v6_or_else(self, f: impl FnOnce(Self) -> String) -> String {
/// match self {
/// IpAddress::V6(f0) => f0,
/// some => f(some),
/// }
/// }
///
/// /// Returns the inner data, if the enum is of the expected type, otherwise panics with
/// /// the given error message.
/// fn expect_v6(self, msg: &str) -> String {
/// match self {
/// IpAddress::V6(f0) => f0,
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum is of the expected type,
/// /// otherwise panics with the given error message.
/// fn expect_v6_as_ref(&self, msg: &str) -> &String {
/// match self {
/// IpAddress::V6(f0) => f0,
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum is of the expected type,
/// /// otherwise panics with the given error message.
/// fn expect_v6_as_mut(&mut self, msg: &str) -> &mut String {
/// match self {
/// IpAddress::V6(f0) => f0,
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns `true`, if the enum value is of the expected type, otherwise returns
/// /// `false`.
/// fn is_v6(&self) -> bool {
/// match self {
/// IpAddress::V6(f0) => true,
/// _ => false,
/// }
/// }
///
/// /// Returns `true`, if the enum value is of the expected type and the given closure
/// /// evalutates to `true`, otherwise returns `false`.
/// fn is_v6_and(&self, f: impl FnOnce(&String) -> bool) -> bool {
/// match self {
/// IpAddress::V6(f0) => f(f0),
/// _ => false,
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns `None`.
/// fn multi(self) -> Option<IpAddressMulti> {
/// match self {
/// IpAddress::Multi { v4, v6 } => Some(IpAddressMulti { v4, v6 }),
/// _ => None,
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum value is of the expected type,
/// /// otherwise returns `None`.
/// fn multi_as_ref(&self) -> Option<IpAddressMultiRef> {
/// match self {
/// IpAddress::Multi { v4, v6 } => Some(IpAddressMultiRef { v4, v6 }),
/// _ => None,
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum value is of the expected
/// /// type, otherwise returns `None`.
/// fn multi_as_mut(&mut self) -> Option<IpAddressMultiMut> {
/// match self {
/// IpAddress::Multi { v4, v6 } => Some(IpAddressMultiMut { v4, v6 }),
/// _ => None,
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// panics.
/// fn unwrap_multi(self) -> IpAddressMulti {
/// match self {
/// IpAddress::Multi { v4, v6 } => IpAddressMulti { v4, v6 },
/// _ => panic!("Failed unwrapping to IpAddress::Multi. Unexpected variant"),
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum value is of the expected type,
/// /// otherwise panics.
/// fn unwrap_multi_as_ref(&self) -> IpAddressMultiRef {
/// match self {
/// IpAddress::Multi { v4, v6 } => IpAddressMultiRef { v4, v6 },
/// _ => panic!("Failed unwrapping to IpAddress::Multi. Unexpected variant"),
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum value is of the expected
/// /// type, otherwise panics.
/// fn unwrap_multi_as_mut(&mut self) -> IpAddressMultiMut {
/// match self {
/// IpAddress::Multi { v4, v6 } => IpAddressMultiMut { v4, v6 },
/// _ => panic!("Failed unwrapping to IpAddress::Multi. Unexpected variant"),
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns the given default value.
/// fn unwrap_multi_or(self, default: IpAddressMulti) -> IpAddressMulti {
/// match self {
/// IpAddress::Multi { v4, v6 } => IpAddressMulti { v4, v6 },
/// _ => default,
/// }
/// }
///
/// /// Returns the inner data, if the enum value is of the expected type, otherwise
/// /// returns the value that the given closure evaluated to.
/// fn unwrap_multi_or_else(
/// self,
/// f: impl FnOnce(Self) -> IpAddressMulti,
/// ) -> IpAddressMulti {
/// match self {
/// IpAddress::Multi { v4, v6 } => IpAddressMulti { v4, v6 },
/// some => f(some),
/// }
/// }
///
/// /// Returns the inner data, if the enum is of the expected type, otherwise panics with
/// /// the given error message.
/// fn expect_multi(self, msg: &str) -> IpAddressMulti {
/// match self {
/// IpAddress::Multi { v4, v6 } => IpAddressMulti { v4, v6 },
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns a reference to the inner data, if the enum is of the expected type,
/// /// otherwise panics with the given error message.
/// fn expect_multi_as_ref(&self, msg: &str) -> IpAddressMultiRef {
/// match self {
/// IpAddress::Multi { v4, v6 } => IpAddressMultiRef { v4, v6 },
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns a mutable reference to the inner data, if the enum is of the expected type,
/// /// otherwise panics with the given error message.
/// fn expect_multi_as_mut(&mut self, msg: &str) -> IpAddressMultiMut {
/// match self {
/// IpAddress::Multi { v4, v6 } => IpAddressMultiMut { v4, v6 },
/// _ => panic!("{}", msg),
/// }
/// }
///
/// /// Returns `true`, if the enum value is of the expected type, otherwise returns
/// /// `false`.
/// fn is_multi(&self) -> bool {
/// match self {
/// IpAddress::Multi { v4, v6 } => true,
/// _ => false,
/// }
/// }
///
/// /// Returns `true`, if the enum value is of the expected type and the given closure
/// /// evalutates to `true`, otherwise returns `false`.
/// fn is_multi_and(&self, f: impl FnOnce(IpAddressMultiRef) -> bool) -> bool {
/// match self {
/// IpAddress::Multi { v4, v6 } => f(IpAddressMultiRef { v4, v6 }),
/// _ => false,
/// }
/// }
/// }
/// ```
/// </details>
///
/// This example will generate methods of the category `Var` and `IsVar`.
/// ```rust
/// # use enpow::enpow;
///
/// #[enpow(Var, IsVar)]
/// #[enpow_derive(Debug, PartialEq)]
/// #[derive(Debug, PartialEq)]
/// enum IpAddress {
/// None,
/// V4(u8, u8, u8, u8),
/// V6(String),
/// Multi {
/// v4: (u8, u8, u8, u8),
/// v6: String,
/// },
/// }
///
/// // fn <variant>()
/// assert_eq!(IpAddress::V4(192, 168, 0, 1).v4(), Some((192, 168, 0, 1)));
/// assert_eq!(IpAddress::None.multi(), None);
///
/// // fn is_<variant>()
/// assert_eq!(IpAddress::None.is_none(), true);
/// assert_eq!(IpAddress::V6("::1".into()).is_v4(), false);
///
/// // fn is_<variant>_and()
/// assert_eq!(IpAddress::V4(192, 168, 0, 1).is_v4_and(|ip| *ip.0 == 192), true);
/// assert_eq!(IpAddress::V6("::1".into()).is_v6_and(|ip| *ip == "::"), false);
/// assert_eq!(IpAddress::None.is_v4_and(|_| true), false);
/// ```
///
/// ## Auto Derives
///
/// Attaching the additional attribute `enpow_derive()` __below__ `enpow` adds the specified auto
/// trait derives to the automatically generated types. `Ref` structs always automatically derive
/// `Clone` and `Copy`, while `Mut` structs are prohibited from deriving these traits. This
/// exclusion will be handled automatically by the macro.
///
/// ```rust
/// # use enpow::enpow;
///
/// #[enpow(All)]
/// #[enpow_derive(Debug, PartialEq)]
/// enum IpAddress {
/// None,
/// V4(u8, u8, u8, u8),
/// V6(String),
/// Multi {
/// v4: (u8, u8, u8, u8),
/// v6: String,
/// },
/// }
///
/// // Using PartialEq and Debug derive
/// assert_eq!(
/// IpAddress::Multi { v4: (0, 0, 0, 0), v6: "::".into() }.unwrap_multi(),
/// IpAddressMulti { v4: (0, 0, 0, 0), v6: "::".into() }
/// );
///
/// // Using automatic Copy derive on Ref struct
/// let ip = IpAddress::Multi { v4: (0, 0, 0, 0), v6: "::".into() };
/// let copy = ip.unwrap_multi_as_ref();
/// let another_copy = copy;
/// assert_eq!(copy, IpAddressMultiRef { v4: &(0, 0, 0, 0), v6: &"::".into() });
/// assert_eq!(another_copy, IpAddressMultiRef { v4: &(0, 0, 0, 0), v6: &"::".into() });
/// ```
///////////////////////////////////////////////////////////////////////////////////////////////////
#[proc_macro_attribute]
pub fn enpow(
attribute: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
match enpow::entry(attribute.into(), item.into()) {
Ok(stream) => stream,
Err(error) => error.to_compile_error(),
}
.into()
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// The `extract` attribute attached to an enum turns each variant into a separate struct that then
/// becomes the only field of the variant. It supports generics and variants of every type, with
/// named or unnamed fields or no fields attached. Variants without data are turned into unit
/// structs, variants with unnamed fields get turned into tuple structs, and variants with named
/// fields are transformed into structs, each named after the enum and variant, i.e.
/// `EnumVariant`. The structs generated inherit the visibility modifier of the target enum.
/// Additionally, doc comments attached to the variants and variant fields are inherited by the
/// generated structs.
///
/// In parethesis, the following arguments to `extract` can be used to specify which variant types
/// to extract. Without any arguments, all variants will be extracted.
///
/// - `Unit`: Extracts variants without data into unit structs.
/// - `Single`: Extracts variants with a single unnamed field into tuple structs.
/// - `Unnamed`: Extracts variants with multiple unnamed fields into tuple structs.
/// - `Named`: Extracts variants with named fields into structs.
/// - `All`: Extracts all variants into structs.
///
/// This example will extract all variants.
/// ```rust
/// # use enpow::extract;
///
/// #[extract(All)]
/// enum IpAddress {
/// None,
/// V4(u8, u8, u8, u8),
/// V6(String),
/// Multi {
/// v4: (u8, u8, u8, u8),
/// v6: String,
/// },
/// }
///
/// // Using the modified enum variants and its generated structs
/// IpAddress::None(IpAddressNone);
/// IpAddress::V4(IpAddressV4(192, 168, 0, 1));
/// IpAddress::V6(IpAddressV6("::1".into()));
/// IpAddress::Multi(IpAddressMulti { v4: (192, 168, 0, 1), v6: "::1".into() });
/// ```
///
/// <details>
/// <summary>ℹ️ Click to reveal generated code</summary>
///
/// ```rust
/// enum IpAddress {
/// None(IpAddressNone),
/// V4(IpAddressV4),
/// V6(IpAddressV6),
/// Multi(IpAddressMulti),
/// }
///
/// struct IpAddressNone;
///
/// struct IpAddressV4(pub u8, pub u8, pub u8, pub u8);
///
/// struct IpAddressV6(pub String);
///
/// struct IpAddressMulti {
/// pub v4: (u8, u8, u8, u8),
/// pub v6: String,
/// }
/// ```
/// </details>
///
/// This example will extract all variants with multiple unnamed fields or named fields into
/// separate structs.
///
/// ```rust
/// # use enpow::extract;
///
/// #[extract(Unnamed, Named)]
/// enum IpAddress {
/// None,
/// V4(u8, u8, u8, u8),
/// V6(String),
/// Multi {
/// v4: (u8, u8, u8, u8),
/// v6: String,
/// },
/// }
///
/// // Using the unmodified enum variants
/// IpAddress::None;
/// IpAddress::V6("::1".into());
///
/// // Using the modified enum variants and its generated structs
/// IpAddress::V4(IpAddressV4(192, 168, 0, 1));
/// IpAddress::Multi(IpAddressMulti { v4: (192, 168, 0, 1), v6: "::1".into() });
/// ```
///
/// <details>
/// <summary>ℹ️ Click to reveal generated code</summary>
///
/// ```rust
/// enum IpAddress {
/// None,
/// V4(IpAddressV4),
/// V6(String),
/// Multi(IpAddressMulti),
/// }
///
/// struct IpAddressV4(pub u8, pub u8, pub u8, pub u8);
///
/// struct IpAddressMulti {
/// pub v4: (u8, u8, u8, u8),
/// pub v6: String,
/// }
/// ```
/// </details>
///
/// An additional `derive` macro attached to the enum should come __after__ `extract`
/// to make sure the automatically derived implementations match the changed enum structure.
///
/// ## Auto Derives
///
/// Attaching the additional attribute `extract_derive()` __below__ `extract` adds the specified
/// auto trait derives to the automatically generated types.
///
/// ```rust
/// # use enpow::extract;
///
/// #[extract]
/// #[extract_derive(Clone, Debug, PartialEq)]
/// enum IpAddress {
/// None,
/// V4(u8, u8, u8, u8),
/// V6(String),
/// Multi {
/// v4: (u8, u8, u8, u8),
/// v6: String,
/// },
/// }
///
/// // Using PartialEq and Debug derive
/// assert_eq!(
/// IpAddressMulti { v4: (0, 0, 0, 0), v6: "::".into() },
/// IpAddressMulti { v4: (0, 0, 0, 0), v6: "::".into() }
/// );
/// ```
///
/// <details>
/// <summary>ℹ️ Click to reveal generated code</summary>
///
/// ```rust
/// enum IpAddress {
/// None(IpAddressNone),
/// V4(IpAddressV4),
/// V6(IpAddressV6),
/// Multi(IpAddressMulti),
/// }
///
/// #[derive(Clone, Debug, PartialEq)]
/// struct IpAddressNone;
///
/// #[derive(Clone, Debug, PartialEq)]
/// struct IpAddressV4(pub u8, pub u8, pub u8, pub u8);
///
/// #[derive(Clone, Debug, PartialEq)]
/// struct IpAddressV6(pub String);
///
/// #[derive(Clone, Debug, PartialEq)]
/// struct IpAddressMulti {
/// pub v4: (u8, u8, u8, u8),
/// pub v6: String,
/// }
/// ```
/// </details>
///////////////////////////////////////////////////////////////////////////////////////////////////
#[proc_macro_attribute]
pub fn extract(
attribute: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
match extract::entry(attribute.into(), item.into()) {
Ok(stream) => stream,
Err(error) => error.to_compile_error(),
}
.into()
}