enum_from_variant

Crates.ioenum_from_variant
lib.rsenum_from_variant
version0.1.1
sourcesrc
created_at2024-06-18 21:33:04.751999
updated_at2024-06-18 21:37:17.279606
descriptionA Rust macro to generate From trait implementations for enum variants, simplifying error conversions and enum mapping.
homepagehttps://github.com/borngraced/enum-from-variant
repositoryhttps://github.com/borngraced/enum-from-variant
max_upload_size
id1276195
size10,217
Samuel Onoja (borngraced)

documentation

https://docs.rs/enum-from-variant

README

enum-from-variant crate provides the EnumFromVariant macro, which simplifies the generation of the From trait for converting one enum variant to another enum variant. This is particularly useful when you need to handle error conversions or map different enum types in your Rust code.

USAGE:

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),
    }
}

Limitations

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.

Commit count: 23

cargo fmt