From d641a547c501aee79d02f85824c86020d21c9cb6 Mon Sep 17 00:00:00 2001 From: Laurent Querel Date: Fri, 12 Apr 2024 11:24:38 -0700 Subject: [PATCH 1/6] feat(template): Make kebab-case filter working with digits in the middle of a word. --- crates/weaver_forge/src/config.rs | 23 +++++++++++++++++++++-- crates/weaver_forge/src/lib.rs | 15 +++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/weaver_forge/src/config.rs b/crates/weaver_forge/src/config.rs index 5f87597c..99017460 100644 --- a/crates/weaver_forge/src/config.rs +++ b/crates/weaver_forge/src/config.rs @@ -4,8 +4,10 @@ use std::collections::HashMap; use std::path::Path; +use std::sync::OnceLock; -use convert_case::{Case, Casing}; +use convert_case::Boundary::{DigitLower, DigitUpper, Hyphen, LowerDigit, UpperDigit}; +use convert_case::{Case, Casing, Converter, Pattern}; use globset::{Glob, GlobSet, GlobSetBuilder}; use serde::Deserialize; @@ -295,6 +297,8 @@ impl Default for CaseConvention { impl CaseConvention { pub fn convert(&self, text: &str) -> String { + static KEBAB_CASE: OnceLock = OnceLock::new(); + let text = text.replace('.', "_"); match self { CaseConvention::LowerCase => text.to_case(Case::Lower), @@ -304,7 +308,22 @@ impl CaseConvention { CaseConvention::CamelCase => text.to_case(Case::Camel), CaseConvention::SnakeCase => text.to_case(Case::Snake), CaseConvention::ScreamingSnakeCase => text.to_case(Case::ScreamingSnake), - CaseConvention::KebabCase => text.to_case(Case::Kebab), + CaseConvention::KebabCase => { + // Convert to kebab case but do not consider digits + // as boundaries. So that `k8s` will stay `k8s` and + // not `k-8-s`. + let conv = KEBAB_CASE.get_or_init(|| { + Converter::new() + .add_boundary(Hyphen) + .remove_boundary(DigitLower) + .remove_boundary(DigitUpper) + .remove_boundary(UpperDigit) + .remove_boundary(LowerDigit) + .set_pattern(Pattern::Lowercase) + .set_delim("-") + }); + conv.convert(&text) + } CaseConvention::ScreamingKebabCase => text.to_case(Case::Cobol), } } diff --git a/crates/weaver_forge/src/lib.rs b/crates/weaver_forge/src/lib.rs index b4ede171..03a13360 100644 --- a/crates/weaver_forge/src/lib.rs +++ b/crates/weaver_forge/src/lib.rs @@ -550,6 +550,21 @@ mod tests { expected: "this-is-a-test", case: super::CaseConvention::KebabCase, }, + TestCase { + input: "This is a k8s test", + expected: "this-is-a-k8s-test", + case: super::CaseConvention::KebabCase, + }, + TestCase { + input: "This is a K8S test", + expected: "this-is-a-k8s-test", + case: super::CaseConvention::KebabCase, + }, + TestCase { + input: "This is 2 K8S test", + expected: "this-is-2-k8s-test", + case: super::CaseConvention::KebabCase, + }, TestCase { input: "ThisIsATest", expected: "THIS_IS_A_TEST", From 8d7cd28b2a923723f5f4cc3baf94e19e870b4402 Mon Sep 17 00:00:00 2001 From: Laurent Querel Date: Fri, 12 Apr 2024 15:47:11 -0700 Subject: [PATCH 2/6] docs(forge): fix case converter names --- Cargo.lock | 59 +++++++++++-------- .../weaver_forge/expected_output/converter.md | 10 ++++ crates/weaver_forge/src/lib.rs | 16 ++--- .../weaver_forge/templates/test/converter.md | 11 ++++ .../weaver_forge/templates/test/weaver.yaml | 47 +++++++++++++++ docs/template-engine.md | 16 ++--- 6 files changed, 117 insertions(+), 42 deletions(-) create mode 100644 crates/weaver_forge/expected_output/converter.md create mode 100644 crates/weaver_forge/templates/test/converter.md diff --git a/Cargo.lock b/Cargo.lock index 239c64d8..7dfbe527 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -317,7 +317,7 @@ dependencies = [ "js-sys", "num-traits", "wasm-bindgen", - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -4579,7 +4579,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -4597,7 +4597,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -4617,17 +4617,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" dependencies = [ - "windows_aarch64_gnullvm 0.52.4", - "windows_aarch64_msvc 0.52.4", - "windows_i686_gnu 0.52.4", - "windows_i686_msvc 0.52.4", - "windows_x86_64_gnu 0.52.4", - "windows_x86_64_gnullvm 0.52.4", - "windows_x86_64_msvc 0.52.4", + "windows_aarch64_gnullvm 0.52.5", + "windows_aarch64_msvc 0.52.5", + "windows_i686_gnu 0.52.5", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.5", + "windows_x86_64_gnu 0.52.5", + "windows_x86_64_gnullvm 0.52.5", + "windows_x86_64_msvc 0.52.5", ] [[package]] @@ -4638,9 +4639,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" [[package]] name = "windows_aarch64_msvc" @@ -4650,9 +4651,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" [[package]] name = "windows_i686_gnu" @@ -4662,9 +4663,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" [[package]] name = "windows_i686_msvc" @@ -4674,9 +4681,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" [[package]] name = "windows_x86_64_gnu" @@ -4686,9 +4693,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" [[package]] name = "windows_x86_64_gnullvm" @@ -4698,9 +4705,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" [[package]] name = "windows_x86_64_msvc" @@ -4710,9 +4717,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" [[package]] name = "winnow" diff --git a/crates/weaver_forge/expected_output/converter.md b/crates/weaver_forge/expected_output/converter.md new file mode 100644 index 00000000..dda2a777 --- /dev/null +++ b/crates/weaver_forge/expected_output/converter.md @@ -0,0 +1,10 @@ +This is a TEST +this is a test +THIS IS A TEST +This Is A Test +ThisIsATest +thisIsATest +this_is_a_test +THIS_IS_A_TEST +this-is-a-test +THIS-IS-A-TEST \ No newline at end of file diff --git a/crates/weaver_forge/src/lib.rs b/crates/weaver_forge/src/lib.rs index 03a13360..60b88533 100644 --- a/crates/weaver_forge/src/lib.rs +++ b/crates/weaver_forge/src/lib.rs @@ -355,19 +355,19 @@ impl TemplateEngine { "field_name", case_converter(self.target_config.field_name.clone()), ); - env.add_filter("lowercase", case_converter(CaseConvention::LowerCase)); - env.add_filter("UPPERCASE", case_converter(CaseConvention::UpperCase)); - env.add_filter("TitleCase", case_converter(CaseConvention::TitleCase)); - env.add_filter("PascalCase", case_converter(CaseConvention::PascalCase)); - env.add_filter("camelCase", case_converter(CaseConvention::CamelCase)); + env.add_filter("lower_case", case_converter(CaseConvention::LowerCase)); + env.add_filter("upper_case", case_converter(CaseConvention::UpperCase)); + env.add_filter("title_case", case_converter(CaseConvention::TitleCase)); + env.add_filter("pascal_case", case_converter(CaseConvention::PascalCase)); + env.add_filter("camel_case", case_converter(CaseConvention::CamelCase)); env.add_filter("snake_case", case_converter(CaseConvention::SnakeCase)); env.add_filter( - "SCREAMING_SNAKE_CASE", + "screaming_snake_case", case_converter(CaseConvention::ScreamingSnakeCase), ); - env.add_filter("kebab-case", case_converter(CaseConvention::KebabCase)); + env.add_filter("kebab_case", case_converter(CaseConvention::KebabCase)); env.add_filter( - "SCREAMING-KEBAB-CASE", + "screaming_kebab_case", case_converter(CaseConvention::ScreamingKebabCase), ); diff --git a/crates/weaver_forge/templates/test/converter.md b/crates/weaver_forge/templates/test/converter.md new file mode 100644 index 00000000..095ab7c5 --- /dev/null +++ b/crates/weaver_forge/templates/test/converter.md @@ -0,0 +1,11 @@ +{%- set text = "This is a TEST" -%} +{{ text }} +{{ text | lower_case }} +{{ text | upper_case }} +{{ text | title_case }} +{{ text | pascal_case }} +{{ text | camel_case }} +{{ text | snake_case }} +{{ text | screaming_snake_case }} +{{ text | kebab_case }} +{{ text | screaming_kebab_case }} \ No newline at end of file diff --git a/crates/weaver_forge/templates/test/weaver.yaml b/crates/weaver_forge/templates/test/weaver.yaml index 5c836f7b..4ee208be 100644 --- a/crates/weaver_forge/templates/test/weaver.yaml +++ b/crates/weaver_forge/templates/test/weaver.yaml @@ -13,3 +13,50 @@ type_mapping: "double[]": "[]double" "boolean[]": "[]bool" "string[]": "[]string" + +templates: + - pattern: converter.md + filter: . + application_mode: single + - pattern: registry.md + filter: . + application_mode: single + - pattern: attribute_group.md + filter: .groups[] | select(.type == "attribute_group") + application_mode: each + - pattern: attribute_groups.md + filter: .groups[] | select(.type == "attribute_group") + application_mode: single + - pattern: event.md + filter: .groups[] | select(.type == "event") + application_mode: each + - pattern: events.md + filter: .groups[] | select(.type == "event") + application_mode: single + - pattern: group.md + filter: .groups + application_mode: each + - pattern: groups.md + filter: .groups + application_mode: single + - pattern: metric.md + filter: .groups[] | select(.type == "metric") + application_mode: each + - pattern: metrics.md + filter: .groups[] | select(.type == "metric") + application_mode: single + - pattern: resource.md + filter: .groups[] | select(.type == "resource") + application_mode: each + - pattern: resources.md + filter: .groups[] | select(.type == "resource") + application_mode: single + - pattern: span.md + filter: .groups[] | select(.type == "span") + application_mode: each + - pattern: spans.md + filter: .groups[] | select(.type == "span") + application_mode: single + - pattern: groups_per_prefix.md + filter: '.groups | map(select(.prefix != null and .prefix != "")) | group_by(.prefix) | map({prefix: .[0].prefix, groups: .})' + application_mode: each \ No newline at end of file diff --git a/docs/template-engine.md b/docs/template-engine.md index e9c7a14f..b712c5aa 100644 --- a/docs/template-engine.md +++ b/docs/template-engine.md @@ -192,15 +192,15 @@ The following filters are available: - `struct_name`: Converts a string to a struct name. - `field_name`: Converts a string to a field name. - `type_mapping`: Converts a semantic convention type to a language type. -- `lowercase`: Converts a string to lowercase. -- `UPPERCASE`: Converts a string to UPPERCASE. -- `TitleCase`: Converts a string to TitleCase. -- `PascalCase`: Converts a string to PascalCase. -- `camelCase`: Converts a string to camelCase. +- `lower_case`: Converts a string to lowercase. +- `upper_case`: Converts a string to UPPERCASE. +- `title_case`: Converts a string to TitleCase. +- `pascal_case`: Converts a string to PascalCase. +- `camel_case`: Converts a string to camelCase. - `snake_case`: Converts a string to snake_case. -- `SCREAMING_SNAKE_CASE`: Converts a string to SCREAMING_SNAKE_CASE. -- `kebab-case`: Converts a string to kebab-case. -- `SCREAMING-KEBAB-CASE`: Converts a string to SCREAMING-KEBAB-CASE. +- `screaming_snake_case`: Converts a string to SCREAMING_SNAKE_CASE. +- `kebab_case`: Converts a string to kebab-case. +- `screaming_kebab_case`: Converts a string to SCREAMING-KEBAB-CASE. > Note: Other filters might be introduced in the future. From 34fbf37bf95b399e86920beca5b7f9d096d99aa5 Mon Sep 17 00:00:00 2001 From: Laurent Querel Date: Fri, 12 Apr 2024 16:07:11 -0700 Subject: [PATCH 3/6] coverage(forge): Fix test coverage regression --- crates/weaver_forge/src/lib.rs | 14 +++++- .../weaver_forge/templates/test/weaver.yaml | 49 +------------------ 2 files changed, 14 insertions(+), 49 deletions(-) diff --git a/crates/weaver_forge/src/lib.rs b/crates/weaver_forge/src/lib.rs index 60b88533..4ac22491 100644 --- a/crates/weaver_forge/src/lib.rs +++ b/crates/weaver_forge/src/lib.rs @@ -457,18 +457,21 @@ fn split_id(value: Value) -> Result, minijinja::Error> { #[cfg(test)] mod tests { + use globset::Glob; use std::collections::HashSet; use std::fs; use std::path::Path; use walkdir::WalkDir; + use crate::config::{ApplicationMode, TemplateConfig}; use weaver_diff::diff_output; use weaver_logger::TestLogger; use weaver_resolver::SchemaResolver; use weaver_semconv::SemConvRegistry; use crate::debug::print_dedup_errors; + use crate::filter::Filter; use crate::registry::TemplateRegistry; #[test] @@ -596,9 +599,18 @@ mod tests { #[test] fn test() { let logger = TestLogger::default(); - let engine = super::TemplateEngine::try_new("test", super::GeneratorConfig::default()) + let mut engine = super::TemplateEngine::try_new("test", super::GeneratorConfig::default()) .expect("Failed to create template engine"); + // Add a template configuration for converter.md on top + // of the default template configuration. This is useful + // for test coverage purposes. + engine.target_config.templates.push(TemplateConfig { + pattern: Glob::new("converter.md").unwrap(), + filter: Filter::try_new(".").unwrap(), + application_mode: ApplicationMode::Single, + }); + let registry_id = "default"; let mut registry = SemConvRegistry::try_from_path(registry_id, "data/*.yaml") .expect("Failed to load registry"); diff --git a/crates/weaver_forge/templates/test/weaver.yaml b/crates/weaver_forge/templates/test/weaver.yaml index 4ee208be..452e1bf4 100644 --- a/crates/weaver_forge/templates/test/weaver.yaml +++ b/crates/weaver_forge/templates/test/weaver.yaml @@ -12,51 +12,4 @@ type_mapping: "int[]": "[]int64" "double[]": "[]double" "boolean[]": "[]bool" - "string[]": "[]string" - -templates: - - pattern: converter.md - filter: . - application_mode: single - - pattern: registry.md - filter: . - application_mode: single - - pattern: attribute_group.md - filter: .groups[] | select(.type == "attribute_group") - application_mode: each - - pattern: attribute_groups.md - filter: .groups[] | select(.type == "attribute_group") - application_mode: single - - pattern: event.md - filter: .groups[] | select(.type == "event") - application_mode: each - - pattern: events.md - filter: .groups[] | select(.type == "event") - application_mode: single - - pattern: group.md - filter: .groups - application_mode: each - - pattern: groups.md - filter: .groups - application_mode: single - - pattern: metric.md - filter: .groups[] | select(.type == "metric") - application_mode: each - - pattern: metrics.md - filter: .groups[] | select(.type == "metric") - application_mode: single - - pattern: resource.md - filter: .groups[] | select(.type == "resource") - application_mode: each - - pattern: resources.md - filter: .groups[] | select(.type == "resource") - application_mode: single - - pattern: span.md - filter: .groups[] | select(.type == "span") - application_mode: each - - pattern: spans.md - filter: .groups[] | select(.type == "span") - application_mode: single - - pattern: groups_per_prefix.md - filter: '.groups | map(select(.prefix != null and .prefix != "")) | group_by(.prefix) | map({prefix: .[0].prefix, groups: .})' - application_mode: each \ No newline at end of file + "string[]": "[]string" \ No newline at end of file From 2275bff99c7b520f0977eb8bdc5063ee06f7d23e Mon Sep 17 00:00:00 2001 From: Laurent Querel Date: Fri, 12 Apr 2024 16:26:32 -0700 Subject: [PATCH 4/6] chore(forge): Fix unit test --- Cargo.lock | 4 ++-- crates/weaver_forge/src/lib.rs | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7dfbe527..64380baf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2381,9 +2381,9 @@ dependencies = [ [[package]] name = "num" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +checksum = "3135b08af27d103b0a51f2ae0f8632117b7b185ccf931445affa8df530576a41" dependencies = [ "num-bigint", "num-complex", diff --git a/crates/weaver_forge/src/lib.rs b/crates/weaver_forge/src/lib.rs index 4ac22491..586cab58 100644 --- a/crates/weaver_forge/src/lib.rs +++ b/crates/weaver_forge/src/lib.rs @@ -186,8 +186,7 @@ impl TemplateEngine { .filter(|dir_entry| dir_entry.path().is_file()) .collect(); - let config = TargetConfig::try_new(&self.path)?; - let tmpl_matcher = config.template_matcher()?; + let tmpl_matcher = self.target_config.template_matcher()?; // Create a read-only context for the filter evaluations let context = serde_json::to_value(context).map_err(|e| ContextSerializationFailed { From afdf547f705fae69d5b4c751fdb3998100d352a9 Mon Sep 17 00:00:00 2001 From: Laurent Querel Date: Mon, 15 Apr 2024 09:15:09 -0700 Subject: [PATCH 5/6] chore(forge): Fix lower and title cases based on @jsuereth feedback See https://github.com/open-telemetry/weaver/pull/106#discussion_r1565819682 --- crates/weaver_forge/src/config.rs | 37 ++++++++++++++++++++++++++++--- crates/weaver_forge/src/lib.rs | 8 +++---- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/crates/weaver_forge/src/config.rs b/crates/weaver_forge/src/config.rs index 99017460..8369e271 100644 --- a/crates/weaver_forge/src/config.rs +++ b/crates/weaver_forge/src/config.rs @@ -6,7 +6,7 @@ use std::collections::HashMap; use std::path::Path; use std::sync::OnceLock; -use convert_case::Boundary::{DigitLower, DigitUpper, Hyphen, LowerDigit, UpperDigit}; +use convert_case::Boundary::{DigitLower, DigitUpper, Hyphen, LowerDigit, Space, UpperDigit}; use convert_case::{Case, Casing, Converter, Pattern}; use globset::{Glob, GlobSet, GlobSetBuilder}; use serde::Deserialize; @@ -297,13 +297,44 @@ impl Default for CaseConvention { impl CaseConvention { pub fn convert(&self, text: &str) -> String { + static LOWER_CASE: OnceLock = OnceLock::new(); + static TITLE_CASE: OnceLock = OnceLock::new(); static KEBAB_CASE: OnceLock = OnceLock::new(); let text = text.replace('.', "_"); match self { - CaseConvention::LowerCase => text.to_case(Case::Lower), + CaseConvention::LowerCase => { + // Convert to lower case but do not consider digits + // as boundaries. So that `k8s` will stay `k8s` and + // not `k-8-s`. + let conv = LOWER_CASE.get_or_init(|| { + Converter::new() + .add_boundary(Space) + .remove_boundary(DigitLower) + .remove_boundary(DigitUpper) + .remove_boundary(UpperDigit) + .remove_boundary(LowerDigit) + .set_pattern(Pattern::Lowercase) + .set_delim(" ") + }); + conv.convert(&text) + }, CaseConvention::UpperCase => text.to_case(Case::Upper), - CaseConvention::TitleCase => text.to_case(Case::Title), + CaseConvention::TitleCase => { + // Convert to title case but do not consider digits + // as boundaries. + let conv = TITLE_CASE.get_or_init(|| { + Converter::new() + .add_boundary(Space) + .remove_boundary(DigitLower) + .remove_boundary(DigitUpper) + .remove_boundary(UpperDigit) + .remove_boundary(LowerDigit) + .set_pattern(Pattern::Capital) + .set_delim(" ") + }); + conv.convert(&text) + }, CaseConvention::PascalCase => text.to_case(Case::Pascal), CaseConvention::CamelCase => text.to_case(Case::Camel), CaseConvention::SnakeCase => text.to_case(Case::Snake), diff --git a/crates/weaver_forge/src/lib.rs b/crates/weaver_forge/src/lib.rs index 586cab58..25dd1a4d 100644 --- a/crates/weaver_forge/src/lib.rs +++ b/crates/weaver_forge/src/lib.rs @@ -488,8 +488,8 @@ mod tests { case: super::CaseConvention::LowerCase, }, TestCase { - input: "This is a TEST", - expected: "this is a test", + input: "This is a K8S TEST", + expected: "this is a k8s test", case: super::CaseConvention::LowerCase, }, TestCase { @@ -508,8 +508,8 @@ mod tests { case: super::CaseConvention::TitleCase, }, TestCase { - input: "This is a TEST", - expected: "This Is A Test", + input: "This is a k8s TEST", + expected: "This Is A K8s Test", case: super::CaseConvention::TitleCase, }, TestCase { From a5dd4514731b69004f230caacccb313afed3ea79 Mon Sep 17 00:00:00 2001 From: Laurent Querel Date: Mon, 15 Apr 2024 22:53:20 -0700 Subject: [PATCH 6/6] chore(template): format code --- crates/weaver_forge/src/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/weaver_forge/src/config.rs b/crates/weaver_forge/src/config.rs index 8369e271..6a378a52 100644 --- a/crates/weaver_forge/src/config.rs +++ b/crates/weaver_forge/src/config.rs @@ -318,7 +318,7 @@ impl CaseConvention { .set_delim(" ") }); conv.convert(&text) - }, + } CaseConvention::UpperCase => text.to_case(Case::Upper), CaseConvention::TitleCase => { // Convert to title case but do not consider digits @@ -334,7 +334,7 @@ impl CaseConvention { .set_delim(" ") }); conv.convert(&text) - }, + } CaseConvention::PascalCase => text.to_case(Case::Pascal), CaseConvention::CamelCase => text.to_case(Case::Camel), CaseConvention::SnakeCase => text.to_case(Case::Snake),