From 4014b3e2d10edbc85f1548fd020e6b6ba170e161 Mon Sep 17 00:00:00 2001 From: Mendy Berger <12537668+MendyBerger@users.noreply.github.com> Date: Mon, 31 Jul 2023 16:44:38 -0400 Subject: [PATCH] Requested changes in review --- strum_macros/src/helpers/case_style.rs | 17 +++++++++++++++++ strum_macros/src/helpers/mod.rs | 4 +--- strum_macros/src/helpers/snakify.rs | 18 ------------------ strum_macros/src/lib.rs | 4 ++-- strum_macros/src/macros/enum_try_as.rs | 6 +++--- strum_tests/tests/enum_try_as.rs | 4 ++++ 6 files changed, 27 insertions(+), 26 deletions(-) delete mode 100644 strum_macros/src/helpers/snakify.rs diff --git a/strum_macros/src/helpers/case_style.rs b/strum_macros/src/helpers/case_style.rs index d714f863..388380be 100644 --- a/strum_macros/src/helpers/case_style.rs +++ b/strum_macros/src/helpers/case_style.rs @@ -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 = 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() +} diff --git a/strum_macros/src/helpers/mod.rs b/strum_macros/src/helpers/mod.rs index 69eeabe9..142ea0b8 100644 --- a/strum_macros/src/helpers/mod.rs +++ b/strum_macros/src/helpers/mod.rs @@ -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; diff --git a/strum_macros/src/helpers/snakify.rs b/strum_macros/src/helpers/snakify.rs deleted file mode 100644 index 96555441..00000000 --- a/strum_macros/src/helpers/snakify.rs +++ /dev/null @@ -1,18 +0,0 @@ -use heck::ToSnakeCase; - -/// 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 = 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() -} diff --git a/strum_macros/src/lib.rs b/strum_macros/src/lib.rs index f09d135e..f877dbb0 100644 --- a/strum_macros/src/lib.rs +++ b/strum_macros/src/lib.rs @@ -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; diff --git a/strum_macros/src/macros/enum_try_as.rs b/strum_macros/src/macros/enum_try_as.rs index dac35b32..088a1f10 100644 --- a/strum_macros/src/macros/enum_try_as.rs +++ b/strum_macros/src/macros/enum_try_as.rs @@ -37,7 +37,7 @@ pub fn enum_try_as_inner(ast: &DeriveInput) -> syn::Result { 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 @@ -46,7 +46,7 @@ pub fn enum_try_as_inner(ast: &DeriveInput) -> syn::Result { #[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 @@ -55,7 +55,7 @@ pub fn enum_try_as_inner(ast: &DeriveInput) -> syn::Result { #[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 diff --git a/strum_tests/tests/enum_try_as.rs b/strum_tests/tests/enum_try_as.rs index d9a8bb9d..aa54205e 100644 --- a/strum_tests/tests/enum_try_as.rs +++ b/strum_tests/tests/enum_try_as.rs @@ -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]