Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
08820048 committed May 9, 2024
0 parents commit 3004737
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "untools"
version = "1.0.0"
edition = "2021"
authors = ["Code0408"]
description = "A simple and user-friendly underscore variable naming tool."
license = "MIT"
keywords = ["rust", "camelcase", "snakecase"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"

replace-with = 'crates-io'

39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# uutool

A Rust utility crate for converting variable names from camelCase to snake_case.

## Features

- Convert variable names from camelCase to snake_case format.
- Option to convert the result to uppercase.
- Simple and easy-to-use tool for maintaining consistent variable naming conventions.

## Installation

Add the following dependency to your `Cargo.toml` file:

```toml
[dependencies]
ctsc_utils = "0.1.0"
```



## Usage

```rust
use ctsc_utils::ctsc;

fn main() {
let camel_case_name = "myVariableName";
let snake_case_name = ctsc(camel_case_name, true);
println!("Converted name: {}", snake_case_name);
}
```

## License

This project is licensed under the MIT License - see the [LICENSE](https://chat.ilikexff.cn/LICENSE) file for details.

------------------

109 changes: 109 additions & 0 deletions src/ctsc_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@


/// Converts a camelCase or PascalCase string to snake_case.
///
/// If the input string is empty, returns an error message.
/// If the input string is a digit, returns an error message.
/// If the input string already contains underscores, returns the input string as-is.
/// If the input string is not in camelCase format, returns an error message.
///
/// # Arguments
///
/// * `name` - A string slice that holds the variable name to be converted.
/// * `is_constant` - A boolean flag indicating if the output should be in uppercase.
///
/// # Returns
///
/// A String containing the snake_case version of the input variable name.
///
/// # Panics
/// When a panic occurs, it may be due to **your variable** not conforming to the rules of **camelCase or PascalCase**, or being an **empty string**.
pub fn ctsc(name: &str, is_constant: bool) -> String {
if name.is_empty() {
return String::from("Input string is empty. Please provide a valid variable name.");
}

if !starts_with_digit(name) {
return String::from("Input string is a digit.");
}

if name.contains('_') {
return name.to_string();
}

if !is_camel_or_pascal_case(name) {
return format!("Input '{}' is not in camelCase format. Please provide a valid camelCase variable name.", name);
}

let mut result = String::new();

for (i, c) in name.chars().enumerate() {
if i > 0 && c.is_uppercase() {
result.push('_');
}
result.push(c.to_lowercase().next().unwrap());
}

if is_constant {
return result.to_uppercase();
}

result
}

/// Checks if a string is in camelCase or PascalCase format.
///
/// # Arguments
///
/// * `name` - A string slice that holds the variable name to be checked.
///
/// # Returns
///
/// A boolean indicating whether the input string is in camelCase or PascalCase format.
///
fn is_camel_or_pascal_case(name: &str) -> bool {
let mut has_uppercase = false;
let mut has_lowercase = false;

let mut chars = name.chars().peekable();

if let Some(first_char) = chars.peek() {
if first_char.is_lowercase() {
has_lowercase = true;
} else if first_char.is_uppercase() {
has_uppercase = true;
}
}

while let Some(c) = chars.next() {
if c.is_uppercase() {
has_uppercase = true;
} else if c.is_lowercase() {
has_lowercase = true;
} else if c.is_whitespace() || c == '_' {
// Ignore whitespaces and underscores
continue;
}
}

has_uppercase && has_lowercase
}

/// Checks if a string starts with a digit.
///
/// # Arguments
///
/// * `name` - A string slice that holds the variable name to be checked.
///
/// # Returns
///
/// A boolean indicating whether the input string starts with a digit.
///
fn starts_with_digit(name: &str) -> bool {
if let Some(first_char) = name.chars().next() {
if first_char.is_ascii_digit() {
return false;
}
}
true
}
Binary file added src/main.exe
Binary file not shown.
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


//! # uutols crate
//! A simple and user-friendly underscore variable naming tool
/// Main function to demonstrate the usage of the ctsc_utils crate.
///
/// This function calls the `camel_to_snake_case` function from the `ctsc_utils` crate
/// to convert the variable name "myVariableName" from camelCase to snake_case format,
/// and then prints the result to the console.
mod ctsc_utils;
fn main() {
println!("out: {}", ctsc_utils::ctsc("myVariableName", true));
}

0 comments on commit 3004737

Please sign in to comment.