loose_enum

Crates.ioloose_enum
lib.rsloose_enum
version0.1.0
created_at2025-12-07 21:29:30.008663+00
updated_at2025-12-16 19:41:59.0222+00
descriptionA macro for defining loose repr enums.
homepage
repositoryhttps://github.com/AlephCubed/loose_enum
max_upload_size
id1972261
size41,623
Josiah Nelson (AlephCubed)

documentation

README

Loose Enum

Version Docs License

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:

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:

#[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<i32> and Into<i32> 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.

use num_traits::{ConstZero, ConstOne};

loose_enum::loose_enum! {
    #[derive(Default, Debug, Clone, Copy, Eq, PartialEq, Hash)]
    pub enum LooseBool<T: PartialEq + ConstZero + ConstOne> {
        #[default]
        False = T::ZERO,
        True = T::ONE,
    }
}

Due to the orphan rule, Into<T> cannot be implemented. Instead, an into_repr method will be added.

Commit count: 0

cargo fmt