Peternator7 / strum

A small rust library for adding custom derives to enums
https://crates.io/crates/strum
MIT License
1.74k stars 148 forks source link

Using `#[strum(to_string = "}")]` results in macro error #363

Open Somfic opened 4 months ago

Somfic commented 4 months ago

Hi!

I’m tinkering with writing a transpiler and am currently in the lexing stage. In my Token enum I have the following variant: CurlyClose. I’d like to implement the Display trait for Token using strum.

What I currently have:

#[derive(Debug, Clone, strum_macros::Display)]
pub enum Token {

    // Other tokens …

    /// An opening curly brace; `{`.
    #[strum(to_string = "{")]
    CurlyOpen,

    /// A closing curly brace; `}`.
    #[strum(to_string = "}")]       // <—- “Bracket closed without previous opened bracket”
    CurlyClose,

    // Other tokens …

}

The #[strum(to_string = "}")] macro is throwing a Bracket closed without previous opened bracket compiler error. When I try to escape the closing curly brace the macro panics with proc-macro panicked: unexpected byte '}' after \ character in string literal.

Am I doing something wrong? Other braces such as { [ ] ( and ) work fine.

Edgeworth commented 4 months ago

I'm also hitting this recently (same code worked before). It doesn't occur in strum 0.25

gmryuuko commented 3 months ago

After #360, the use of }} for escaping is also not possible.

Peternator7 commented 3 months ago

Related to #361. I haven't had time to fix this yet