An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# Loose Enum
[![Version](https://img.shields.io/crates/v/loose_enum)](https://crates.io/crates/loose_enum)
[![Docs](https://img.shields.io/docsrs/loose_enum)](https://docs.rs/loose_enum)
![License](https://img.shields.io/crates/l/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.