Skip to content

Commit

Permalink
More attribute refactoring (#134)
Browse files Browse the repository at this point in the history
* Remove unused method NestedMetaHelpers::expect_lit

* Use references match instead of ref patterns

* Use custom meta types instead of syn::Meta

This should improve both readability of the code and the quality of
error messages.

* Replace TryFrom<&str> with FromStr

* Restore Rust 1.31.1 compatibility

* Re-run `cargo fmt`
  • Loading branch information
jplatte authored Oct 15, 2020
1 parent 4ad3992 commit 25eaa5a
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 318 deletions.
2 changes: 1 addition & 1 deletion strum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ macro_rules! DocumentMacroRexports {
// for docsrs. You can do a weird thing where you rename the macro
// and then reference it through strum. The renaming feature should be deprecated now that
// 2018 edition is almost 2 years old, but we'll need to give people some time to do that.
DocumentMacroRexports!{
DocumentMacroRexports! {
AsRefStr,
AsStaticStr,
Display,
Expand Down
63 changes: 42 additions & 21 deletions strum_macros/src/helpers/case_style.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use heck::{CamelCase, KebabCase, MixedCase, ShoutySnakeCase, SnakeCase, TitleCase};
use syn::Ident;
use std::str::FromStr;
use syn::{
parse::{Parse, ParseStream},
Ident, LitStr,
};

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CaseStyle {
Expand All @@ -15,9 +19,41 @@ pub enum CaseStyle {
PascalCase,
}

impl<'s> From<&'s str> for CaseStyle {
fn from(text: &'s str) -> CaseStyle {
match text {
const VALID_CASE_STYLES: &[&str] = &[
"camelCase",
"PascalCase",
"kebab-case",
"snake_case",
"SCREAMING_SNAKE_CASE",
"SCREAMING-KEBAB-CASE",
"lowercase",
"UPPERCASE",
"title_case",
"mixed_case",
];

impl Parse for CaseStyle {
fn parse(input: ParseStream) -> syn::Result<Self> {
let text = input.parse::<LitStr>()?;
let val = text.value();

val.as_str().parse().map_err(|_| {
syn::Error::new_spanned(
&text,
format!(
"Unexpected case style for serialize_all: `{}`. Valid values are: `{:?}`",
val, VALID_CASE_STYLES
),
)
})
}
}

impl FromStr for CaseStyle {
type Err = ();

fn from_str(text: &str) -> Result<CaseStyle, ()> {
Ok(match text {
"camel_case" | "PascalCase" => CaseStyle::PascalCase,
"camelCase" => CaseStyle::CamelCase,
"snake_case" | "snek_case" => CaseStyle::SnakeCase,
Expand All @@ -30,23 +66,8 @@ impl<'s> From<&'s str> for CaseStyle {
"mixed_case" => CaseStyle::MixedCase,
"lowercase" => CaseStyle::LowerCase,
"UPPERCASE" => CaseStyle::UpperCase,
_ => panic!(
"Unexpected case style for serialize_all: `{}`. Valid values are: `{:?}`",
text,
[
"camelCase",
"PascalCase",
"kebab-case",
"snake_case",
"SCREAMING_SNAKE_CASE",
"SCREAMING-KEBAB-CASE",
"lowercase",
"UPPERCASE",
"title_case",
"mixed_case",
]
),
}
_ => return Err(()),
})
}
}

Expand Down
56 changes: 0 additions & 56 deletions strum_macros/src/helpers/has_metadata.rs

This file was deleted.

63 changes: 0 additions & 63 deletions strum_macros/src/helpers/meta_helpers.rs

This file was deleted.

Loading

0 comments on commit 25eaa5a

Please sign in to comment.