use serde_json::Value;
use serde_json::to_writer;
use super::internal::RedactedValue;
use crate::RedactionPolicy;
use crate::runtime::OperationByteSink;
pub(super) enum BoundedJsonRedaction {
Complete(
String,
),
Truncated(
String,
),
Invalid(
String,
),
}
impl BoundedJsonRedaction {
#[must_use]
#[inline]
pub(super) fn into_parts(self) -> (String, bool, bool) {
match self {
Self::Complete(text) => (text, false, false),
Self::Truncated(text) => (text, true, false),
Self::Invalid(text) => (text, false, true),
}
}
}
pub(super) fn redacted_json_value_bounded(
value: &Value,
policy: &RedactionPolicy,
max_output: usize,
) -> BoundedJsonRedaction {
let mut writer = OperationByteSink::new(max_output);
let redacted = RedactedValue::root(value, policy);
if to_writer(&mut writer, &redacted).is_err() {
return BoundedJsonRedaction::Truncated("<truncated>".to_owned());
}
BoundedJsonRedaction::Complete(
writer
.into_string()
.unwrap_or_else(|| policy.masking().mask_opaque(crate::Sensitivity::Secret).to_owned()),
)
}