Crates.io | enum_from_variant |
lib.rs | enum_from_variant |
version | 0.1.1 |
source | src |
created_at | 2024-06-18 21:33:04.751999 |
updated_at | 2024-06-18 21:37:17.279606 |
description | A Rust macro to generate From |
homepage | https://github.com/borngraced/enum-from-variant |
repository | https://github.com/borngraced/enum-from-variant |
max_upload_size | |
id | 1276195 |
size | 10,217 |
enum-from-variant
crate provides the EnumFromVariant macro, which simplifies the generation of the From
Consider the following example where we convert between different enum types using the EnumFromVariant macro:
use enum_from_variant::EnumFromVariant;
use derive_more::Display;
#[derive(Debug, EnumFromVariant)]
pub enum MainError {
#[enum_from_variant("NetworkError")]
Network(String),
#[enum_from_variant("DatabaseError")]
Database(DatabaseError),
}
#[derive(Debug, Display)]
pub enum NetworkError {
Timeout(String),
}
#[derive(Debug, Display)]
pub enum DatabaseError {
ConnectionFailed(String),
}
fn network_request() -> Result<(), MainError> {
Err(NetworkError::Timeout("Network timeout".to_string()).into())
}
fn main() {
match network_request() {
Ok(_) => println!("Request succeeded"),
Err(e) => println!("Error: {:?}", e),
}
}
Current Support
: The macro only supports enum variants with basic inner types like String and other enums.
Unsupported Types
: Tuple variants, struct variants, and more complex inner types are not supported at this time.