summaryrefslogtreecommitdiff
path: root/crates/utils/string_proc/src/format_processer.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-09-26 14:18:53 +0800
committer魏曹先生 <1992414357@qq.com>2025-09-26 14:18:53 +0800
commit81c9f47f5d9517ab273a34aeea4b6e40f45aac36 (patch)
tree90e1d033b28fb09d2b9c76b4658b7559acf35c2d /crates/utils/string_proc/src/format_processer.rs
parentf5e2a00d6701729eb33da5962069c4432db426c8 (diff)
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
Diffstat (limited to 'crates/utils/string_proc/src/format_processer.rs')
-rw-r--r--crates/utils/string_proc/src/format_processer.rs132
1 files changed, 132 insertions, 0 deletions
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<String>,
+}
+
+impl From<String> 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<String> {
+ 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::<String>());
+ result.push_str(&chars.collect::<String>().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::<String>());
+ result.push_str(&chars.collect::<String>().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::<String>());
+ result.push_str(&chars.collect::<String>().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()
+ }
+}