Function camel_to_snake

Source
pub fn camel_to_snake(name: &str, is_constant: bool) -> String
Expand description

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 all caps.

§Returns

A String containing the snake_case version of the input variable name.

§Usage

This converts the the variable “testVariable” from camelcase to snakecase in all caps.

use untools::camel_to_snake;
assert_eq!(camel_to_snake("testVariable", true), "TEST_VARIABLE")