Crates.io | matched_enums |
lib.rs | matched_enums |
version | 1.3.0 |
created_at | 2025-04-13 12:58:03.1419+00 |
updated_at | 2025-08-09 06:39:25.420968+00 |
description | A macro that provides the option to bin enum attribute to match-statements. This makes it easier to convert values into enums. |
homepage | |
repository | https://gitlab.com/Larsbl00/rust-matched-enums |
max_upload_size | |
id | 1631809 |
size | 43,866 |
Rust allows you to specify integer values for enumerations which make it easy to convert integers to enums.
However, such luxary does not exist for complex patterns like range-based matching.
This library aims to add such functionality.
It will respect the option to assign integer values to enumartion allowing idiomatic derives from Ord
for comparisons.
While allowing for enumerations to be derived from ranges.
This conversion is achieved through the From
trait.
For example:
use matched_enums::Matched;
#[derive(Matched, PartialEq, Debug)]
#[matched_enum(value_type=i32)]
enum Signature {
#[matches(1..)]
Positive,
#[matches(..=-1)]
Negative,
#[matches(0..1)]
Zero,
}
assert_eq!(Signature::from(-42), Signature::Negative);
assert_eq!(Signature::from(0), Signature::Zero);
assert_eq!(Signature::from(451), Signature::Positive);
NOTE: Checkout the examples for more detailed and complex examples.
This library offers the following features:
runtime_configurable
: Allows the creation of runtime configurable structures.After deriving from Matched
, the following macros are available.
This library uses the following macros:
#[derive(Matched, ..)]
When enabled allows the use of the other macros.
#[matched_enum(..)]
Configure the behavior of the macro using key-value pairs (configured using key=value
).
The following keys are available:
Name | Type | Default | Description |
---|---|---|---|
allow_runtime_configurable |
Boolean Literal | false |
If the runtime_configurable feature is enabled, it will generate a struct with a Matcher postfix. For example: enum Foo will have struct FooMatcher . This matcher, holds binary predicates for each enumeration. Defaulting to the normal matcher1. |
precedence |
Integer | MAX |
Indicates the priority of the matcher in ascending order. The lower the value, the higher the priority. Keeps the order in which the enum attributes are defined by default. |
use_partial |
Boolean Literal | false |
When used, implements TryFrom instead of From . The implementation will use the _ matcher after all other statements to return an error if it failed. This means it can only be used if the input is only partially covered. |
value_type | value_types |
Any type or array of types | i32 |
Specify the type used from which the enum can be constructed. |
matches(..)
Indicates the criteria for matching a specific enum attribute as well as some configuration options passed as key-value pairs.
Arguments should match the pattern matches(<match-arm>[[,<attribute>=value]..])
.
The match-arm
can be any [MatchArm
]. Some examples:
#[_]
2#[matches(1)]
.#[matches(0|1)]
#[1 | _ if { i.set(i.get() + 1); false }]
The following attributes are available:
Name | Type | Default | Description |
---|---|---|---|
bind |
Any type | None |
Binds the current match arm to the provided type. |
If you are curious about certain decisions that have shaped this package over time. Please check out the design-decisions as this contains such detailed information.
A list of tasks that still need to achieved. These items are ordered arbitrarily and have no importance in their order.
NOTE: These issues are not yet tracked in git as most have been defined since the start before it was hosted online.
value_type
is set to MyType<T>
, From
should be implemented with the generic types: impl<T> From<MyType<T>> for Enum
.Cargo.toml
to find this easier (proc-macro, enum, match, etc.)precedence
attribute to matches which, optionally, allows one to specify the order of match priority without relying on enum declarations (if desired).DefaultMatcher
as an alias to a Matcher
, making it easier to use.allow_incomplete
is provided, else it is accepted.
value_type
has been specified, it must span $[T_{MIN}, T_{MAX}]$ instead, where T
represents the value_type
.Result<Self>
. Returning an "IncompleteRangeError" if invallid.uint8
should be fully matchable, but an i32
should have partial matching.allow_partial
dictates whether all type should be matched partially or fully, ought to be repurposed as the default.value_type
is an int32
and the attribute is something like Foo(i32)
. The value should be injected into foo.
Example of Matched
enum:
When using the runtime configurable match options, the Matcher will go through all of the predicates linearly in a top-to-bottom order. If, a wild card predicate is used before any of the other predicates, the matcher will always go for the wild card. ↩
There is an intention to add a try_from
returning an Option<Self>
.
It is recommended to use this once added and chaining the Option
with .or_default
.
Allowing for more idiomatic default construction. ↩