enum2pos is a rust derive macro for enums that generates from_index(usize, Vec<String>) -> Option<Self>
and
to_index()
methods for converting between an variants and
their position within the enum declaration (similar to an index).
Add this to your Cargo.toml
:
toml
enum2pos = "0.1.1"
Example:
```rust use enum2pos::EnumIndex;
enum SampleEnum { Unit, Unnamed(i32, String), }
fn to_index() { let unit = SampleEnum::Unit; let unnamed = SampleEnum::Unnamed(42, String::from("test"));
assert_eq!(unit.to_index(), 0);
assert_eq!(unnamed.to_index(), 1);
}
fn fromindexunit() {
let index = 0;
let args: Vec
assert_eq!(SampleEnum::from_index(index, &args), expected);
}
fn fromindexunnamed() { let index = 1; let args = vec!["42".tostring(), "test".tostring()]; let expected = Some(SampleEnum::Unnamed(42, String::from("test")));
assert_eq!(SampleEnum::from_index(index, &args), expected);
}
fn fromindexinvalid() {
let index = 2;
let args: Vec
assert_eq!(SampleEnum::from_index(index, &args), None);
} ```