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

Is this expected behavior (combining EnumStrings with multiple serializes and ascii_case_insensitive with VariantNames)? #366

Open andrewsuperlegit opened 5 months ago

andrewsuperlegit commented 5 months ago

I'm pretty new to rust and decided to implement my own magic the gathering implementation so i have to match a bunch of different cases and stuff and some of this isn't behaving how I'm expecting it to, so i just want to sanity check:

#[derive(Debug, PartialEq, EnumString, Clone, Hash, Eq, VariantNames, VariantArray)]
#[strum(serialize_all="lowercase")]
enum Color {
    #[strum(serialize="G", serialize="{green}", ascii_case_insensitive)]
    Green
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test] 
    fn color_accepts_lowercase(){  // <----- This test fails. but.. should it? because i'm serializing all to lowercase AND using case insensitivity
        let _green = Color::Green;
        let green = Color::from_str("green");
        assert_eq!(_green, green.unwrap()); // <-- called `Result::unwrap()` on an `Err` value: VariantNotFound
    }
    #[test]
    fn color_accepts_single_letter(){
        let green = Color::Green;
        let g = Color::from_str("g");
        assert_eq!(green, g.unwrap());
    }
    #[test]
    fn color_accepts_single_letter_uppercase(){
        let green = Color::Green;
        let G = Color::from_str("G");
        assert_eq!(green, G.unwrap());
    }

    #[test]
    fn color_accepts_brackets(){
        let green = Color::Green;
        let G = Color::from_str("{green}");
        assert_eq!(green, G.unwrap());
    }
}

All the other tests work fine, but I'm also a bit confused by the Color::VARIANTS behavior-

I assumed that it would be ALL of the at least named variants-

So...

Green, G, {green}

but for some reason if i println it, i only get: ["{green}", ascii_case_insensitive]

but all my tests pass. Is that the expected behavior for the Color::VARIANTS?