use std::borrow::Cow;
#[derive(Debug, Clone)]
pub struct UrlTemplateLabel(Cow<'static, str>);
impl UrlTemplateLabel {
#[must_use]
pub fn new(label: impl Into<Cow<'static, str>>) -> Self {
Self(label.into())
}
#[must_use]
pub const fn new_static(label: &'static str) -> Self {
Self(Cow::Borrowed(label))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn into_cow(self) -> Cow<'static, str> {
self.0
}
}
impl std::fmt::Display for UrlTemplateLabel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl AsRef<str> for UrlTemplateLabel {
fn as_ref(&self) -> &str {
&self.0
}
}
impl From<&'static str> for UrlTemplateLabel {
fn from(label: &'static str) -> Self {
Self::new_static(label)
}
}
impl From<String> for UrlTemplateLabel {
fn from(label: String) -> Self {
Self::new(label)
}
}
impl From<UrlTemplateLabel> for Cow<'static, str> {
fn from(label: UrlTemplateLabel) -> Self {
label.into_cow()
}
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use super::*;
#[test]
fn new_with_static_str() {
let label = UrlTemplateLabel::new("/api/users/{id}");
assert_eq!(label.as_str(), "/api/users/{id}");
}
#[test]
fn new_with_string() {
let label = UrlTemplateLabel::new("/api/users/{id}".to_string());
assert_eq!(label.as_str(), "/api/users/{id}");
}
#[test]
fn new_with_cow() {
let cow: Cow<'static, str> = Cow::Owned("/api/users/{id}".to_string());
let label = UrlTemplateLabel::new(cow);
assert_eq!(label.as_str(), "/api/users/{id}");
}
#[test]
fn new_static_creates_borrowed_cow() {
let label = UrlTemplateLabel::new_static("/api/users/{id}");
assert_eq!(label.as_str(), "/api/users/{id}");
assert!(matches!(label.0, Cow::Borrowed(_)));
}
#[test]
fn into_cow_via_from() {
let label = UrlTemplateLabel::new("/api/users/{id}");
let cow: Cow<'static, str> = label.into();
assert_eq!(cow, "/api/users/{id}");
}
#[test]
fn from_static_str() {
let label: UrlTemplateLabel = "/api/users/{id}".into();
assert_eq!(label.as_str(), "/api/users/{id}");
}
#[test]
fn from_string() {
let label: UrlTemplateLabel = "/api/users/{id}".to_string().into();
assert_eq!(label.as_str(), "/api/users/{id}");
}
#[test]
fn display_impl() {
let label = UrlTemplateLabel::new("/api/users/{id}");
assert_eq!(format!("{label}"), "/api/users/{id}");
}
#[test]
fn as_ref_str_impl() {
let label = UrlTemplateLabel::new("/api/users/{id}");
let s: &str = label.as_ref();
assert_eq!(s, "/api/users/{id}");
}
}