diff --git a/Cargo.lock b/Cargo.lock index acfbbdc..6c0b539 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -567,6 +567,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -965,7 +974,7 @@ version = "0.99.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" dependencies = [ - "convert_case", + "convert_case 0.4.0", "proc-macro2", "quote", "rustc_version", @@ -4662,6 +4671,7 @@ dependencies = [ "anyhow", "build-fs-tree", "clap 4.5.4", + "convert_case 0.6.0", "file_tree_utils", "handlebars", "ignore", diff --git a/crates/templates_scaffolding_utils/Cargo.toml b/crates/templates_scaffolding_utils/Cargo.toml index 2e1ebc4..6d43732 100644 --- a/crates/templates_scaffolding_utils/Cargo.toml +++ b/crates/templates_scaffolding_utils/Cargo.toml @@ -20,3 +20,4 @@ thiserror = "1.0.58" file_tree_utils = { path = "../file_tree_utils" } build-fs-tree = "0.4" handlebars ="5.1.2" +convert_case = "0.6.0" diff --git a/crates/templates_scaffolding_utils/src/lib.rs b/crates/templates_scaffolding_utils/src/lib.rs index d536cf4..173b061 100644 --- a/crates/templates_scaffolding_utils/src/lib.rs +++ b/crates/templates_scaffolding_utils/src/lib.rs @@ -1,9 +1,10 @@ use std::{collections::BTreeMap, path::PathBuf}; +use convert_case::{Case, Casing}; use file_tree_utils::{ file_content, find_files, flatten_file_tree, unflatten_file_tree, FileTree, FileTreeError, }; -use handlebars::Handlebars; +use handlebars::{handlebars_helper, Handlebars}; use regex::Regex; use serde::Serialize; use thiserror::Error; @@ -217,3 +218,25 @@ pub fn render_template_file_tree_and_merge_with_existing<'a, T: Serialize>( Ok(file_tree) } + +pub fn register_case_helpers<'a>(mut h: Handlebars<'a>) -> Handlebars<'a> { + handlebars_helper!(title_case: |s: String| s.to_case(Case::Title)); + h.register_helper("title_case", Box::new(title_case)); + + handlebars_helper!(lower_case: |s: String| s.to_case(Case::Lower)); + h.register_helper("lower_case", Box::new(lower_case)); + + handlebars_helper!(snake_case: |s: String| s.to_case(Case::Snake)); + h.register_helper("snake_case", Box::new(snake_case)); + + handlebars_helper!(kebab_case: |s: String| s.to_case(Case::Kebab)); + h.register_helper("kebab_case", Box::new(kebab_case)); + + handlebars_helper!(camel_case: |s: String| s.to_case(Case::Camel)); + h.register_helper("camel_case", Box::new(camel_case)); + + handlebars_helper!(pascal_case: |s: String| s.to_case(Case::Pascal)); + h.register_helper("pascal_case", Box::new(pascal_case)); + + h +}