Skip to content

Commit

Permalink
Apply serialize_all casing to get_serializations (#84)
Browse files Browse the repository at this point in the history
* Apply serialize_all casing to get_serializations

* Update changelog

* Fix grammar
  • Loading branch information
t-mw committed Feb 10, 2020
1 parent 2b83ece commit 043b60f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions 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;
Expand All @@ -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();
Expand All @@ -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();
Expand Down
27 changes: 27 additions & 0 deletions strum_tests/tests/enum_message.rs
Expand Up @@ -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()
);
}

0 comments on commit 043b60f

Please sign in to comment.