Peternator7 / strum

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

Round tripping "{{}}" doesn't work with Display + EnumString in 0.26 #361

Open tiye opened 5 months ago

tiye commented 5 months ago

upgraded from 0.25.2 to 0.26. My previous code broke. here are the cases I reproduced. any suggestion?

case 1

use strum_macros::{AsRefStr, EnumString};

#[derive(EnumString, strum_macros::Display, AsRefStr, Debug)]
enum A {
    #[strum(serialize = "{}")] // <--- this lie
    Bra,
    #[strum(serialize = "nor")]
    Nor,
}

fn main() {
    println!("{}", A::Bra);

    // parse
    let a = "{}".parse::<A>();
    println!("{:?}", a);
}
error: 1 positional argument in format string, but no arguments were given
 --> src/main.rs:5:26
  |
5 |     #[strum(serialize = "{}")]
  |                          ^^

error: could not compile `strum-demo` (bin "strum-demo") due to 1 previous error

case 2

use strum_macros::{AsRefStr, EnumString};

#[derive(EnumString, strum_macros::Display, AsRefStr, Debug)]
enum A {
    #[strum(serialize = "{{}}")] // <--- this line
    Bra,
    #[strum(serialize = "nor")]
    Nor,
}

fn main() {
    println!("{}", A::Bra);

    // parse
    let a = "{}".parse::<A>();
    println!("{:?}", a);
}
{}
Err(VariantNotFound)

case 3

use strum_macros::{AsRefStr, EnumString};

#[derive(EnumString, strum_macros::Display, AsRefStr, Debug)]
enum A {
    #[strum(serialize = "{{}}")] // <--- this line
    Bra,
    #[strum(serialize = "nor")]
    Nor,
}

fn main() {
    println!("{}", A::Bra);

    // parse
    let a = "{{}}".parse::<A>();
    println!("{:?}", a);
}
{}
Ok(Bra)
martin-g commented 5 months ago

Looks related to https://github.com/Peternator7/strum/issues/359

Peternator7 commented 5 months ago

Hey @tiye, sorry for the break here. There was a breaking change upgrading to 0.26 around format strings, but it should work correctly with escaped curly-braces so that's a bug. I'll work on fixing this, but in the meantime, the best solution is going to be staying on 0.25