opusmeta/
utils.rs

1use std::fmt::Display;
2use std::ops::Deref;
3
4/// A lowercase String. Holds a [`String`] internally.
5#[derive(Debug, Clone)]
6pub struct LowercaseString(pub(crate) String);
7
8impl Deref for LowercaseString {
9    type Target = str;
10
11    fn deref(&self) -> &Self::Target {
12        &self.0
13    }
14}
15
16impl Display for LowercaseString {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        Display::fmt(&self.0, f)
19    }
20}
21
22impl LowercaseString {
23    /// Create a new `LowercaseString`. This will copy the contents of the argument to a newly
24    /// allocated buffer.
25    #[must_use]
26    pub fn new(str: &str) -> Self {
27        Self(str.to_ascii_lowercase())
28    }
29}
30
31impl<T> From<T> for LowercaseString
32where
33    T: AsRef<str>,
34{
35    fn from(value: T) -> Self {
36        Self::new(value.as_ref())
37    }
38}