diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/heck/src/shouty_kebab.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/heck/src/shouty_kebab.rs')
-rw-r--r-- | third_party/rust/heck/src/shouty_kebab.rs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/third_party/rust/heck/src/shouty_kebab.rs b/third_party/rust/heck/src/shouty_kebab.rs new file mode 100644 index 0000000000..e679978bef --- /dev/null +++ b/third_party/rust/heck/src/shouty_kebab.rs @@ -0,0 +1,72 @@ +use std::fmt; + +use crate::{transform, uppercase}; + +/// This trait defines a shouty kebab case conversion. +/// +/// In SHOUTY-KEBAB-CASE, word boundaries are indicated by hyphens and all +/// words are in uppercase. +/// +/// ## Example: +/// +/// ```rust +/// use heck::ToShoutyKebabCase; +/// +/// let sentence = "We are going to inherit the earth."; +/// assert_eq!(sentence.to_shouty_kebab_case(), "WE-ARE-GOING-TO-INHERIT-THE-EARTH"); +/// ``` +pub trait ToShoutyKebabCase: ToOwned { + /// Convert this type to shouty kebab case. + fn to_shouty_kebab_case(&self) -> Self::Owned; +} + +impl ToShoutyKebabCase for str { + fn to_shouty_kebab_case(&self) -> Self::Owned { + AsShoutyKebabCase(self).to_string() + } +} + +/// This wrapper performs a kebab case conversion in [`fmt::Display`]. +/// +/// ## Example: +/// +/// ``` +/// use heck::AsShoutyKebabCase; +/// +/// let sentence = "We are going to inherit the earth."; +/// assert_eq!(format!("{}", AsShoutyKebabCase(sentence)), "WE-ARE-GOING-TO-INHERIT-THE-EARTH"); +/// ``` +pub struct AsShoutyKebabCase<T: AsRef<str>>(pub T); + +impl<T: AsRef<str>> fmt::Display for AsShoutyKebabCase<T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + transform(self.0.as_ref(), uppercase, |f| write!(f, "-"), f) + } +} + +#[cfg(test)] +mod tests { + use super::ToShoutyKebabCase; + + macro_rules! t { + ($t:ident : $s1:expr => $s2:expr) => { + #[test] + fn $t() { + assert_eq!($s1.to_shouty_kebab_case(), $s2) + } + }; + } + + t!(test1: "CamelCase" => "CAMEL-CASE"); + t!(test2: "This is Human case." => "THIS-IS-HUMAN-CASE"); + t!(test3: "MixedUP CamelCase, with some Spaces" => "MIXED-UP-CAMEL-CASE-WITH-SOME-SPACES"); + t!(test4: "mixed_up_ snake_case with some _spaces" => "MIXED-UP-SNAKE-CASE-WITH-SOME-SPACES"); + t!(test5: "kebab-case" => "KEBAB-CASE"); + t!(test6: "SHOUTY_SNAKE_CASE" => "SHOUTY-SNAKE-CASE"); + t!(test7: "snake_case" => "SNAKE-CASE"); + t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "THIS-CONTAINS-ALL-KINDS-OF-WORD-BOUNDARIES"); + #[cfg(feature = "unicode")] + t!(test9: "XΣXΣ baffle" => "XΣXΣ-BAFFLE"); + t!(test10: "XMLHttpRequest" => "XML-HTTP-REQUEST"); + t!(test11: "SHOUTY-KEBAB-CASE" => "SHOUTY-KEBAB-CASE"); +} |