Skip to content

Commit

Permalink
Requested changes in review
Browse files Browse the repository at this point in the history
  • Loading branch information
MendyBerger committed Jul 31, 2023
1 parent e0f81e9 commit 4014b3e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 26 deletions.
17 changes: 17 additions & 0 deletions strum_macros/src/helpers/case_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,20 @@ fn test_convert_case() {
assert_eq!("TestMe", id.convert_case(Some(CaseStyle::PascalCase)));
assert_eq!("Test-Me", id.convert_case(Some(CaseStyle::TrainCase)));
}

/// heck doesn't treat numbers as new words, but this function does.
/// E.g. for input `Hello2You`, heck would output `hello2_you`, and snakify would output `hello_2_you`.
pub fn snakify(s: &str) -> String {
let mut output: Vec<char> = s.to_string().to_snake_case().chars().collect();
let mut num_starts = vec![];
for (pos, c) in output.iter().enumerate() {
if c.is_digit(10) && pos != 0 && !output[pos - 1].is_digit(10) {
num_starts.push(pos);
}
}
// need to do in reverse, because after inserting, all chars after the point of insertion are off
for i in num_starts.into_iter().rev() {
output.insert(i, '_')
}
output.into_iter().collect()
}
4 changes: 1 addition & 3 deletions strum_macros/src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
pub use self::case_style::CaseStyleHelpers;
pub use self::snakify::snakify;
pub use self::case_style::{CaseStyleHelpers, snakify};
pub use self::type_props::HasTypeProperties;
pub use self::variant_props::HasStrumVariantProperties;

pub mod case_style;
mod metadata;
mod snakify;
pub mod type_props;
pub mod variant_props;

Expand Down
18 changes: 0 additions & 18 deletions strum_macros/src/helpers/snakify.rs

This file was deleted.

4 changes: 2 additions & 2 deletions strum_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ pub fn enum_is(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
toks.into()
}

/// Generated `try_as_*()` methods for all unnamed variants.
/// Generated `try_as_*()` methods for all tuple-style variants.
/// E.g. `Message.try_as_write()`.
///
/// These methods will only be generated for unnamed variants, not for named or unit variants.
/// These methods will only be generated for tuple-style variants, not for named or unit variants.
///
/// ```
/// use strum_macros::EnumTryAs;
Expand Down
6 changes: 3 additions & 3 deletions strum_macros/src/macros/enum_try_as.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn enum_try_as_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
Some(quote! {
#[must_use]
#[inline]
pub fn #move_fn_name(self) -> Option<(#(#types),*)> {
pub fn #move_fn_name(self) -> ::core::option::Option<(#(#types),*)> {
match self {
#enum_name::#variant_name (#(#field_names),*) => Some((#(#field_names),*)),
_ => None
Expand All @@ -46,7 +46,7 @@ pub fn enum_try_as_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {

#[must_use]
#[inline]
pub const fn #ref_fn_name(&self) -> Option<(#(&#types),*)> {
pub const fn #ref_fn_name(&self) -> ::core::option::Option<(#(&#types),*)> {
match self {
#enum_name::#variant_name (#(#field_names),*) => Some((#(#field_names),*)),
_ => None
Expand All @@ -55,7 +55,7 @@ pub fn enum_try_as_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {

#[must_use]
#[inline]
pub fn #mut_fn_name(&mut self) -> Option<(#(&mut #types),*)> {
pub fn #mut_fn_name(&mut self) -> ::core::option::Option<(#(&mut #types),*)> {
match self {
#enum_name::#variant_name (#(#field_names),*) => Some((#(#field_names),*)),
_ => None
Expand Down
4 changes: 4 additions & 0 deletions strum_tests/tests/enum_try_as.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ enum Foo {
#[strum(disabled)]
#[allow(dead_code)]
Disabled(u32),
#[allow(dead_code)]
Unit,
#[allow(dead_code)]
Named { _a: u32, _b: String },
}

#[test]
Expand Down

0 comments on commit 4014b3e

Please sign in to comment.