Giter Club home page Giter Club logo

enum-debug's Introduction

EnumDebug

Simple Rust crate that implements EnumDebug, which can automatically derive a formatter for enum variant names only.

Installation

To use this crate, simply add the following to your Cargo.toml:

enum-debug = { git = "https://github.com/Lut99/enum-debug" }

This will use the latest version. You can also commit yourself to a specific version by using:

enum-debug = { git = "https://github.com/Lut99/enum-debug", tag="v<VERSION>" }

where you should replace <VERSION> with the version of your choice (check the releases to find possible options).

Usage

This crate makes the EnumDebug trait available, which can be implemented on an enum of your choice.

Custom implementation is done as follows:

use enum_debug::EnumDebug;

enum Jedi {
    ObiWanKenobi,
    AnakinSkywalker,
    MaceWindu,
    MasterYoda,
}
impl EnumDebug for Jedi {
    // NOTE: Not necessary, but otherwise it will use the Rust internal type name
    #[inline]
    fn type_name() -> &'static str { "Jedi" }

    #[inline]
    fn variant_names() -> &'static [&'static str] {
        &["ObiWanKenobi", "AnakinSkywalker", "MaceWindu", "MasterYoda"]
    }

    #[inline]
    fn variant_name(&self) -> &'static str {
        match self {
            Self::ObiWanKenobi => Self::variant_names()[0],
            Self::AnakinSkywalker => Self::variant_names()[1],
            Self::MaceWindu => Self::variant_names()[2],
            Self::MasterYoda => Self::variant_names()[3],
        }
    }
}

assert_eq!(format!("{}", Jedi::ObiWanKenobi.variant()), "ObiWanKenobi");
assert_eq!(format!("{:?}", Jedi::AnakinSkywalker.variant()), "Jedi::AnakinSkywalker");
assert_eq!(Jedi::MaceWindu.variant_name(), "MaceWindu");

However, this is quite tedious. A faster way is to use the derive feature and the use the derive macro:

# Enable the feature
enum-debug = { git = "https://github.com/Lut99/enum-debug", features = ["derive"] }
use enum_debug::EnumDebug;

// Now it becomes as easy as
#[derive(EnumDebug)]
enum Jedi {
    ObiWanKenobi,
    AnakinSkywalker,
    MaceWindu,
    MasterYoda,
}

assert_eq!(format!("{}", Jedi::ObiWanKenobi.variant()), "ObiWanKenobi");
assert_eq!(format!("{:?}", Jedi::AnakinSkywalker.variant()), "Jedi::AnakinSkywalker");
assert_eq!(Jedi::MaceWindu.variant_name(), "MaceWindu");

See the documentation on the derive-module for more information on the derive-macro.

Contribution

If you have any suggestions, comments, tip or bugs, please create an issue to let us know! Or go ahead and create a pull request.

License

This project is licensed under the Apache 2.0 license. See LICENSE for more information.

enum-debug's People

Contributors

lut99 avatar

Watchers

 avatar

enum-debug's Issues

Automatically derive `#[enum_debug(name)]`

In contrast to what I expected, the default derived name of the enum's type is quite an awkward one (typically the full path). Thus, automatically implementing #[enum_debug(name)] should be preferred.

For compatibility purposes, we should add #[enum_debug(path)] in addition to #[enum_debug(name)] to emulate the old behaviour, and perhaps a feature to switch between the default.

Fix `write` macro being context-dependent

While probably a niche use-case, the write macro used in the enum-debug-derive crate is still dependent on the parent scope. This means that, when the write macro is overridden, this causes the derivation process to crash.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.