use std::borrow::Cow;
use super::RedactedText;
use super::RedactionSummary;
use crate::RedactionCompletion;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RedactionTextOutput {
text: RedactedText,
summary: RedactionSummary,
}
impl RedactionTextOutput {
#[must_use]
#[inline(always)]
pub(crate) fn new(text: RedactedText, summary: RedactionSummary) -> Self {
Self { text, summary }
}
#[must_use]
#[inline(always)]
pub const fn text(&self) -> &RedactedText {
&self.text
}
#[must_use]
#[inline(always)]
pub const fn summary(&self) -> &RedactionSummary {
&self.summary
}
#[inline]
pub fn complete_text(&self) -> Result<&RedactedText, &RedactionSummary> {
if self.summary.completion() == RedactionCompletion::Complete {
Ok(&self.text)
} else {
Err(&self.summary)
}
}
#[must_use]
#[inline]
pub fn text_or_marker(&self, marker: &str) -> Cow<'_, str> {
self.complete_text().map_or_else(
|_| {
Cow::Owned(crate::output::log_escape::escape_log_control_characters(Cow::Borrowed(marker)).into_owned())
},
|text| Cow::Borrowed(text.as_str()),
)
}
#[inline]
pub fn into_complete_text(self) -> Result<RedactedText, RedactionSummary> {
if self.summary.completion() == RedactionCompletion::Complete {
Ok(self.text)
} else {
Err(self.summary)
}
}
#[must_use]
#[inline]
pub fn into_text_or_marker(self, marker: &str) -> RedactedText {
self.into_complete_text().unwrap_or_else(|_| {
RedactedText::from_escaped(
crate::output::log_escape::escape_log_control_characters(Cow::Borrowed(marker)).into_owned(),
)
})
}
#[must_use]
#[inline(always)]
pub fn into_parts(self) -> (RedactedText, RedactionSummary) {
(self.text, self.summary)
}
}