use std::ffi::OsStr;
use std::fmt::Display;
#[cfg(feature = "http")]
use http::HeaderMap;
#[cfg(feature = "http")]
use http::HeaderValue;
#[cfg(feature = "json")]
use serde_json::Value;
use super::DiagnosticRedactionBatchOutput;
use super::DiagnosticRedactionHandle;
use super::DiagnosticRedactionOutput;
use crate::domain::Redact;
#[cfg(feature = "http")]
use crate::formats::http::BodyCapture;
use crate::runtime::BatchSession;
use crate::runtime::RedactionHandle;
const DEFAULT_DIAGNOSTIC_MARKER: &str = "<redaction incomplete>";
pub struct DiagnosticRedactionBatch {
session: BatchSession,
}
impl DiagnosticRedactionBatch {
#[must_use]
#[inline(always)]
pub(crate) const fn from_session(session: BatchSession) -> Self {
Self { session }
}
#[must_use]
#[inline(always)]
fn wrap(handle: RedactionHandle) -> DiagnosticRedactionHandle {
let (batch_id, item_index) = handle.parts();
DiagnosticRedactionHandle { batch_id, item_index }
}
#[must_use]
#[inline(always)]
pub fn is_output_exhausted(&self) -> bool {
self.session.is_output_exhausted()
}
#[must_use]
#[inline(always)]
pub fn redact_field<T>(&mut self, field: &str, value: &T) -> DiagnosticRedactionHandle
where
T: Display + ?Sized,
{
let handle = self.session.redact_field(field, value);
let (batch_id, item_index) = handle.parts();
DiagnosticRedactionHandle { batch_id, item_index }
}
#[must_use]
#[inline(always)]
pub fn redact_value<T>(&mut self, value: &T) -> DiagnosticRedactionHandle
where
T: Redact + ?Sized,
{
let handle = self.session.redact_value(value);
let (batch_id, item_index) = handle.parts();
DiagnosticRedactionHandle { batch_id, item_index }
}
#[must_use]
#[inline(always)]
pub fn redact_argv<'items, I>(&mut self, items: I) -> DiagnosticRedactionHandle
where
I: IntoIterator<Item = crate::formats::argv::ArgvItem<'items>>,
{
Self::wrap(self.session.redact_argv(items))
}
#[must_use]
#[inline(always)]
pub fn redact_heuristic_argv<'items, I>(&mut self, items: I) -> DiagnosticRedactionHandle
where
I: IntoIterator<Item = crate::formats::argv::ArgvItem<'items>>,
{
Self::wrap(self.session.redact_heuristic_argv(items))
}
#[must_use]
#[inline(always)]
pub fn redact_env(&mut self, name: &str, value: &str) -> DiagnosticRedactionHandle {
Self::wrap(self.session.redact_env(name, value))
}
#[must_use]
#[inline(always)]
pub fn redact_env_pairs<'items, I>(&mut self, pairs: I) -> DiagnosticRedactionHandle
where
I: IntoIterator<Item = (&'items OsStr, &'items OsStr)>,
{
Self::wrap(self.session.redact_env_pairs(pairs))
}
#[must_use]
#[inline(always)]
pub fn redact_process<'arguments, 'variables, A, E>(
&mut self,
program: &'arguments OsStr,
arguments: A,
variables: E,
) -> DiagnosticRedactionHandle
where
A: IntoIterator<Item = crate::formats::argv::ArgvItem<'arguments>>,
E: IntoIterator<Item = (&'variables OsStr, &'variables OsStr)>,
{
Self::wrap(self.session.redact_process(program, arguments, variables))
}
#[cfg(feature = "json")]
#[must_use]
#[inline(always)]
pub fn redact_json(&mut self, text: &str) -> DiagnosticRedactionHandle {
Self::wrap(self.session.redact_json(text))
}
#[cfg(feature = "json")]
#[must_use]
#[inline(always)]
pub fn redact_json_value(&mut self, value: &Value) -> DiagnosticRedactionHandle {
Self::wrap(self.session.redact_json_value(value))
}
#[cfg(feature = "http")]
#[must_use]
#[inline(always)]
pub fn redact_http_url(&mut self, value: &str) -> DiagnosticRedactionHandle {
Self::wrap(self.session.redact_http_url(value))
}
#[cfg(feature = "http")]
#[must_use]
#[inline(always)]
pub fn redact_http_headers(&mut self, headers: &HeaderMap) -> DiagnosticRedactionHandle {
Self::wrap(self.session.redact_http_headers(headers))
}
#[cfg(feature = "http")]
#[must_use]
#[inline(always)]
pub fn redact_http_body(
&mut self,
capture: BodyCapture<'_>,
content_type: Option<&HeaderValue>,
) -> DiagnosticRedactionHandle {
Self::wrap(self.session.redact_http_body(capture, content_type))
}
#[cfg(feature = "http")]
#[must_use]
#[inline(always)]
pub fn redact_http_body_with_content_type_text(
&mut self,
capture: BodyCapture<'_>,
content_type: Option<&str>,
) -> DiagnosticRedactionHandle {
Self::wrap(
self.session
.redact_http_body_with_content_type_text(capture, content_type),
)
}
#[cfg(feature = "uri")]
#[must_use]
#[inline(always)]
pub fn redact_uri(&mut self, value: &str) -> DiagnosticRedactionHandle {
Self::wrap(self.session.redact_uri(value))
}
#[must_use]
#[inline(always)]
pub fn finish_with_marker(self, marker: &str) -> DiagnosticRedactionOutput {
DiagnosticRedactionOutput::new(self.finish_publication(), marker)
}
#[must_use]
#[inline(always)]
pub fn finish(self) -> DiagnosticRedactionOutput {
self.finish_with_marker(DEFAULT_DIAGNOSTIC_MARKER)
}
#[must_use]
#[inline(always)]
pub(crate) fn finish_publication(self) -> DiagnosticRedactionBatchOutput {
DiagnosticRedactionBatchOutput::from_publication(self.session.finish())
}
}