1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::path::{Path, PathBuf};

pub use self::config::Config;
pub use self::errors::{CliError, CliResult, SubstrateResult};

pub mod command_prelude;
pub mod config;
pub mod errors;
pub mod restricted_names;

pub fn normalize_paths(root_path: &Path, path: &Path) -> color_eyre::eyre::Result<PathBuf> {
    let canonical_parent = if let Some(parent) = path.parent() {
        cargo_util::paths::normalize_path(&root_path.join(parent))
    } else {
        PathBuf::from("")
    };

    Ok(canonical_parent.join(path.file_name().unwrap()))
}

pub fn indented_lines(text: &str) -> String {
    text.lines()
        .map(|line| {
            if line.is_empty() {
                String::from("\n")
            } else {
                format!("  {}\n", line)
            }
        })
        .collect()
}

/// Transform a string to PascalCase string
pub fn to_pascal_case(string: &str) -> String {
    let mut chars: Vec<char> = vec![];
    let mut uppercase_next = false;
    for (i, ch) in string.chars().enumerate() {
        if i == 0 {
            chars.extend(ch.to_uppercase())
        } else if ch == '_' || ch == '-' {
            uppercase_next = true;
        } else if uppercase_next {
            chars.extend(ch.to_uppercase());
            uppercase_next = false;
        } else {
            chars.push(ch);
        }
    }

    String::from_iter(chars)
}

/// Transform a string to snake_case string
pub fn to_snake_case(string: &str) -> String {
    let mut buffer = String::with_capacity(string.len() + string.len() / 2);
    let mut prev_was_delimiter = true; // Start with true to handle cases where the input starts with a non-alphanumeric character

    for c in string.chars() {
        if c.is_ascii_alphanumeric() {
            if c.is_uppercase() {
                if !prev_was_delimiter && !buffer.is_empty() {
                    buffer.push('_');
                }
                buffer.push(c.to_ascii_lowercase());
            } else {
                buffer.push(c);
            }
            prev_was_delimiter = false;
        } else if c == ' ' || c == '-' {
            if !prev_was_delimiter && !buffer.is_empty() {
                buffer.push('_');
            }
            prev_was_delimiter = true;
        }
    }

    buffer
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_to_pascal_case() {
        assert_eq!(to_pascal_case("HelloWorld"), "HelloWorld");
        assert_eq!(to_pascal_case("Hello_World"), "HelloWorld");
        assert_eq!(to_pascal_case("Hello-World"), "HelloWorld");
        assert_eq!(to_pascal_case("helloWorld"), "HelloWorld");
    }

    #[test]
    fn test_to_snake_case() {
        assert_eq!(to_snake_case("HelloWorld"), "hello_world");
        assert_eq!(to_snake_case("Hello_World"), "hello_world");
        assert_eq!(to_snake_case("Hello-World"), "hello_world");
        assert_eq!(to_snake_case("hello-world"), "hello_world");
        assert_eq!(to_snake_case("helloWorld"), "hello_world");
        assert_eq!(to_snake_case("ABc   wOW"), "a_bc_w_o_w");
    }
}