Code Monkey home page Code Monkey logo

is-macro's Introduction

is-macro's People

Contributors

aaron1011 avatar alexstrnik avatar coolreader18 avatar kdy1 avatar skittles1412 avatar snejugal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

is-macro's Issues

as_mut_variant

The macro currently generates as_variant(), returning Option<&T>. Could it also generate as_mut_variant(), returning Option<&mut T>?

More than one field in tuple variants cause the macro to panic

Minimal reproducible example:

use is_macro::Is;

#[derive(Debug, Is)]
enum Foo {
    A(u8),
    B(u8, u8)
}

fn main() {
    println!("Hello, world!");
}
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
 --> src\main.rs:3:17
  |
3 | #[derive(Debug, Is)]
  |                 ^^ expected 2 fields, found 1
...
6 |     B(u8, u8),
  |     --------- tuple variant defined here

error: aborting due to previous error

Attribute to rename generated method names

While we're adopting is-macro in tbot, we found that translation to snake_case may yield a bit unexpected results around digits. For this enum, I would expect that the generated is_ method would be named is_video_mp4, not is_video_mp_4 (notice the underscore before the number):

#[derive(Is)]
pub enum MimeType {/// The `video/mp4` MIME type.
    VideoMp4,
}

Such cases should probably be left to humans, because one might actually expect an underscore inserted between a word and a digit. I suggest an attribute, setting which changes the generated method names:

#[derive(Is)]
pub enum MimeType {#[is(root = "video_mp4")]
    VideoMp4,
}

With this attribute, Is would generate is_video_mp4 instead of is_video_mp_4. If it were possible for this variant, it would also emit expect_video_mp4 and video_mp4 instead of expect_video_mp_4 and video_mp_4. This can be extended in the future with ability to choose names for each method or change their prefixes. The only downside to this is a more complicated implementation. Also, if this is going to be implemented, we should probably come up with a better name than root.

(by the way, we really appreciate this crate for generating expect_ and taking methods, and also that we don't have to manually implement is_ methods 😀)

References in enum variants cause the macro to panic

Minimal reproducible example:

use is_macro::Is;

#[derive(Is)]
enum Foo {
    A(&'static u8),
}
error: proc-macro derive panicked
 --> src\main.rs:3:10
  |
3 | #[derive(Is)]
  |          ^^
  |
  = help: message: Quote::parse() failed.
                          Note: quasi quotting was invoked from:
                 D:\Tools\Rust\cargo\registry\src\github.com-1ecc6299db9ec823\is-macro-0.1.1\src\lib.rs:131:45

          Error from syn: expected identifier
              >>>>>
                  & 'static u8
              <<<<<

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.