#enums #variant #error #companion #function #generate #enum-extract

enum-extract-error

A companion crate for enum-extract that exports an error type

2 releases

0.1.1 Dec 30, 2023
0.1.0 Dec 28, 2023

#2667 in Rust patterns

Download history 3/week @ 2024-07-22 21/week @ 2024-07-29 8/week @ 2024-08-05 4/week @ 2024-08-12 13/week @ 2024-08-19 40/week @ 2024-08-26 43/week @ 2024-09-02 14/week @ 2024-09-09 9/week @ 2024-09-16 37/week @ 2024-09-23 14/week @ 2024-09-30 11/week @ 2024-10-07 11/week @ 2024-10-14 9/week @ 2024-10-21 17/week @ 2024-10-28 14/week @ 2024-11-04

52 downloads per month
Used in 2 crates

MIT/Apache

7KB

enum-extract

License MIT License Apache 2.0

A deriving proc-macro that generates functions to the inner members of the enum.

This is a heavily modified fork of enum-as-inner. Some of the key differences are listed below:

  1. as_[variant], into_[variant] and as_[variant]_mut methods return a Result that can contain a concrete error type EnumExtractError.
    • The error is returned when the actual variant does not match the expected variant, and it contains both the name of the expected variant and the name of the actual variant for troubleshooting purposes.
    • The error also implements Display with a message of expected {expected}, got {actual}, so that you have a place to start troubleshooting rather than calling unwrap on an Option, or repeatedly writing better error messages by hand.
  2. Added extract_as_[variant], extract_into_[variant] and extract_as_[variant]_mut methods, which panic if the actual variant does not match the expected variant.
    • These are very useful in tests where panicking is acceptable, especially when combined with the better error messages supported by the EnumExtractError struct.

Crates

This project consists of two crates that should be used together. They are separate because crates the export procedural macros can only export procedural macros. They should be treated as a single project, and will therefore be versioned in lock-step.

enum-extract-macro

Dependency Status Crates.io doc.rs

Provides the EnumExtract derive macro that can be used on enums to get as_[variant] functions for each variant, along with other useful functions.

See the documentation for examples and more details.

enum-extract-error

Dependency Status Crates.io doc.rs

Provides the EnumExtractError used as a return value for the as_[variant] functions.

See the documentation for examples and more details.

Dependencies

~260–720KB
~17K SLoC