aboutsummaryrefslogtreecommitdiff
path: root/src/shouty_snake.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/shouty_snake.rs')
-rw-r--r--src/shouty_snake.rs41
1 files changed, 31 insertions, 10 deletions
diff --git a/src/shouty_snake.rs b/src/shouty_snake.rs
index 8f4289a..d504375 100644
--- a/src/shouty_snake.rs
+++ b/src/shouty_snake.rs
@@ -1,3 +1,5 @@
+use std::fmt;
+
use crate::{transform, uppercase};
/// This trait defines a shouty snake case conversion.
@@ -8,39 +10,57 @@ use crate::{transform, uppercase};
/// ## Example:
///
/// ```rust
-/// use heck::ShoutySnakeCase;
-///
+/// use heck::ToShoutySnakeCase;
+///
/// let sentence = "That world is growing in this minute.";
/// assert_eq!(sentence.to_shouty_snake_case(), "THAT_WORLD_IS_GROWING_IN_THIS_MINUTE");
/// ```
-pub trait ShoutySnakeCase: ToOwned {
+pub trait ToShoutySnakeCase: ToOwned {
/// Convert this type to shouty snake case.
fn to_shouty_snake_case(&self) -> Self::Owned;
}
-/// Oh heck, ShoutySnekCase is an alias for ShoutySnakeCase. See ShoutySnakeCase
-/// for more documentation.
-pub trait ShoutySnekCase: ToOwned {
+/// Oh heck, ToShoutySnekCase is an alias for ToShoutySnakeCase. See
+/// ToShoutySnakeCase for more documentation.
+pub trait ToShoutySnekCase: ToOwned {
/// CONVERT THIS TYPE TO SNEK CASE.
#[allow(non_snake_case)]
fn TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned;
}
-impl<T: ?Sized + ShoutySnakeCase> ShoutySnekCase for T {
+impl<T: ?Sized + ToShoutySnakeCase> ToShoutySnekCase for T {
fn TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned {
self.to_shouty_snake_case()
}
}
-impl ShoutySnakeCase for str {
+impl ToShoutySnakeCase for str {
fn to_shouty_snake_case(&self) -> Self::Owned {
- transform(self, uppercase, |s| s.push('_'))
+ AsShoutySnakeCase(self).to_string()
+ }
+}
+
+/// This wrapper performs a shouty snake case conversion in [`fmt::Display`].
+///
+/// ## Example:
+///
+/// ```
+/// use heck::AsShoutySnakeCase;
+///
+/// let sentence = "That world is growing in this minute.";
+/// assert_eq!(format!("{}", AsShoutySnakeCase(sentence)), "THAT_WORLD_IS_GROWING_IN_THIS_MINUTE");
+/// ```
+pub struct AsShoutySnakeCase<T: AsRef<str>>(pub T);
+
+impl<T: AsRef<str>> fmt::Display for AsShoutySnakeCase<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::ShoutySnakeCase;
+ use super::ToShoutySnakeCase;
macro_rules! t {
($t:ident : $s1:expr => $s2:expr) => {
@@ -59,6 +79,7 @@ mod tests {
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");
}