1 unstable release

0.1.0 Sep 7, 2022

#13 in #variant-name

Download history 93/week @ 2024-07-19 63/week @ 2024-07-26 160/week @ 2024-08-02 57/week @ 2024-08-09 25/week @ 2024-08-16 21/week @ 2024-08-23 21/week @ 2024-08-30 212/week @ 2024-09-06 70/week @ 2024-09-13 75/week @ 2024-09-20 78/week @ 2024-09-27 78/week @ 2024-10-04 55/week @ 2024-10-11 62/week @ 2024-10-18 38/week @ 2024-10-25 49/week @ 2024-11-01

208 downloads per month
Used in solwalrs

Apache-2.0

6KB
57 lines

name_variant

Generates methods to print an enum variant's name as a string.

Example

 use name_variant::NamedVariant;

 #[derive(NamedVariant)]
 enum TestEnum {
     A,
     B(),
     C(i32, i32),
     D { _name: String, _age: i32 },
     VariantTest,
 }

 fn main() {
     let x = TestEnum::C(1, 2);
     assert_eq!(x.variant_name(), "C");

     let x = TestEnum::A;
     assert_eq!(x.variant_name(), "A");

     let x = TestEnum::B();
     assert_eq!(x.variant_name(), "B");

     let x = TestEnum::D {_name: "Jane Doe".into(), _age: 30 };
     assert_eq!(x.variant_name(), "D");

     let x = TestEnum::VariantTest;
     assert_eq!(x.variant_name(), "VariantTest");
 }

lib.rs:

Generates methods to print the name of the enum variant.

Example

use name_variant::NamedVariant;

#[derive(NamedVariant)]
enum TestEnum {
    A,
    B(),
    C(i32, i32),
    D { _name: String, _age: i32 },
    VariantTest,
}

let x = TestEnum::C(1, 2);
assert_eq!(x.variant_name(), "C");

let x = TestEnum::A;
assert_eq!(x.variant_name(), "A");

let x = TestEnum::B();
assert_eq!(x.variant_name(), "B");

let x = TestEnum::D {_name: "Jane Doe".into(), _age: 30 };
assert_eq!(x.variant_name(), "D");

let x = TestEnum::VariantTest;
assert_eq!(x.variant_name(), "VariantTest");

Dependencies

~1.5MB
~37K SLoC