1
0
mirror of https://github.com/danog/strum.git synced 2024-12-03 09:57:55 +01:00

Ran rustfmt on the whole tree and added a note about compatibility (#44)

* Ran rustfmt on the whole tree and added a note about compatibility

* Removing a doc-comment
This commit is contained in:
Peter Glotfelty 2019-03-03 23:27:59 -08:00 committed by GitHub
parent 7605c67ec6
commit 68d7fba35b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 107 additions and 74 deletions

View File

@ -3,6 +3,7 @@ rust:
- stable - stable
- beta - beta
- nightly - nightly
- 1.26.0
matrix: matrix:
allow_failures: allow_failures:
- rust: nightly - rust: nightly

View File

@ -7,6 +7,12 @@
Strum is a set of macros and traits for working with Strum is a set of macros and traits for working with
enums and strings easier in Rust. enums and strings easier in Rust.
# Compatibility
Strum is compatible with versions of rustc >= 1.26.0. That's the earliest version of stable rust that supports
impl trait. Pull Requests that improve compatibility with older versions are welcome, but new feature work
will focus on the current version of rust with an effort to avoid breaking compatibility with older versions.
# Including Strum in Your Project # Including Strum in Your Project
Import strum and strum_macros into your project by adding the following lines to your Import strum and strum_macros into your project by adding the following lines to your
@ -14,8 +20,8 @@ Cargo.toml. Strum_macros contains the macros needed to derive all the traits in
```toml ```toml
[dependencies] [dependencies]
strum = "0.13.0" strum = "0.14.0"
strum_macros = "0.13.0" strum_macros = "0.14.0"
``` ```
And add these lines to the root of your project, either lib.rs or main.rs. And add these lines to the root of your project, either lib.rs or main.rs.

View File

@ -13,7 +13,7 @@ homepage = "https://github.com/Peternator7/strum"
readme = "../README.md" readme = "../README.md"
[dev-dependencies] [dev-dependencies]
strum_macros = { path = "../strum_macros", version = "0.13.0" } strum_macros = { path = "../strum_macros", version = "0.14.0" }
[badges] [badges]
travis-ci = { repository = "Peternator7/strum" } travis-ci = { repository = "Peternator7/strum" }

View File

@ -1,6 +1,6 @@
[package] [package]
name = "strum_macros" name = "strum_macros"
version = "0.13.0" version = "0.14.0"
authors = ["Peter Glotfelty <peglotfe@microsoft.com>"] authors = ["Peter Glotfelty <peglotfe@microsoft.com>"]
license = "MIT" license = "MIT"

View File

@ -6,6 +6,7 @@ pub(crate) fn enum_count_inner(ast: &syn::DeriveInput) -> TokenStream {
syn::Data::Enum(ref v) => v.variants.len(), syn::Data::Enum(ref v) => v.variants.len(),
_ => panic!("EnumCount can only be used with enums"), _ => panic!("EnumCount can only be used with enums"),
}; };
// Used in the quasi-quotation below as `#name` // Used in the quasi-quotation below as `#name`
let name = &ast.ident; let name = &ast.ident;
let const_name = &syn::Ident::new( let const_name = &syn::Ident::new(
@ -17,7 +18,7 @@ pub(crate) fn enum_count_inner(ast: &syn::DeriveInput) -> TokenStream {
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
quote! { quote! {
// The generated impl // Implementation
impl #impl_generics ::strum::EnumCount for #name #ty_generics #where_clause { impl #impl_generics ::strum::EnumCount for #name #ty_generics #where_clause {
fn count() -> usize { fn count() -> usize {
#n #n

View File

@ -19,6 +19,7 @@ pub fn enum_discriminants_inner(ast: &syn::DeriveInput) -> TokenStream {
let discriminant_attrs = get_meta_list(type_meta.iter(), "strum_discriminants") let discriminant_attrs = get_meta_list(type_meta.iter(), "strum_discriminants")
.flat_map(|meta| extract_list_metas(meta).collect::<Vec<_>>()) .flat_map(|meta| extract_list_metas(meta).collect::<Vec<_>>())
.collect::<Vec<&syn::Meta>>(); .collect::<Vec<&syn::Meta>>();
let derives = get_meta_list(discriminant_attrs.iter().map(|&m| m), "derive") let derives = get_meta_list(discriminant_attrs.iter().map(|&m| m), "derive")
.flat_map(extract_list_metas) .flat_map(extract_list_metas)
.filter_map(get_meta_ident) .filter_map(get_meta_ident)
@ -44,7 +45,8 @@ pub fn enum_discriminants_inner(ast: &syn::DeriveInput) -> TokenStream {
filter_metas(discriminant_attrs.iter().map(|&m| m), |meta| match meta { filter_metas(discriminant_attrs.iter().map(|&m| m), |meta| match meta {
syn::Meta::List(ref metalist) => metalist.ident != "derive" && metalist.ident != "name", syn::Meta::List(ref metalist) => metalist.ident != "derive" && metalist.ident != "name",
_ => true, _ => true,
}).map(|meta| quote! { #[ #meta ] }) })
.map(|meta| quote! { #[ #meta ] })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// Add the variants without fields, but exclude the `strum` meta item // Add the variants without fields, but exclude the `strum` meta item
@ -99,7 +101,8 @@ pub fn enum_discriminants_inner(ast: &syn::DeriveInput) -> TokenStream {
}; };
quote! { #name::#ident #params => #discriminants_name::#ident } quote! { #name::#ident #params => #discriminants_name::#ident }
}).collect::<Vec<_>>(); })
.collect::<Vec<_>>();
let from_fn_body = quote! { match val { #(#arms),* } }; let from_fn_body = quote! { match val { #(#arms),* } };
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();

View File

@ -20,7 +20,8 @@ fn extract_properties(meta: &[Meta]) -> Vec<(&syn::Ident, &syn::Lit)> {
} }
} }
_ => None, _ => None,
}).flat_map(|prop| prop) })
.flat_map(|prop| prop)
.filter_map(|prop| match *prop { .filter_map(|prop| match *prop {
NestedMeta::Meta(Meta::List(MetaList { NestedMeta::Meta(Meta::List(MetaList {
ref ident, ref ident,
@ -34,14 +35,16 @@ fn extract_properties(meta: &[Meta]) -> Vec<(&syn::Ident, &syn::Lit)> {
} }
} }
_ => None, _ => None,
}).flat_map(|prop| prop) })
.flat_map(|prop| prop)
// Only look at key value pairs // Only look at key value pairs
.filter_map(|prop| match *prop { .filter_map(|prop| match *prop {
NestedMeta::Meta(Meta::NameValue(MetaNameValue { NestedMeta::Meta(Meta::NameValue(MetaNameValue {
ref ident, ref lit, .. ref ident, ref lit, ..
})) => Some((ident, lit)), })) => Some((ident, lit)),
_ => None, _ => None,
}).collect() })
.collect()
} }
pub fn enum_properties_inner(ast: &syn::DeriveInput) -> TokenStream { pub fn enum_properties_inner(ast: &syn::DeriveInput) -> TokenStream {

View File

@ -107,7 +107,8 @@ pub fn extract_attrs(meta: &[Meta], attr: &str, prop: &str) -> Vec<String> {
} }
} }
_ => None, _ => None,
}).flat_map(|nested| nested) })
.flat_map(|nested| nested)
// Get all the inner elements as long as they start with ser. // Get all the inner elements as long as they start with ser.
.filter_map(|meta| match *meta { .filter_map(|meta| match *meta {
NestedMeta::Meta(Meta::NameValue(MetaNameValue { NestedMeta::Meta(Meta::NameValue(MetaNameValue {
@ -122,7 +123,8 @@ pub fn extract_attrs(meta: &[Meta], attr: &str, prop: &str) -> Vec<String> {
} }
} }
_ => None, _ => None,
}).collect() })
.collect()
} }
pub fn unique_attr(attrs: &[Meta], attr: &str, prop: &str) -> Option<String> { pub fn unique_attr(attrs: &[Meta], attr: &str, prop: &str) -> Option<String> {

View File

@ -1,6 +1,6 @@
[package] [package]
name = "strum_tests" name = "strum_tests"
version = "0.13.0" version = "0.14.0"
authors = ["Peter Glotfelty <peglotfe@microsoft.com>"] authors = ["Peter Glotfelty <peglotfe@microsoft.com>"]
[dependencies] [dependencies]

View File

@ -1 +1,17 @@
extern crate strum;
#[macro_use]
extern crate strum_macros;
#[allow(dead_code)]
#[derive(Debug, Eq, PartialEq, EnumString, ToString, EnumCount, EnumDiscriminants)]
pub enum Color {
/// Docs on red
#[strum(to_string = "RedRed")]
Red,
#[strum(serialize = "b", to_string = "blue")]
Blue { hue: usize },
#[strum(serialize = "y", serialize = "yellow")]
Yellow,
#[strum(disabled = "true")]
Green(String),
}

View File

@ -3,8 +3,9 @@ extern crate strum;
extern crate strum_macros; extern crate strum_macros;
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug, Eq, PartialEq, EnumString, ToString)] #[derive(Debug, Eq, PartialEq, EnumString, ToString, EnumCount, EnumDiscriminants)]
enum Color { enum Color {
/// Random Docs
#[strum(to_string = "RedRed")] #[strum(to_string = "RedRed")]
Red, Red,
#[strum(serialize = "b", to_string = "blue")] #[strum(serialize = "b", to_string = "blue")]

View File

@ -124,7 +124,7 @@ fn split_attributes_test() {
#[strum_discriminants( #[strum_discriminants(
name(PassThroughBoo), name(PassThroughBoo),
derive(Display, EnumIter, EnumString), derive(Display, EnumIter, EnumString),
strum(serialize_all = "snake_case"), strum(serialize_all = "snake_case")
)] )]
enum PassThrough { enum PassThrough {
DarkBlack(bool), DarkBlack(bool),
@ -168,7 +168,7 @@ fn from_ref_test() {
struct Rara; struct Rara;
#[derive(Debug, Eq, PartialEq, EnumDiscriminants)] #[derive(Debug, Eq, PartialEq, EnumDiscriminants)]
#[strum_discriminants(name(EnumIntoComplexVars),)] #[strum_discriminants(name(EnumIntoComplexVars))]
enum EnumIntoComplex<'a, T: 'a> { enum EnumIntoComplex<'a, T: 'a> {
A(&'a T), A(&'a T),
} }