https://github.com/alephcubed/loose_enum
A macro for defining loose repr enums.
https://github.com/alephcubed/loose_enum
macro macros repr representation rust utility
Last synced: 4 months ago
JSON representation
A macro for defining loose repr enums.
- Host: GitHub
- URL: https://github.com/alephcubed/loose_enum
- Owner: AlephCubed
- License: apache-2.0
- Created: 2025-12-07T19:33:06.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-12-16T19:41:45.000Z (6 months ago)
- Last Synced: 2025-12-19T18:55:30.551Z (6 months ago)
- Topics: macro, macros, repr, representation, rust, utility
- Language: Rust
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Loose Enum
[](https://crates.io/crates/loose_enum)
[](https://docs.rs/loose_enum)

A macro for defining loose repr enums.
When parsing userdata, you often have known/supported cases; however, users don't always follow the rules.
One way to solve this is having a backup `Undefined` case that supports any value. This crate hopes to simplify this process.
### Example:
An integer repr bool, with 0 being false and 1 being true would look something like this:
```rust
loose_enum::loose_enum! {
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum LooseBool: i32 {
#[default]
False = 0,
True = 1,
}
}
```
Which expands into this type:
```rust
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum LooseBool {
#[default]
False,
True,
/// Any value that doesn't match another case.
Undefined(i32),
}
```
The macro will also generate `From` and `Into` trait implementations.
If the `serde` feature is enabled, `Serialize` and `Deserialize` will also be implemented.
## Special Cases
### String
A special case has been created for when `String` is the data type, which will implement `From<&str>` on top of the normal trait implementations.
### Generics
There is also **experimental** support for generic types.
```rust
use num_traits::{ConstZero, ConstOne};
loose_enum::loose_enum! {
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum LooseBool {
#[default]
False = T::ZERO,
True = T::ONE,
}
}
```
Due to the orphan rule, `Into` cannot be implemented. Instead, an `into_repr` method will be added.