2 releases

0.3.1 Sep 14, 2021
0.3.0 Aug 2, 2021

#280 in #variant

Download history 1132/week @ 2024-08-29 1165/week @ 2024-09-05 774/week @ 2024-09-12 1303/week @ 2024-09-19 1006/week @ 2024-09-26 1298/week @ 2024-10-03 130/week @ 2024-10-10 785/week @ 2024-10-17 730/week @ 2024-10-24 1475/week @ 2024-10-31 1590/week @ 2024-11-07 684/week @ 2024-11-14 1280/week @ 2024-11-21 1603/week @ 2024-11-28 1482/week @ 2024-12-05 649/week @ 2024-12-12

5,107 downloads per month
Used in every_variant

MIT license

12KB
247 lines

every_variant

Provides an EveryVariant trait that provides the every_variant() method on types. Allows you to easily generate every combination of variants in structures that contains Enums, or in nested enum trees. This to allow for additional testing of codepaths where nested enum trees are used.

The derive macro EveryVariant will provide the every_variant() method for you, with some preset values for the std types such as floats, integers and strings,

The generated data inside at the lowest level is currently fixed to specific values. If you have types that are dependend on strings with a specific format on the strings for example, I suggest that those be made into their own types and every_variant implemented manually for those.

Example:


use every_variant::EveryVariant;

/// Type of the message
#[derive(EveryVariant, Debug, Clone)]
enum MessageType {
    Codified,
    Markdown,
    Html,
}

/// This type should generate 4 different variant
#[derive(EveryVariant, Debug, Clone)]
struct FormattedMessage {
    /// Enum dictating how to render the string, None means its hidden
    rendermethod: Option<MessageType>,
    /// The optional content of the message
    text: String,
}

fn main() {
    let all_diferent_messages = FormattedMessage::every_variant();
    println!("{:#?}", all_diferent_messages);
}


the output will be:

[
    FormattedMessage {
        rendermethod: None,
        text: "example String",
    },
    FormattedMessage {
        rendermethod: Some(
            Codified,
        ),
        text: "example String",
    },
    FormattedMessage {
        rendermethod: Some(
            Markdown,
        ),
        text: "example String",
    },
    FormattedMessage {
        rendermethod: Some(
            Html,
        ),
        text: "example String",
    },
]

lib.rs:

Proc macros that can help with generating implements of the EveryVariant trait for structs and enums

Dependencies

~1.5MB
~38K SLoC