| Crates.io | bidirectional_enum |
| lib.rs | bidirectional_enum |
| version | 0.2.0 |
| created_at | 2023-01-10 14:16:36.182611+00 |
| updated_at | 2023-01-10 14:32:06.712281+00 |
| description | Automatically generates conversions between an enum type and any other type |
| homepage | https://github.com/Giftzwerg02/bidirectional_enum |
| repository | |
| max_upload_size | |
| id | 755375 |
| size | 4,881 |
A macro to automatically generate a two-way binding between an enum-type and any other type.
use std::error::Error;
#[macro_use]
extern crate bidirectional_enum;
bi_enum! {
#[derive(Debug)]
enum SomeEnum <=> char
{
T1 <=> 'a',
T2 <=> 'b'
}
}
fn main() -> Result<(), Box<dyn Error>> {
let t1 = SomeEnum::T1;
// from enum to char
let a = char::from(t1);
dbg!(&a); // 'a'
// from char back to enum
let t1 = SomeEnum::try_from(a);
dbg!(&t1); // Ok(SomeEnum::T1)
// invalid char
let err = SomeEnum::try_from('c');
dbg!(&err); // Err(EnumTryFromErr { from: "char", to: "SomeEnum" })
Ok(())
}