to_and_fro

Crates.ioto_and_fro
lib.rsto_and_fro
version0.6.0
sourcesrc
created_at2023-11-24 05:35:05.172949
updated_at2024-07-26 06:31:18.992631
descriptionProc macro for generating implimentations of Display and FromStr for Enum Variants
homepagehttps://github.com/tascord/ToAndFro
repositoryhttps://github.com/tascord/ToAndFro
max_upload_size
id1046814
size25,293
flora ~ 🏳️‍⚧️ ❤ 🦀 (tascord)

documentation

README

To, and Fro

Automatic implimentations for Display, FromStr, and others for Enums.

MIT License Crates.io GitHub Workflow Status (with event)

Package available through cargo

cargo add to_and_fro

Implimentation

#[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")

Casing

#[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
}

Fallback for FromStr

#[derive(ToAndFro)]
#[default("Fallback")]
pub enum TestEnum {
  Fallback,
  ValueOne,
  ValueTwo,
  ValueThree
}

TestEnum::from_str("ValueFour") // TestEnum::Fallback

Disallow field to be parsed FromStr

#[derive(ToAndFro)]
pub enum TestEnum {
  #[reject]
  ValueOne,
  ValueTwo
}

TestEnum::from_str("ValueOne")  // anyhow::Error("Invalid variant ValueOne for enum TestEnum")

Implement Serialize and Deserialize from serde

#[derive(ToAndFro)]
#[serde]
pub enum TestEnum {
  ValueOne,
  ValueTwo,
  ValueThree
}

List of supported cases:

Feedback

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.

Related

  • Synstructure, a crate that provides helper types for matching against enum variants, and extracting bindings to each of the fields in the deriving Struct or Enum in a generic way.
  • Heck, a crate that exists to provide case conversion between common cases like CamelCase and snake_case. It is intended to be unicode aware, internally consistent, and reasonably well performing.
Commit count: 30

cargo fmt