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

More attribute refactoring #134

Merged
merged 6 commits into from Oct 15, 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
2 changes: 1 addition & 1 deletion strum/src/lib.rs
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
@@ -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.