Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply serialize_all casing to get_serializations #84

Merged
merged 4 commits into from Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
);
}