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

Adding support for int and bool EnumProperties #259

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 8 additions & 11 deletions strum/src/lib.rs
Expand Up @@ -136,9 +136,9 @@ 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, string, integer and boolean literals
/// are supported in attributes, the other methods will be implemented as additional
/// attribute types become stabilized.
///
/// # Example
///
Expand All @@ -152,24 +152,21 @@ pub trait EnumMessage {
/// #[strum(props(Teacher="Ms.Frizzle", Room="201"))]
/// History,
/// #[strum(props(Teacher="Mr.Smith"))]
/// #[strum(props(Room="103"))]
/// #[strum(props(Room=103))]
/// Mathematics,
/// #[strum(props(Time="2:30"))]
/// Science,
/// }
///
/// let history = Class::History;
/// assert_eq!("Ms.Frizzle", history.get_str("Teacher").unwrap());
/// let maths = Class::Mathematics;
/// assert_eq!(103, maths.get_usize("Room").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_usize(&self, prop: &str) -> Option<usize>;
fn get_bool(&self, prop: &str) -> Option<bool>;
}

/// A cheap reference-to-reference conversion. Used to convert a value to a
Expand Down
2 changes: 1 addition & 1 deletion strum_macros/Cargo.toml
Expand Up @@ -26,4 +26,4 @@ rustversion = "1.0"
syn = { version = "1.0", features = ["parsing", "extra-traits"] }

[dev-dependencies]
strum = "0.24"
strum = { path = "../strum" }
Copy link
Owner

Choose a reason for hiding this comment

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

I don't believe you can publish to crates.io with a path dependency.

5 changes: 3 additions & 2 deletions strum_macros/src/helpers/metadata.rs
Expand Up @@ -10,6 +10,7 @@ use syn::{
};

use super::case_style::CaseStyle;
use super::PropertyValue;

pub mod kw {
use syn::custom_keyword;
Expand Down Expand Up @@ -184,7 +185,7 @@ pub enum VariantMeta {
},
Props {
kw: kw::props,
props: Vec<(LitStr, LitStr)>,
props: Vec<(LitStr, PropertyValue)>,
},
}

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

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

impl Parse for Prop {
fn parse(input: ParseStream) -> syn::Result<Self> {
Expand Down
30 changes: 30 additions & 0 deletions strum_macros/src/helpers/mod.rs
Expand Up @@ -30,3 +30,33 @@ pub fn occurrence_error<T: ToTokens>(fst: T, snd: T, attr: &str) -> syn::Error {
e.combine(syn::Error::new_spanned(fst, "first one here"));
e
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PropertyValue {
Str(syn::LitStr),
Num(syn::LitInt),
Bool(syn::LitBool),
}

impl syn::parse::Parse for PropertyValue {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let value = if input.peek(syn::LitBool) {
PropertyValue::Bool(input.parse()?)
} else if input.peek(syn::LitInt) {
PropertyValue::Num(input.parse()?)
} else {
PropertyValue::Str(input.parse()?)
};
Ok(value)
}
}

impl quote::ToTokens for PropertyValue {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
match self {
PropertyValue::Str(s) => s.to_tokens(tokens),
PropertyValue::Num(n) => n.to_tokens(tokens),
PropertyValue::Bool(b) => b.to_tokens(tokens),
}
}
}
3 changes: 2 additions & 1 deletion strum_macros/src/helpers/variant_props.rs
Expand Up @@ -4,6 +4,7 @@ use syn::{Ident, LitStr, Variant};
use super::case_style::{CaseStyle, CaseStyleHelpers};
use super::metadata::{kw, VariantExt, VariantMeta};
use super::occurrence_error;
use super::PropertyValue;

pub trait HasStrumVariantProperties {
fn get_variant_properties(&self) -> syn::Result<StrumVariantProperties>;
Expand All @@ -17,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 string_props: Vec<(LitStr, PropertyValue)>,
serialize: Vec<LitStr>,
to_string: Option<LitStr>,
ident: Option<Ident>,
Expand Down
14 changes: 7 additions & 7 deletions strum_macros/src/lib.rs
Expand Up @@ -560,12 +560,12 @@ pub fn enum_messages(input: proc_macro::TokenStream) -> proc_macro::TokenStream
/// Add custom properties to enum variants.
///
/// Enables the encoding of arbitary constants into enum variants. This method
/// currently only supports adding additional string values. Other types of literals are still
/// experimental in the rustc compiler. The generated code works by nesting match statements.
/// The first match statement matches on the type of the enum, and the inner match statement
/// matches on the name of the property requested. This design works well for enums with a small
/// number of variants and properties, but scales linearly with the number of variants so may not
/// be the best choice in all situations.
/// currently only supports adding additional string, integer and boolean values. Other types
/// of literals are still experimental in the rustc compiler. The generated code works by
/// nesting match statements. The first match statement matches on the type of the enum,
/// and the inner match statement matches on the name of the property requested. This design
/// works well for enums with a small number of variants and properties, but scales linearly
/// with the number of variants so may not be the best choice in all situations.
///
/// ```
///
Expand Down Expand Up @@ -630,7 +630,7 @@ pub fn enum_properties(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
/// // Bring trait into scope
/// use std::str::FromStr;
/// use strum::{IntoEnumIterator, EnumMessage};
/// use strum_macros::{EnumDiscriminants, EnumIter, EnumString, EnumMessage};
/// use strum_macros::{EnumDiscriminants, EnumIter, EnumString};
///
/// #[derive(Debug)]
/// struct NonDefault;
Expand Down
55 changes: 49 additions & 6 deletions strum_macros/src/macros/enum_properties.rs
Expand Up @@ -2,6 +2,7 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::{Data, DeriveInput, Fields};

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

pub fn enum_properties_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
Expand All @@ -14,7 +15,9 @@ 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 string_props = Vec::new();
let mut num_props = Vec::new();
let mut bool_props = Vec::new();
for variant in variants {
let ident = &variant.ident;
let variant_properties = variant.get_variant_properties()?;
Expand All @@ -33,31 +36,71 @@ pub fn enum_properties_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
};

for (key, value) in variant_properties.string_props {
string_arms.push(quote! { #key => ::core::option::Option::Some( #value )});
match value {
PropertyValue::Str(s) => {
string_arms.push(quote! { #key => ::core::option::Option::Some( #s )})
}
PropertyValue::Num(n) => {
num_arms.push(quote! { #key => ::core::option::Option::Some( #n )})
}
PropertyValue::Bool(b) => {
bool_arms.push(quote! { #key => ::core::option::Option::Some( #b )})
}
}
}

string_arms.push(quote! { _ => ::core::option::Option::None });
bool_arms.push(quote! { _ => ::core::option::Option::None });
num_arms.push(quote! { _ => ::core::option::Option::None });

arms.push(quote! {
string_props.push(quote! {
&#name::#ident #params => {
match prop {
#(#string_arms),*
}
}
});
bool_props.push(quote! {
&#name::#ident #params => {
match prop {
#(#bool_arms),*
}
}
});
num_props.push(quote! {
&#name::#ident #params => {
match prop {
#(#num_arms),*
}
}
});
}

if arms.len() < variants.len() {
arms.push(quote! { _ => ::core::option::Option::None });
if string_props.len() < variants.len() {
string_props.push(quote! { _ => ::core::option::Option::None });
}
if bool_props.len() < variants.len() {
bool_props.push(quote! { _ => ::core::option::Option::None });
}
if num_props.len() < variants.len() {
num_props.push(quote! { _ => ::core::option::Option::None });
}

Ok(quote! {
impl #impl_generics #strum_module_path::EnumProperty for #name #ty_generics #where_clause {
fn get_str(&self, prop: &str) -> ::core::option::Option<&'static str> {
match self {
#(#arms),*
#(#string_props),*
}
}
fn get_bool(&self, prop: &str) -> ::core::option::Option<bool> {
match self {
#(#bool_props),*
}
}
fn get_usize(&self, prop: &str) -> ::core::option::Option<usize> {
match self {
#(#num_props),*
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions strum_tests/tests/enum_props.rs
Expand Up @@ -39,9 +39,16 @@ fn crate_module_path_test() {
enum Test {
#[strum(props(key = "value"))]
A,
#[strum(props(answer = 42))]
B,
#[strum(props(to_be = false))]
C,
}

let a = Test::A;
assert_eq!("value", a.get_str("key").unwrap());
let b = Test::B;
assert_eq!(42, b.get_usize("answer").unwrap());
let c = Test::C;
assert_eq!(false, c.get_bool("to_be").unwrap());
}