Giter VIP home page Giter VIP logo

derive_is_enum_variant's Introduction

derive_is_enum_variant

derive_is_enum_variant

Build Status

Stop writing pub is_whatever(&self) -> bool for your enums by hand -- it's a pain! Just #[derive(is_enum_variant)] instead!

Usage

Add derive_is_enum_variant to your crate's Cargo.toml:

[dependencies]
derive_is_enum_variant = "<insert-latest-version-here>"

And then add #[derive(is_enum_variant)] to your enum definitions:

#[macro_use]
extern crate derive_is_enum_variant;

#[derive(is_enum_variant)]
pub enum Pet {
    Doggo,
    Kitteh,
}

fn main() {
    let pet = Pet::Doggo;

    assert!(pet.is_doggo());
    assert!(!pet.is_kitteh());
}

Customizing Predicate Names

By default, the predicates are named is_snake_case_of_variant_name. You can use any name you want instead with #[is_enum_variant(name = "..")]:

#[derive(is_enum_variant)]
pub enum Pet {
    #[is_enum_variant(name = "is_real_good_boy")]
    Doggo,
    Kitteh,
}

let pet = Pet::Doggo;
assert!(pet.is_real_good_boy());

Skipping Predicates for Certain Variants

If you don't want to generate a predicate for a certain variant, you can use #[is_enum_variant(skip)]:

#[derive(is_enum_variant)]
pub enum Errors {
    Io(::std::io::Error),

    #[doc(hidden)]
    #[is_enum_variant(skip)]
    __NonExhaustive,
}

License

Licensed under either of

at your option.

Contribution

See CONTRIBUTING.md for hacking!

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

License: Apache-2.0/MIT

derive_is_enum_variant's People

Contributors

fitzgen avatar

Stargazers

Wandalen avatar Ben avatar Carlos Pérez avatar José Luis Cruz avatar Christoph Grabo avatar Denis Andrejew avatar  avatar  avatar Tobias Bieniek avatar Juri Hahn avatar Mike avatar Yosh avatar Dan Kolsoi avatar Rust avatar Marin Atanasov Nikolov avatar  avatar Zack M. Davis avatar Thomas Wickham avatar Willi Kappler avatar Davy Duperron avatar Dawid Ciężarkiewicz avatar Cody Laeder avatar

Watchers

 avatar José Luis Cruz avatar  avatar

derive_is_enum_variant's Issues

Failure to parse

I tried adding this macro to this enum: https://github.com/apollographql/apollo-cli/blob/920b15820f1847af97732d15da9c1436a8ddd3c3/cli/src/cli.rs#L54-L65

I received this error message:

   = help: message: should parse input tokens into AST: "failed to parse derive input: \"pub enum Subcommand {\\n\\n    #[structopt(name = \\\"update\\\")]\\n    ///  🚀  update the Apollo CLI\\n    Update(commands::Update),\\n\\n    #[structopt(name = \\\"print\\\", setting = AppSettings :: Hidden)]\\n    ///  🖨   parse and pretty print schemas to stdout\\n    Print(commands::Print),\\n\\n    #[structopt(name = \\\"setup\\\", setting = AppSettings :: Hidden)]\\n    ///  🚜  setup the Apollo toolchain in your environment\\n    Setup(commands::Setup),\\n}\""

added using
#[derive(derive_is_enum_variant::is_enum_variant)]

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.