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

Implement get_int/get_bool for properties #391

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions strum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ pub trait EnumMessage {

/// `EnumProperty` is a trait that makes it possible to store additional information
/// with enum variants. This trait is designed to be used with the macro of the same
/// name in the `strum_macros` crate. Currently, the only string literals are supported
/// in attributes, the other methods will be implemented as additional attribute types
/// become stabilized.
/// name in the `strum_macros` crate. Currently, the string, integer and bool literals
/// are supported in attributes.
///
/// # Example
///
Expand All @@ -168,27 +167,24 @@ pub trait EnumMessage {
///
/// #[derive(PartialEq, Eq, Debug, EnumProperty)]
/// enum Class {
/// #[strum(props(Teacher="Ms.Frizzle", Room="201"))]
/// #[strum(props(Teacher="Ms.Frizzle", Room="201", students=16, mandatory=true))]
/// History,
/// #[strum(props(Teacher="Mr.Smith"))]
/// #[strum(props(Room="103"))]
/// #[strum(props(Room="103", students=10))]
/// Mathematics,
/// #[strum(props(Time="2:30"))]
/// #[strum(props(Time="2:30", mandatory=true))]
/// Science,
/// }
///
/// let history = Class::History;
/// assert_eq!("Ms.Frizzle", history.get_str("Teacher").unwrap());
/// assert_eq!(16, history.get_int("students").unwrap());
/// assert!(history.get_bool("mandatory").unwrap());
/// ```
pub trait EnumProperty {
fn get_str(&self, prop: &str) -> Option<&'static str>;
fn get_int(&self, _prop: &str) -> Option<usize> {
Option::None
}

fn get_bool(&self, _prop: &str) -> Option<bool> {
Option::None
}
fn get_int(&self, _prop: &str) -> Option<i64>;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this is an API break. But also the old get_int did nothing so most likely no one used it and it is okay.

fn get_bool(&self, _prop: &str) -> Option<bool>;
}

/// A cheap reference-to-reference conversion. Used to convert a value to a
Expand Down
4 changes: 2 additions & 2 deletions strum_macros/src/helpers/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub enum VariantMeta {
},
Props {
kw: kw::props,
props: Vec<(LitStr, LitStr)>,
props: Vec<(LitStr, Lit)>,
},
}

Expand Down Expand Up @@ -239,7 +239,7 @@ impl Parse for VariantMeta {
}
}

struct Prop(Ident, LitStr);
struct Prop(Ident, Lit);

impl Parse for Prop {
fn parse(input: ParseStream) -> syn::Result<Self> {
Expand Down
6 changes: 3 additions & 3 deletions strum_macros/src/helpers/variant_props.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::default::Default;
use syn::{Ident, LitStr, Variant};
use syn::{Ident, Lit, LitStr, Variant};

use super::case_style::{CaseStyle, CaseStyleHelpers};
use super::metadata::{kw, VariantExt, VariantMeta};
Expand All @@ -18,7 +18,7 @@ pub struct StrumVariantProperties {
pub message: Option<LitStr>,
pub detailed_message: Option<LitStr>,
pub documentation: Vec<LitStr>,
pub string_props: Vec<(LitStr, LitStr)>,
pub props: Vec<(LitStr, Lit)>,
serialize: Vec<LitStr>,
pub to_string: Option<LitStr>,
ident: Option<Ident>,
Expand Down Expand Up @@ -143,7 +143,7 @@ impl HasStrumVariantProperties for Variant {
output.ascii_case_insensitive = Some(value);
}
VariantMeta::Props { props, .. } => {
output.string_props.extend(props);
output.props.extend(props);
}
}
}
Expand Down
84 changes: 68 additions & 16 deletions strum_macros/src/macros/enum_properties.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
use std::collections::HashMap;

use proc_macro2::TokenStream;
use quote::quote;
use syn::{Data, DeriveInput, Fields};
use syn::{Data, DeriveInput, Fields, Lit};

use crate::helpers::{non_enum_error, HasStrumVariantProperties, HasTypeProperties};

#[derive(Hash, PartialEq, Eq)]
enum PropertyType {
String,
Integer,
Bool,
}

const PROPERTY_TYPES: [PropertyType; 3] = [
PropertyType::String,
PropertyType::Integer,
PropertyType::Bool,
];

pub fn enum_properties_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
let name = &ast.ident;
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
Expand All @@ -14,11 +29,12 @@ pub fn enum_properties_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
let type_properties = ast.get_type_properties()?;
let strum_module_path = type_properties.crate_module_path();

let mut arms = Vec::new();
let mut built_arms: HashMap<_, _> = PROPERTY_TYPES.iter().map(|p| (p, Vec::new())).collect();

for variant in variants {
let ident = &variant.ident;
let variant_properties = variant.get_variant_properties()?;
let mut string_arms = Vec::new();
let mut arms: HashMap<_, _> = PROPERTY_TYPES.iter().map(|p| (p, Vec::new())).collect();
// But you can disable the messages.
if variant_properties.disabled.is_some() {
continue;
Expand All @@ -30,33 +46,69 @@ pub fn enum_properties_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
Fields::Named(..) => quote! { {..} },
};

for (key, value) in variant_properties.string_props {
string_arms.push(quote! { #key => ::core::option::Option::Some( #value )});
}
for (key, value) in variant_properties.props {
let property_type = match value {
Lit::Str(..) => PropertyType::String,
Lit::Bool(..) => PropertyType::Bool,
Lit::Int(..) => PropertyType::Integer,
_ => todo!("TODO"),
};

string_arms.push(quote! { _ => ::core::option::Option::None });
arms.get_mut(&property_type)
.unwrap()
.push(quote! { #key => ::core::option::Option::Some( #value )});
}

arms.push(quote! {
&#name::#ident #params => {
match prop {
#(#string_arms),*
for property in &PROPERTY_TYPES {
arms.get_mut(&property)
.unwrap()
.push(quote! { _ => ::core::option::Option::None });
let arms_as_string = &arms[property];
built_arms.get_mut(&property).unwrap().push(quote! {
&#name::#ident #params => {
match prop {
#(#arms_as_string),*
}
}
}
});
});
}
}

if arms.len() < variants.len() {
arms.push(quote! { _ => ::core::option::Option::None });
for (_, arms) in built_arms.iter_mut() {
if arms.len() < variants.len() {
arms.push(quote! { _ => ::core::option::Option::None });
}
}

let (built_string_arms, built_int_arms, built_bool_arms) = (
&built_arms[&PropertyType::String],
&built_arms[&PropertyType::Integer],
&built_arms[&PropertyType::Bool],
);

Ok(quote! {
impl #impl_generics #strum_module_path::EnumProperty for #name #ty_generics #where_clause {
#[inline]
fn get_str(&self, prop: &str) -> ::core::option::Option<&'static str> {
match self {
#(#arms),*
#(#built_string_arms),*
}
}

#[inline]
fn get_int(&self, prop: &str) -> ::core::option::Option<i64> {
match self {
#(#built_int_arms),*
}
}

#[inline]
fn get_bool(&self, prop: &str) -> ::core::option::Option<bool> {
match self {
#(#built_bool_arms),*
}
}

}
})
}
27 changes: 27 additions & 0 deletions strum_tests/tests/enum_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,30 @@ fn crate_module_path_test() {
let a = Test::A;
assert_eq!("value", a.get_str("key").unwrap());
}

#[derive(Debug, EnumProperty)]
enum TestGet {
#[strum(props(weight = 42, flat = true, big = false))]
A,
#[strum(props(weight = -42, flat = false))]
B,
C,
}

#[test]
fn get_bool_test() {
const BOOL_KEY: &str = "flat";
assert_eq!(Some(true), TestGet::A.get_bool(BOOL_KEY));
assert_eq!(Some(false), TestGet::B.get_bool(BOOL_KEY));
assert_eq!(None, TestGet::C.get_bool(BOOL_KEY));
assert_eq!(None, TestGet::A.get_bool("weight"));
}

#[test]
fn get_int_test() {
const INT_KEY: &str = "weight";
assert_eq!(Some(42), TestGet::A.get_int(INT_KEY));
assert_eq!(Some(-42), TestGet::B.get_int(INT_KEY));
assert_eq!(None, TestGet::C.get_int(INT_KEY));
assert_eq!(None, TestGet::A.get_int("flat"));
}