use std::fmt;
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Redaction(Box<str>);
impl Redaction {
#[must_use]
pub fn new(replacement: impl Into<Box<str>>) -> Self {
Self(replacement.into())
}
#[must_use]
pub fn hidden() -> Self {
Self::default()
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Default for Redaction {
fn default() -> Self {
Self::new("[REDACTED]")
}
}
impl AsRef<str> for Redaction {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for Redaction {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl From<&str> for Redaction {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl From<String> for Redaction {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl From<Box<str>> for Redaction {
fn from(value: Box<str>) -> Self {
Self(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hidden_uses_safe_default_marker() {
assert_eq!(Redaction::hidden().as_str(), "[REDACTED]");
}
#[test]
fn supports_custom_replacement_text() {
let redaction = Redaction::new("***");
assert_eq!(redaction.as_str(), "***");
assert_eq!(redaction.to_string(), "***");
}
}