use std::{
borrow::Cow,
fmt::{
self,
Display,
Formatter,
},
};
use super::{
BoundedLogSafeDisplay,
LogOutputLimit,
};
#[must_use = "render or otherwise consume the log-safe value"]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LogSafeText<'a>(
Cow<'a, str>,
);
impl<'a> LogSafeText<'a> {
#[inline(always)]
pub(crate) const fn from_escaped(value: Cow<'a, str>) -> Self {
Self(value)
}
#[inline(always)]
pub fn as_str(&self) -> &str {
self.0.as_ref()
}
#[inline(always)]
pub fn into_owned(self) -> String {
self.0.into_owned()
}
#[must_use = "format the bounded log-safe text"]
#[inline(always)]
pub const fn with_output_limit(
&self,
limit: LogOutputLimit,
) -> BoundedLogSafeDisplay<'_> {
BoundedLogSafeDisplay::new(self, limit)
}
}
impl AsRef<str> for LogSafeText<'_> {
#[inline(always)]
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Display for LogSafeText<'_> {
#[inline(always)]
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}