Crates.io | to_and_fro |
lib.rs | to_and_fro |
version | 0.6.0 |
source | src |
created_at | 2023-11-24 05:35:05.172949 |
updated_at | 2024-07-26 06:31:18.992631 |
description | Proc macro for generating implimentations of Display and FromStr for Enum Variants |
homepage | https://github.com/tascord/ToAndFro |
repository | https://github.com/tascord/ToAndFro |
max_upload_size | |
id | 1046814 |
size | 25,293 |
Automatic implimentations for Display
, FromStr
, and others for Enums.
cargo add to_and_fro
#[derive(ToAndFro)]
pub enum TestEnum {
ValueOne,
ValueTwo,
ValueThree
}
TestEnum::ValueOne.to_string() // "ValueOne"
TestEnum::from_str("ValueTwo") // TestEnum::ValueTwo
TestEnum::from_str("ValueFour") // anyhow::Error("Invalid variant ValueFour for enum TestEnum")
#[derive(ToAndFro)]
pub enum TestEnum {
#[input_case("snake")] // FromStr will parse only snake_case input
ValueOne,
#[output_case("kebab")] // Display methods will produce a kebab-case output
ValueTwo,
ValueThree // Defaults to as written input, and as-written output
}
#[derive(ToAndFro)]
#[default("Fallback")]
pub enum TestEnum {
Fallback,
ValueOne,
ValueTwo,
ValueThree
}
TestEnum::from_str("ValueFour") // TestEnum::Fallback
#[derive(ToAndFro)]
pub enum TestEnum {
#[reject]
ValueOne,
ValueTwo
}
TestEnum::from_str("ValueOne") // anyhow::Error("Invalid variant ValueOne for enum TestEnum")
Serialize
and Deserialize
from serde
#[derive(ToAndFro)]
#[serde]
pub enum TestEnum {
ValueOne,
ValueTwo,
ValueThree
}
kebab
(heck)pascal
(heck)snake
(heck)title
(heck)train
(heck)lower_camel
(heck)upper_camel
(heck)shouty_kebab
(heck)shouty_snake
(heck)upper
(UPPERCASE)lower
(lowercase)percent
I appreciate all feedback, in whatever forms they might take.
If you're looking to specifically make a Bug Report, or Suggest a Feature, please do so through their templates in the issues section.