From 81c9f47f5d9517ab273a34aeea4b6e40f45aac36 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 26 Sep 2025 14:18:53 +0800 Subject: refactor: Update sheet input handling and fix tests - Modify Sheet::add_input to accept InputPackage instead of separate parameters - Use output_mappings method to generate InputPackage in tests - Update test assertions to match new path transformation logic - Fix mapping count assertions after adding multiple mappings - Clean up string_proc module structure --- crates/utils/string_proc/src/format_processer.rs | 132 +++++++++++++++++++++++ crates/utils/string_proc/src/lib.rs | 9 +- crates/utils/string_proc/src/macros.rs | 32 +++--- crates/utils/string_proc/src/simple_processer.rs | 15 +++ crates/utils/string_proc/src/string_processer.rs | 130 ---------------------- 5 files changed, 168 insertions(+), 150 deletions(-) create mode 100644 crates/utils/string_proc/src/format_processer.rs create mode 100644 crates/utils/string_proc/src/simple_processer.rs delete mode 100644 crates/utils/string_proc/src/string_processer.rs (limited to 'crates/utils') diff --git a/crates/utils/string_proc/src/format_processer.rs b/crates/utils/string_proc/src/format_processer.rs new file mode 100644 index 0000000..8d0a770 --- /dev/null +++ b/crates/utils/string_proc/src/format_processer.rs @@ -0,0 +1,132 @@ +pub struct FormatProcesser { + content: Vec, +} + +impl From for FormatProcesser { + fn from(value: String) -> Self { + Self { + content: Self::process_string(value), + } + } +} + +impl From<&str> for FormatProcesser { + fn from(value: &str) -> Self { + Self { + content: Self::process_string(value.to_string()), + } + } +} + +impl FormatProcesser { + /// Process the string into an intermediate format + fn process_string(input: String) -> Vec { + let mut result = String::new(); + let mut prev_space = false; + + for c in input.chars() { + match c { + 'a'..='z' | 'A'..='Z' | '0'..='9' => { + result.push(c); + prev_space = false; + } + '_' | ',' | '.' | '-' | ' ' => { + if !prev_space { + result.push(' '); + prev_space = true; + } + } + _ => {} + } + } + + let mut processed = String::new(); + let mut chars = result.chars().peekable(); + + while let Some(c) = chars.next() { + processed.push(c); + if let Some(&next) = chars.peek() + && c.is_lowercase() + && next.is_uppercase() + { + processed.push(' '); + } + } + + processed + .to_lowercase() + .split_whitespace() + .map(|s| s.to_string()) + .collect() + } + + /// Convert to camelCase format (brewCoffee) + pub fn to_camel_case(&self) -> String { + let mut result = String::new(); + for (i, word) in self.content.iter().enumerate() { + if i == 0 { + result.push_str(&word.to_lowercase()); + } else { + let mut chars = word.chars(); + if let Some(first) = chars.next() { + result.push_str(&first.to_uppercase().collect::()); + result.push_str(&chars.collect::().to_lowercase()); + } + } + } + result + } + + /// Convert to PascalCase format (BrewCoffee) + pub fn to_pascal_case(&self) -> String { + let mut result = String::new(); + for word in &self.content { + let mut chars = word.chars(); + if let Some(first) = chars.next() { + result.push_str(&first.to_uppercase().collect::()); + result.push_str(&chars.collect::().to_lowercase()); + } + } + result + } + + /// Convert to kebab-case format (brew-coffee) + pub fn to_kebab_case(&self) -> String { + self.content.join("-").to_lowercase() + } + + /// Convert to snake_case format (brew_coffee) + pub fn to_snake_case(&self) -> String { + self.content.join("_").to_lowercase() + } + + /// Convert to dot.case format (brew.coffee) + pub fn to_dot_case(&self) -> String { + self.content.join(".").to_lowercase() + } + + /// Convert to Title Case format (Brew Coffee) + pub fn to_title_case(&self) -> String { + let mut result = String::new(); + for word in &self.content { + let mut chars = word.chars(); + if let Some(first) = chars.next() { + result.push_str(&first.to_uppercase().collect::()); + result.push_str(&chars.collect::().to_lowercase()); + } + result.push(' '); + } + result.pop(); + result + } + + /// Convert to lower case format (brew coffee) + pub fn to_lower_case(&self) -> String { + self.content.join(" ").to_lowercase() + } + + /// Convert to UPPER CASE format (BREW COFFEE) + pub fn to_upper_case(&self) -> String { + self.content.join(" ").to_uppercase() + } +} diff --git a/crates/utils/string_proc/src/lib.rs b/crates/utils/string_proc/src/lib.rs index 1f24028..e5559b9 100644 --- a/crates/utils/string_proc/src/lib.rs +++ b/crates/utils/string_proc/src/lib.rs @@ -1,9 +1,10 @@ +pub mod format_processer; pub mod macros; -pub mod string_processer; +pub mod simple_processer; #[cfg(test)] mod tests { - use crate::string_processer::StringProcesser; + use crate::format_processer::FormatProcesser; #[test] fn test_processer() { @@ -22,7 +23,7 @@ mod tests { ]; for (input, expected) in test_cases { - let processor = StringProcesser::from(input); + let processor = FormatProcesser::from(input); assert_eq!( processor.to_camel_case(), expected, @@ -34,7 +35,7 @@ mod tests { #[test] fn test_conversions() { - let processor = StringProcesser::from("brewCoffee"); + let processor = FormatProcesser::from("brewCoffee"); assert_eq!(processor.to_upper_case(), "BREW COFFEE"); assert_eq!(processor.to_lower_case(), "brew coffee"); diff --git a/crates/utils/string_proc/src/macros.rs b/crates/utils/string_proc/src/macros.rs index 9d85338..135268e 100644 --- a/crates/utils/string_proc/src/macros.rs +++ b/crates/utils/string_proc/src/macros.rs @@ -1,63 +1,63 @@ #[macro_export] macro_rules! camel_case { ($input:expr) => {{ - use string_proc::string_processer::StringProcesser; - StringProcesser::from($input).to_camel_case() + use string_proc::format_processer::FormatProcesser; + FormatProcesser::from($input).to_camel_case() }}; } #[macro_export] macro_rules! upper_case { ($input:expr) => {{ - use string_proc::string_processer::StringProcesser; - StringProcesser::from($input).to_upper_case() + use string_proc::format_processer::FormatProcesser; + FormatProcesser::from($input).to_upper_case() }}; } #[macro_export] macro_rules! lower_case { ($input:expr) => {{ - use string_proc::string_processer::StringProcesser; - StringProcesser::from($input).to_lower_case() + use string_proc::format_processer::FormatProcesser; + FormatProcesser::from($input).to_lower_case() }}; } #[macro_export] macro_rules! title_case { ($input:expr) => {{ - use string_proc::string_processer::StringProcesser; - StringProcesser::from($input).to_title_case() + use string_proc::format_processer::FormatProcesser; + FormatProcesser::from($input).to_title_case() }}; } #[macro_export] macro_rules! dot_case { ($input:expr) => {{ - use string_proc::string_processer::StringProcesser; - StringProcesser::from($input).to_dot_case() + use string_proc::format_processer::FormatProcesser; + FormatProcesser::from($input).to_dot_case() }}; } #[macro_export] macro_rules! snake_case { ($input:expr) => {{ - use string_proc::string_processer::StringProcesser; - StringProcesser::from($input).to_snake_case() + use string_proc::format_processer::FormatProcesser; + FormatProcesser::from($input).to_snake_case() }}; } #[macro_export] macro_rules! kebab_case { ($input:expr) => {{ - use string_proc::string_processer::StringProcesser; - StringProcesser::from($input).to_kebab_case() + use string_proc::format_processer::FormatProcesser; + FormatProcesser::from($input).to_kebab_case() }}; } #[macro_export] macro_rules! pascal_case { ($input:expr) => {{ - use string_proc::string_processer::StringProcesser; - StringProcesser::from($input).to_pascal_case() + use string_proc::format_processer::FormatProcesser; + FormatProcesser::from($input).to_pascal_case() }}; } diff --git a/crates/utils/string_proc/src/simple_processer.rs b/crates/utils/string_proc/src/simple_processer.rs new file mode 100644 index 0000000..2de5dfc --- /dev/null +++ b/crates/utils/string_proc/src/simple_processer.rs @@ -0,0 +1,15 @@ +/// Sanitizes a file path by replacing special characters with underscores. +/// +/// This function takes a file path as input and returns a sanitized version +/// where characters that are not allowed in file paths (such as path separators +/// and other reserved characters) are replaced with underscores. +pub fn sanitize_file_path>(path: P) -> String { + let path_str = path.as_ref(); + path_str + .chars() + .map(|c| match c { + '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_', + _ => c, + }) + .collect() +} diff --git a/crates/utils/string_proc/src/string_processer.rs b/crates/utils/string_proc/src/string_processer.rs deleted file mode 100644 index 8b51c12..0000000 --- a/crates/utils/string_proc/src/string_processer.rs +++ /dev/null @@ -1,130 +0,0 @@ -pub struct StringProcesser { - content: Vec, -} - -impl From for StringProcesser { - fn from(value: String) -> Self { - Self { - content: Self::process_string(value), - } - } -} - -impl From<&str> for StringProcesser { - fn from(value: &str) -> Self { - Self { - content: Self::process_string(value.to_string()), - } - } -} - -impl StringProcesser { - /// Process the string into an intermediate format - fn process_string(input: String) -> Vec { - let mut result = String::new(); - let mut prev_space = false; - - for c in input.chars() { - match c { - 'a'..='z' | 'A'..='Z' | '0'..='9' => { - result.push(c); - prev_space = false; - } - '_' | ',' | '.' | '-' | ' ' => { - if !prev_space { - result.push(' '); - prev_space = true; - } - } - _ => {} - } - } - - let mut processed = String::new(); - let mut chars = result.chars().peekable(); - - while let Some(c) = chars.next() { - processed.push(c); - if let Some(&next) = chars.peek() - && c.is_lowercase() && next.is_uppercase() { - processed.push(' '); - } - } - - processed - .to_lowercase() - .split_whitespace() - .map(|s| s.to_string()) - .collect() - } - - /// Convert to camelCase format (brewCoffee) - pub fn to_camel_case(&self) -> String { - let mut result = String::new(); - for (i, word) in self.content.iter().enumerate() { - if i == 0 { - result.push_str(&word.to_lowercase()); - } else { - let mut chars = word.chars(); - if let Some(first) = chars.next() { - result.push_str(&first.to_uppercase().collect::()); - result.push_str(&chars.collect::().to_lowercase()); - } - } - } - result - } - - /// Convert to PascalCase format (BrewCoffee) - pub fn to_pascal_case(&self) -> String { - let mut result = String::new(); - for word in &self.content { - let mut chars = word.chars(); - if let Some(first) = chars.next() { - result.push_str(&first.to_uppercase().collect::()); - result.push_str(&chars.collect::().to_lowercase()); - } - } - result - } - - /// Convert to kebab-case format (brew-coffee) - pub fn to_kebab_case(&self) -> String { - self.content.join("-").to_lowercase() - } - - /// Convert to snake_case format (brew_coffee) - pub fn to_snake_case(&self) -> String { - self.content.join("_").to_lowercase() - } - - /// Convert to dot.case format (brew.coffee) - pub fn to_dot_case(&self) -> String { - self.content.join(".").to_lowercase() - } - - /// Convert to Title Case format (Brew Coffee) - pub fn to_title_case(&self) -> String { - let mut result = String::new(); - for word in &self.content { - let mut chars = word.chars(); - if let Some(first) = chars.next() { - result.push_str(&first.to_uppercase().collect::()); - result.push_str(&chars.collect::().to_lowercase()); - } - result.push(' '); - } - result.pop(); - result - } - - /// Convert to lower case format (brew coffee) - pub fn to_lower_case(&self) -> String { - self.content.join(" ").to_lowercase() - } - - /// Convert to UPPER CASE format (BREW COFFEE) - pub fn to_upper_case(&self) -> String { - self.content.join(" ").to_uppercase() - } -} -- cgit