From 667a0aa4a1292746c4258577f9b874a9041c598f Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Sat, 22 Feb 2025 13:43:00 +0100 Subject: [PATCH] feat: basic parsing and read structure implemented --- .gitignore | 1 + Cargo.lock | 454 ++++++++++++++++++++++++++++++ Cargo.toml | 8 + src/files/mod.rs | 66 +++++ src/main.rs | 38 +++ src/parsers/expressions.rs | 23 ++ src/parsers/mod.rs | 85 ++++++ src/parsers/test/parsers_tests.rs | 26 ++ 8 files changed, 701 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/files/mod.rs create mode 100644 src/main.rs create mode 100644 src/parsers/expressions.rs create mode 100644 src/parsers/mod.rs create mode 100644 src/parsers/test/parsers_tests.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..35e05fa --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,454 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "code-templates" +version = "0.1.0" +dependencies = [ + "inquire", + "regex", +] + +[[package]] +name = "crossterm" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67" +dependencies = [ + "bitflags 1.3.2", + "crossterm_winapi", + "libc", + "mio", + "parking_lot", + "signal-hook", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +dependencies = [ + "winapi", +] + +[[package]] +name = "dyn-clone" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "feeef44e73baff3a26d371801df019877a9866a8c493d315ab00177843314f35" + +[[package]] +name = "fuzzy-matcher" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54614a3312934d066701a80f20f15fa3b56d67ac7722b39eea5b4c9dd1d66c94" +dependencies = [ + "thread_local", +] + +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" +dependencies = [ + "byteorder", +] + +[[package]] +name = "inquire" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fddf93031af70e75410a2511ec04d49e758ed2f26dad3404a934e0fb45cc12a" +dependencies = [ + "bitflags 2.8.0", + "crossterm", + "dyn-clone", + "fuzzy-matcher", + "fxhash", + "newline-converter", + "once_cell", + "unicode-segmentation", + "unicode-width", +] + +[[package]] +name = "libc" +version = "0.2.169" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "mio" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys", +] + +[[package]] +name = "newline-converter" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b6b097ecb1cbfed438542d16e84fd7ad9b0c76c8a65b7f9039212a3d14dc7f" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "once_cell" +version = "1.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.52.6", +] + +[[package]] +name = "redox_syscall" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" +dependencies = [ + "bitflags 2.8.0", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "signal-hook" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + +[[package]] +name = "smallvec" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" + +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ca13805 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "code-templates" +version = "0.1.0" +edition = "2021" + +[dependencies] +inquire = "0.7.5" +regex = "1.11.1" diff --git a/src/files/mod.rs b/src/files/mod.rs new file mode 100644 index 0000000..a944494 --- /dev/null +++ b/src/files/mod.rs @@ -0,0 +1,66 @@ +use std::{env, fs, path::PathBuf}; + +const ENV_KEY: &str = "BLUEPRINTS_PATHS"; +const DEFAULT_PATH: &str = "./.blueprints"; + +// ╭────────────────────────╮ +// │- Read from filesystem -│ +// ╰────────────────────────╯ +pub fn get_template_options(base_paths: Vec) -> Vec { + let mut type_options: Vec = vec![]; + + for base_path in base_paths { + let type_dirs = get_valid_dirs_paths(&base_path); + // TODO: prettify this in one line + for type_dir in type_dirs { + type_options.push(type_dir); + } + } + + type_options +} + +pub fn get_valid_dirs_paths(dir: &str) -> Vec { + // FIXME: this panics when the directory does not exist + let paths = fs::read_dir(dir).unwrap(); + + let mut valid_dirs: Vec = vec![]; + + for path in paths { + let _path = path.unwrap().path(); + if is_template_path_valid(&_path) { + valid_dirs.push(_path.display().to_string()); + } + } + return valid_dirs; +} + +pub fn is_template_path_valid(path: &PathBuf) -> bool { + path.is_dir() +} + +pub fn get_base_template_paths() -> Vec { + let mut base_paths: Vec = vec![]; + + match env::var(ENV_KEY) { + Ok(value) => { + let env_path_list = value.split(";"); + for raw_path in env_path_list { + base_paths.push(raw_path.to_string()); + } + } + Err(_) => { + base_paths.push(DEFAULT_PATH.to_string()); + println!( + "${} is not set. Falling back to default './.blueprints' route.", + ENV_KEY + ); + } + } + + base_paths +} + +// ╭───────────────────────╮ +// │- Write to filesystem -│ +// ╰───────────────────────╯ diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..bf87768 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,38 @@ +mod files; +mod parsers; + +use files::*; +use inquire::{Select, Text}; + +#[cfg(test)] +#[path = "./parsers/test/parsers_tests.rs"] +mod test; + +fn main() { + println!( + "╭──────────────────╮ +│- Code Templates -│ +╰──────────────────╯" + ); + let base_paths = get_base_template_paths(); + let template_type_options = get_template_options(base_paths); + let template_type_result = + Select::new("Select a template variant:", template_type_options).prompt(); + + if let Ok(template_type) = template_type_result { + let template_file_options = get_template_options(vec![template_type]); + let template_file_result = + Select::new("Select a template:", template_file_options).prompt(); + + if let Ok(template_file) = template_file_result { + let target_name_result = Text::new("Insert the desired name:").prompt(); + + if let Ok(target_name) = target_name_result { + // TODO: decide if the path should be inserted automatically of with a loop of selections -> Maybe better the loop + let target_path_result = Text::new("Insert the target path:").prompt(); + + if let Ok(target_path) = target_path_result {} + } + } + } +} diff --git a/src/parsers/expressions.rs b/src/parsers/expressions.rs new file mode 100644 index 0000000..696079b --- /dev/null +++ b/src/parsers/expressions.rs @@ -0,0 +1,23 @@ +pub const FILENAME_EXPRESSIONS: [&str; 9] = [ + "__name__", + "__upperCase_name__", + "__lowerCase_name__", + "__camelCase_name__", + "__pascalCase_name__", + "__snakeCase_name__", + "__upperSnakeCase_name__", + "__kebabCase_name__", + "__lowerDotCase_name__", +]; + +pub const TEMPLATE_EXPRESSIONS: [&str; 9] = [ + "{{name}}", + "{{upperCase name}}", + "{{lowerCase name}}", + "{{camelCase name}}", + "{{pascalCase name}}", + "{{snakeCase name}}", + "{{upperSnakeCase name}}", + "{{kebabCase name}}", + "{{lowerDotCase name}}", +]; diff --git a/src/parsers/mod.rs b/src/parsers/mod.rs new file mode 100644 index 0000000..318ef80 --- /dev/null +++ b/src/parsers/mod.rs @@ -0,0 +1,85 @@ +use regex::Regex; + +pub mod expressions; + +pub fn apply_filename_template(template: &str, filename: &str) -> String { + match template { + "__name__" => filename.to_string(), + "__upperCase_name__" => filename.to_uppercase().to_string(), + "__lowerCase_name__" => filename.to_lowercase().to_string(), + "__camelCase_name__" => parse_camel_case(filename), + "__pascalCase_name__" => parse_pascal_case(filename), + "__snakeCase_name__" => parse_snake_case(filename), + "__upperSnakeCase_name__" => parse_snake_case(filename).to_uppercase(), + "__kebabCase_name__" => parse_snake_case(filename).replace("_", "-"), + "__lowerDotCase_name__" => parse_snake_case(filename).replace("_", "."), + _ => filename.to_string(), + } +} + +fn parse_camel_case(filename: &str) -> String { + let first_char_regex = Regex::new(r"^[A-Z]").unwrap(); + let filename = parse_pascal_case(filename); + let filename = first_char_regex + .replace_all(&filename, |captured: ®ex::Captures| { + captured[0].to_lowercase() + }) + .into_owned(); + filename +} + +fn parse_pascal_case(filename: &str) -> String { + let char_after_space_regex = Regex::new(r" ([a-z])").unwrap(); + let first_char_regex = Regex::new(r"^[a-z]").unwrap(); + + // Change all separators by " " to facilitate regex parsing + let filename = filename.replace("-", " ").replace("_", " "); + + let filename = char_after_space_regex + .replace_all(&filename, |captured: ®ex::Captures| { + format!(" {}", captured[1].to_uppercase()) + }) + .into_owned(); + let filename = first_char_regex + .replace_all(&filename, |captured: ®ex::Captures| { + captured[0].to_uppercase() + }) + .into_owned(); + + let filename = filename.replace(" ", ""); + + filename +} + +fn parse_snake_case(filename: &str) -> String { + let highlight_regex = Regex::new(r"[\s\_\-A-Z]([A-Za-z])").unwrap(); + + let splited_filename = filename.split_at(1); + let rest_filename = splited_filename.1.to_string(); + let rest_filename = highlight_regex + .replace_all(&rest_filename, |captured: ®ex::Captures| { + let valid_char_regex = Regex::new(r"^[A-Za-z]$").unwrap(); + + let discarded = captured[0].to_string().chars().nth(0); + let mut left_side = "".to_string(); + + if let Some(discarded_first) = discarded { + if valid_char_regex.is_match(&discarded_first.to_string()) { + left_side = discarded_first.to_string(); + } + } + format!( + "_{}{}", + left_side.to_lowercase(), + captured[1].to_lowercase() + ) + }) + .into_owned(); + let filename = format!( + "{}{}", + splited_filename.0.to_lowercase(), + rest_filename.to_lowercase() + ); + + filename.to_string() +} diff --git a/src/parsers/test/parsers_tests.rs b/src/parsers/test/parsers_tests.rs new file mode 100644 index 0000000..4c0f0da --- /dev/null +++ b/src/parsers/test/parsers_tests.rs @@ -0,0 +1,26 @@ +#[cfg(test)] +pub mod tests { + use crate::parsers::{apply_filename_template, expressions::FILENAME_EXPRESSIONS}; + + #[test] + fn test_apply_filename_template() { + const FILENAME: &str = "this_is a-TeSt"; + + let expected_filename_output = [ + "this_is a-TeSt", // normal + "THIS_IS A-TEST", // upper + "this_is a-test", // lower + "thisIsATeSt", // camel + "ThisIsATeSt", // Pascal + "this_is_a_te_st", // snake + "THIS_IS_A_TE_ST", // snake upper + "this-is-a-te-st", // kebab + "this.is.a.te.st", // lower dot + ]; + + for (i, expression) in FILENAME_EXPRESSIONS.into_iter().enumerate() { + let output = apply_filename_template(expression, FILENAME); + assert_eq!(output, expected_filename_output[i]) + } + } +}