diff --git a/Cargo.lock b/Cargo.lock index 0649f3be..71d79adb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -717,7 +717,6 @@ dependencies = [ name = "conduit_macros" version = "0.4.5" dependencies = [ - "conduit_core", "proc-macro2", "quote", "syn 1.0.109", diff --git a/src/macros/Cargo.toml b/src/macros/Cargo.toml index b9a35aab..ca98f169 100644 --- a/src/macros/Cargo.toml +++ b/src/macros/Cargo.toml @@ -18,7 +18,6 @@ proc-macro = true syn.workspace = true quote.workspace = true proc-macro2.workspace = true -conduit-core.workspace = true [lints] workspace = true diff --git a/src/macros/admin.rs b/src/macros/admin.rs index a1a5b22a..e1d294b9 100644 --- a/src/macros/admin.rs +++ b/src/macros/admin.rs @@ -1,9 +1,10 @@ -use conduit_core::utils::string::camel_to_snake_string; use proc_macro::{Span, TokenStream}; use proc_macro2::TokenStream as TokenStream2; use quote::quote; use syn::{parse_macro_input, AttributeArgs, Fields, Ident, ItemEnum, Variant}; +use crate::utils::camel_to_snake_string; + pub(super) fn command_dispatch(args: TokenStream, input_: TokenStream) -> TokenStream { let input = input_.clone(); let item = parse_macro_input!(input as ItemEnum); diff --git a/src/macros/mod.rs b/src/macros/mod.rs index 0aba7560..6f286d66 100644 --- a/src/macros/mod.rs +++ b/src/macros/mod.rs @@ -1,4 +1,5 @@ mod admin; +mod utils; use proc_macro::TokenStream; diff --git a/src/macros/utils.rs b/src/macros/utils.rs new file mode 100644 index 00000000..f512c56c --- /dev/null +++ b/src/macros/utils.rs @@ -0,0 +1,25 @@ +#[must_use] +pub(crate) fn camel_to_snake_string(s: &str) -> String { + let mut output = String::with_capacity( + s.chars() + .fold(s.len(), |a, ch| a.saturating_add(usize::from(ch.is_ascii_uppercase()))), + ); + + let mut state = false; + s.chars().for_each(|ch| { + let m = ch.is_ascii_uppercase(); + let s = exchange(&mut state, !m); + if m && s { + output.push('_'); + } + output.push(ch.to_ascii_lowercase()); + }); + + output +} + +pub(crate) fn exchange(state: &mut T, source: T) -> T { + let ret = state.clone(); + *state = source; + ret +}