summaryrefslogtreecommitdiff
path: root/utils/string_proc/src
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-02-25 15:24:18 +0800
committer魏曹先生 <1992414357@qq.com>2026-02-25 15:24:18 +0800
commitfcbd18c4b7d90388a9a2b9e28555d2526727958c (patch)
tree0de680895d6a162a99356d6e4d733942a339275e /utils/string_proc/src
parent8b96e8171c4e9d6516fd63d37cbe613bd5927a04 (diff)
Replace string_proc with just_fmt as external dependency
Diffstat (limited to 'utils/string_proc/src')
-rw-r--r--utils/string_proc/src/format_path.rs187
-rw-r--r--utils/string_proc/src/format_processer.rs242
-rw-r--r--utils/string_proc/src/lib.rs15
-rw-r--r--utils/string_proc/src/macros.rs127
-rw-r--r--utils/string_proc/src/simple_processer.rs15
5 files changed, 0 insertions, 586 deletions
diff --git a/utils/string_proc/src/format_path.rs b/utils/string_proc/src/format_path.rs
deleted file mode 100644
index d1fe849..0000000
--- a/utils/string_proc/src/format_path.rs
+++ /dev/null
@@ -1,187 +0,0 @@
-use std::path::{Path, PathBuf};
-
-#[derive(PartialEq, Eq, Clone, Copy, Debug)]
-pub struct PathFormatConfig {
- pub strip_ansi: bool,
- pub strip_unfriendly_chars: bool,
- pub resolve_parent_dirs: bool,
- pub collapse_consecutive_slashes: bool,
- pub escape_backslashes: bool,
-}
-
-impl Default for PathFormatConfig {
- fn default() -> Self {
- Self {
- strip_ansi: true,
- strip_unfriendly_chars: true,
- resolve_parent_dirs: true,
- collapse_consecutive_slashes: true,
- escape_backslashes: true,
- }
- }
-}
-
-/// Normalize an input path string into a canonical, platform‑agnostic form.
-///
-/// This function removes ANSI escape sequences, unifies separators to `/`,
-/// collapses duplicate slashes, strips unfriendly characters (`*`, `?`, `"`, `<`, `>`, `|`),
-/// resolves simple `..` components, and preserves a trailing slash when present.
-///
-/// See examples below for the exact normalization behavior.
-///
-/// # Examples
-///
-/// ```
-/// # use string_proc::format_path::format_path_str;
-/// use std::io::Error;
-///
-/// # fn main() -> Result<(), Error> {
-/// assert_eq!(format_path_str("C:\\Users\\\\test")?, "C:/Users/test");
-/// assert_eq!(
-/// format_path_str("/path/with/*unfriendly?chars")?,
-/// "/path/with/unfriendlychars"
-/// );
-/// assert_eq!(format_path_str("\x1b[31m/path\x1b[0m")?, "/path");
-/// assert_eq!(format_path_str("/home/user/dir/")?, "/home/user/dir/");
-/// assert_eq!(
-/// format_path_str("/home/user/file.txt")?,
-/// "/home/user/file.txt"
-/// );
-/// assert_eq!(
-/// format_path_str("/home/my_user/DOCS/JVCS_TEST/Workspace/../Vault/")?,
-/// "/home/my_user/DOCS/JVCS_TEST/Vault/"
-/// );
-/// assert_eq!(format_path_str("./home/file.txt")?, "home/file.txt");
-/// assert_eq!(format_path_str("./home/path/")?, "home/path/");
-/// assert_eq!(format_path_str("./")?, "");
-/// # Ok(())
-/// # }
-/// ```
-pub fn format_path_str(path: impl Into<String>) -> Result<String, std::io::Error> {
- format_path_str_with_config(path, &PathFormatConfig::default())
-}
-
-/// Normalize an input path string into a canonical, platform‑agnostic form.
-///
-/// This function removes ANSI escape sequences, unifies separators to `/`,
-/// collapses duplicate slashes, strips unfriendly characters (`*`, `?`, `"`, `<`, `>`, `|`),
-/// resolves simple `..` components, and preserves a trailing slash when present.
-///
-/// Unlike `format_path_str`,
-/// this method uses `PathFormatConfig` to precisely control
-/// what should be processed
-pub fn format_path_str_with_config(
- path: impl Into<String>,
- config: &PathFormatConfig,
-) -> Result<String, std::io::Error> {
- let path_str = path.into();
- let ends_with_slash = path_str.ends_with('/');
-
- // ANSI Strip
- let cleaned = if config.strip_ansi {
- strip_ansi_escapes::strip(&path_str)
- } else {
- path_str.as_bytes().to_vec()
- };
- let path_without_ansi = String::from_utf8(cleaned)
- .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
-
- let path_with_forward_slash = if config.escape_backslashes {
- path_without_ansi.replace('\\', "/")
- } else {
- path_without_ansi
- };
- let mut result = String::new();
- let mut prev_char = '\0';
-
- for c in path_with_forward_slash.chars() {
- if config.collapse_consecutive_slashes && c == '/' && prev_char == '/' {
- continue;
- }
- result.push(c);
- prev_char = c;
- }
-
- if config.strip_unfriendly_chars {
- let unfriendly_chars = ['*', '?', '"', '<', '>', '|'];
- result = result
- .chars()
- .filter(|c| !unfriendly_chars.contains(c))
- .collect();
- }
-
- // Handle ".." path components
- let path_buf = PathBuf::from(&result);
- let normalized_path = if config.resolve_parent_dirs {
- normalize_path(&path_buf)
- } else {
- path_buf
- };
- result = normalized_path.to_string_lossy().replace('\\', "/");
-
- // Restore trailing slash if original path had one
- if ends_with_slash && !result.ends_with('/') {
- result.push('/');
- }
-
- // Special case: when result is only "./", return ""
- if result == "./" {
- return Ok(String::new());
- }
-
- Ok(result)
-}
-
-/// Normalize path by resolving ".." components without requiring file system access
-fn normalize_path(path: &Path) -> PathBuf {
- let mut components = Vec::new();
-
- for component in path.components() {
- match component {
- std::path::Component::ParentDir => {
- if !components.is_empty() {
- components.pop();
- }
- }
- std::path::Component::CurDir => {
- // Skip current directory components
- }
- _ => {
- components.push(component);
- }
- }
- }
-
- if components.is_empty() {
- PathBuf::from(".")
- } else {
- components.iter().collect()
- }
-}
-
-/// Format a [`PathBuf`] into its canonical string form and convert it back.
-///
-/// This is a convenience wrapper around [`format_path_str`], preserving
-/// the semantics of [`PathBuf`] while applying the same normalization rules:
-/// - normalize separators to `/`
-/// - remove duplicated separators
-/// - strip ANSI escape sequences
-/// - remove unfriendly characters (`*`, `?`, etc.)
-/// - resolve simple `..` segments
-pub fn format_path(path: impl Into<PathBuf>) -> Result<PathBuf, std::io::Error> {
- let path_str = format_path_str(path.into().display().to_string())?;
- Ok(PathBuf::from(path_str))
-}
-
-/// Format a [`PathBuf`] into its canonical string form and convert it back.
-///
-/// Unlike `format_path`,
-/// this method uses `PathFormatConfig` to precisely control
-/// what should be processed
-pub fn format_path_with_config(
- path: impl Into<PathBuf>,
- config: &PathFormatConfig,
-) -> Result<PathBuf, std::io::Error> {
- let path_str = format_path_str_with_config(path.into().display().to_string(), config)?;
- Ok(PathBuf::from(path_str))
-}
diff --git a/utils/string_proc/src/format_processer.rs b/utils/string_proc/src/format_processer.rs
deleted file mode 100644
index bac84c0..0000000
--- a/utils/string_proc/src/format_processer.rs
+++ /dev/null
@@ -1,242 +0,0 @@
-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)
- ///
- /// # Examples
- ///
- /// ```
- /// # use string_proc::format_processer::FormatProcesser;
- /// let processor = FormatProcesser::from("brew_coffee");
- /// assert_eq!(processor.to_camel_case(), "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)
- ///
- /// # Examples
- ///
- /// ```
- /// # use string_proc::format_processer::FormatProcesser;
- /// let processor = FormatProcesser::from("brew_coffee");
- /// assert_eq!(processor.to_pascal_case(), "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)
- ///
- /// # Examples
- ///
- /// ```
- /// # use string_proc::format_processer::FormatProcesser;
- /// let processor = FormatProcesser::from("brew_coffee");
- /// assert_eq!(processor.to_kebab_case(), "brew-coffee");
- /// ```
- pub fn to_kebab_case(&self) -> String {
- self.content.join("-").to_lowercase()
- }
-
- /// Convert to snake_case format (brew_coffee)
- ///
- /// # Examples
- ///
- /// ```
- /// # use string_proc::format_processer::FormatProcesser;
- /// let processor = FormatProcesser::from("brewCoffee");
- /// assert_eq!(processor.to_snake_case(), "brew_coffee");
- /// ```
- pub fn to_snake_case(&self) -> String {
- self.content.join("_").to_lowercase()
- }
-
- /// Convert to dot.case format (brew.coffee)
- ///
- /// # Examples
- ///
- /// ```
- /// # use string_proc::format_processer::FormatProcesser;
- /// let processor = FormatProcesser::from("brew_coffee");
- /// assert_eq!(processor.to_dot_case(), "brew.coffee");
- /// ```
- pub fn to_dot_case(&self) -> String {
- self.content.join(".").to_lowercase()
- }
-
- /// Convert to Title Case format (Brew Coffee)
- ///
- /// # Examples
- ///
- /// ```
- /// # use string_proc::format_processer::FormatProcesser;
- /// let processor = FormatProcesser::from("brew_coffee");
- /// assert_eq!(processor.to_title_case(), "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)
- ///
- /// # Examples
- ///
- /// ```
- /// # use string_proc::format_processer::FormatProcesser;
- /// let processor = FormatProcesser::from("BREW COFFEE");
- /// assert_eq!(processor.to_lower_case(), "brew coffee");
- /// ```
- pub fn to_lower_case(&self) -> String {
- self.content.join(" ").to_lowercase()
- }
-
- /// Convert to UPPER CASE format (BREW COFFEE)
- ///
- /// # Examples
- ///
- /// ```
- /// # use string_proc::format_processer::FormatProcesser;
- /// let processor = FormatProcesser::from("brew coffee");
- /// assert_eq!(processor.to_upper_case(), "BREW COFFEE");
- /// ```
- pub fn to_upper_case(&self) -> String {
- self.content.join(" ").to_uppercase()
- }
-}
-
-#[cfg(test)]
-mod tests {
- use crate::format_processer::FormatProcesser;
-
- #[test]
- fn test_processer() {
- let test_cases = vec![
- ("brew_coffee", "brewCoffee"),
- ("brew, coffee", "brewCoffee"),
- ("brew-coffee", "brewCoffee"),
- ("Brew.Coffee", "brewCoffee"),
- ("bRewCofFee", "bRewCofFee"),
- ("brewCoffee", "brewCoffee"),
- ("b&rewCoffee", "brewCoffee"),
- ("BrewCoffee", "brewCoffee"),
- ("brew.coffee", "brewCoffee"),
- ("Brew_Coffee", "brewCoffee"),
- ("BREW COFFEE", "brewCoffee"),
- ];
-
- for (input, expected) in test_cases {
- let processor = FormatProcesser::from(input);
- assert_eq!(
- processor.to_camel_case(),
- expected,
- "Failed for input: '{}'",
- input
- );
- }
- }
-
- #[test]
- fn test_conversions() {
- let processor = FormatProcesser::from("brewCoffee");
-
- assert_eq!(processor.to_upper_case(), "BREW COFFEE");
- assert_eq!(processor.to_lower_case(), "brew coffee");
- assert_eq!(processor.to_title_case(), "Brew Coffee");
- assert_eq!(processor.to_dot_case(), "brew.coffee");
- assert_eq!(processor.to_snake_case(), "brew_coffee");
- assert_eq!(processor.to_kebab_case(), "brew-coffee");
- assert_eq!(processor.to_pascal_case(), "BrewCoffee");
- assert_eq!(processor.to_camel_case(), "brewCoffee");
- }
-}
diff --git a/utils/string_proc/src/lib.rs b/utils/string_proc/src/lib.rs
deleted file mode 100644
index d019003..0000000
--- a/utils/string_proc/src/lib.rs
+++ /dev/null
@@ -1,15 +0,0 @@
-#[deprecated = "The module has been migrated to crates.io, please migrate to `just_fmt = \"0.1\"`"]
-#[allow(deprecated)]
-pub mod format_path;
-
-#[deprecated = "The module has been migrated to crates.io, please migrate to `just_fmt = \"0.1\"`"]
-#[allow(deprecated)]
-pub mod format_processer;
-
-#[deprecated = "The module has been migrated to crates.io, please migrate to `just_fmt = \"0.1\"`"]
-#[allow(deprecated)]
-pub mod macros;
-
-#[deprecated = "The module has been migrated to crates.io, please migrate to `just_fmt = \"0.1\"`"]
-#[allow(deprecated)]
-pub mod simple_processer;
diff --git a/utils/string_proc/src/macros.rs b/utils/string_proc/src/macros.rs
deleted file mode 100644
index 414baf0..0000000
--- a/utils/string_proc/src/macros.rs
+++ /dev/null
@@ -1,127 +0,0 @@
-/// Convert to camelCase format (brewCoffee)
-///
-/// # Examples
-///
-/// ```
-/// # use string_proc::camel_case;
-/// assert_eq!(camel_case!("brew_coffee"), "brewCoffee");
-/// ```
-#[macro_export]
-macro_rules! camel_case {
- ($input:expr) => {{
- use string_proc::format_processer::FormatProcesser;
- FormatProcesser::from($input).to_camel_case()
- }};
-}
-
-/// Convert to UPPER CASE format (BREW COFFEE)
-///
-/// # Examples
-///
-/// ```
-/// # use string_proc::upper_case;
-/// assert_eq!(upper_case!("brew coffee"), "BREW COFFEE");
-/// ```
-#[macro_export]
-macro_rules! upper_case {
- ($input:expr) => {{
- use string_proc::format_processer::FormatProcesser;
- FormatProcesser::from($input).to_upper_case()
- }};
-}
-
-/// Convert to lower case format (brew coffee)
-///
-/// # Examples
-///
-/// ```
-/// # use string_proc::lower_case;
-/// assert_eq!(lower_case!("BREW COFFEE"), "brew coffee");
-/// ```
-#[macro_export]
-macro_rules! lower_case {
- ($input:expr) => {{
- use string_proc::format_processer::FormatProcesser;
- FormatProcesser::from($input).to_lower_case()
- }};
-}
-
-/// Convert to Title Case format (Brew Coffee)
-///
-/// # Examples
-///
-/// ```
-/// # use string_proc::title_case;
-/// assert_eq!(title_case!("brew_coffee"), "Brew Coffee");
-/// ```
-#[macro_export]
-macro_rules! title_case {
- ($input:expr) => {{
- use string_proc::format_processer::FormatProcesser;
- FormatProcesser::from($input).to_title_case()
- }};
-}
-
-/// Convert to dot.case format (brew.coffee)
-///
-/// # Examples
-///
-/// ```
-/// # use string_proc::dot_case;
-/// assert_eq!(dot_case!("brew_coffee"), "brew.coffee");
-/// ```
-#[macro_export]
-macro_rules! dot_case {
- ($input:expr) => {{
- use string_proc::format_processer::FormatProcesser;
- FormatProcesser::from($input).to_dot_case()
- }};
-}
-
-/// Convert to snake_case format (brew_coffee)
-///
-/// # Examples
-///
-/// ```
-/// # use string_proc::snake_case;
-/// assert_eq!(snake_case!("brewCoffee"), "brew_coffee");
-/// ```
-#[macro_export]
-macro_rules! snake_case {
- ($input:expr) => {{
- use string_proc::format_processer::FormatProcesser;
- FormatProcesser::from($input).to_snake_case()
- }};
-}
-
-/// Convert to kebab-case format (brew-coffee)
-///
-/// # Examples
-///
-/// ```
-/// # use string_proc::kebab_case;
-/// assert_eq!(kebab_case!("brew_coffee"), "brew-coffee");
-/// ```
-#[macro_export]
-macro_rules! kebab_case {
- ($input:expr) => {{
- use string_proc::format_processer::FormatProcesser;
- FormatProcesser::from($input).to_kebab_case()
- }};
-}
-
-/// Convert to PascalCase format (BrewCoffee)
-///
-/// # Examples
-///
-/// ```
-/// # use string_proc::pascal_case;
-/// assert_eq!(pascal_case!("brew_coffee"), "BrewCoffee");
-/// ```
-#[macro_export]
-macro_rules! pascal_case {
- ($input:expr) => {{
- use string_proc::format_processer::FormatProcesser;
- FormatProcesser::from($input).to_pascal_case()
- }};
-}
diff --git a/utils/string_proc/src/simple_processer.rs b/utils/string_proc/src/simple_processer.rs
deleted file mode 100644
index 2de5dfc..0000000
--- a/utils/string_proc/src/simple_processer.rs
+++ /dev/null
@@ -1,15 +0,0 @@
-/// 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<P: AsRef<str>>(path: P) -> String {
- let path_str = path.as_ref();
- path_str
- .chars()
- .map(|c| match c {
- '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
- _ => c,
- })
- .collect()
-}