Repository: Peternator7/strum
Pull request #84 (2020-02-07 16:48:27)

Apply serialize_all casing to get_serializations
I'm not sure if this is intentional, but it seems to make sense to convert case for `get_serializations`. When `to_string` is already being used for display you may still want to use the automatic case conversion for serialization. 

Peternator7: I'd agree it seems more consistent. Would you mind adding a note to the changelog as well?
t-mw: Ok great. Sure, I've added the note.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c706ddd..cbcb064 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,9 @@
 ## Unreleased
 
 * Only copy across `"doc", "cfg", "allow", "deny"` attributes from main enum variants to discriminant variants. [#73](https://github.com/Peternator7/strum/issues/73)
+* The formatting of generated serialization variants returned by `get_serializations()` from an
+  enum that derives `EnumMessage` is now affected by the `serialize_all` property on the enum.
+  [#84](https://github.com/Peternator7/strum/pull/84)
 
 ## 0.17.1
 
diff --git a/strum_macros/src/macros/enum_messages.rs b/strum_macros/src/macros/enum_messages.rs
index 24ec451..d46b66d 100644
--- a/strum_macros/src/macros/enum_messages.rs
+++ b/strum_macros/src/macros/enum_messages.rs
@@ -1,7 +1,8 @@
 use proc_macro2::TokenStream;
 use syn;
 
-use helpers::{extract_meta, MetaIteratorHelpers};
+use crate::helpers::case_style::CaseStyle;
+use helpers::{extract_meta, CaseStyleHelpers, MetaIteratorHelpers};
 
 pub fn enum_message_inner(ast: &syn::DeriveInput) -> TokenStream {
     let name = &ast.ident;
@@ -11,6 +12,11 @@ pub fn enum_message_inner(ast: &syn::DeriveInput) -> TokenStream {
         _ => panic!("EnumMessage only works on Enums"),
     };
 
+    let type_meta = extract_meta(&ast.attrs);
+    let case_style = type_meta
+        .find_unique_property("strum", "serialize_all")
+        .map(|style| CaseStyle::from(style.as_ref()));
+
     let mut arms = Vec::new();
     let mut detailed_arms = Vec::new();
     let mut serializations = Vec::new();
@@ -32,7 +38,7 @@ pub fn enum_message_inner(ast: &syn::DeriveInput) -> TokenStream {
         {
             let mut serialization_variants = meta.find_properties("strum", "serialize");
             if serialization_variants.len() == 0 {
-                serialization_variants.push(ident.to_string());
+                serialization_variants.push(ident.convert_case(case_style));
             }
 
             let count = serialization_variants.len();


Test patch:
diff --git a/strum_tests/tests/enum_message.rs b/strum_tests/tests/enum_message.rs
index bcde8bf..ad617b8 100644
--- a/strum_tests/tests/enum_message.rs
+++ b/strum_tests/tests/enum_message.rs
@@ -54,3 +54,30 @@ fn disabled_message() {
     assert_eq!(None, (Pets::Hamster).get_message());
     assert_eq!(None, (Pets::Hamster).get_detailed_message());
 }
+
+#[derive(Debug, Eq, PartialEq, EnumMessage)]
+#[strum(serialize_all = "kebab_case")]
+enum Brightness {
+    DarkBlack,
+    Dim {
+        glow: usize,
+    },
+    #[strum(serialize = "bright")]
+    BrightWhite,
+}
+
+#[test]
+fn get_serializations() {
+    assert_eq!(
+        vec!["dark-black"],
+        (Brightness::DarkBlack).get_serializations()
+    );
+    assert_eq!(
+        vec!["dim"],
+        (Brightness::Dim { glow: 1 }).get_serializations()
+    );
+    assert_eq!(
+        vec!["bright"],
+        (Brightness::BrightWhite).get_serializations()
+    );
+}