use std::borrow::Cow;
use serde_json::Value;
use super::RedactionFields;
mod private {
pub trait Sealed {
}
}
#[doc(hidden)]
pub trait RedactJsonValue: private::Sealed {
#[doc(hidden)]
fn write_redacted_json(&self, fields: &mut RedactionFields<'_, '_>, name: &str);
}
macro_rules! json_text {
($($type:ty),+ $(,)?) => {
$(impl private::Sealed for $type {}
impl RedactJsonValue for $type {
fn write_redacted_json(&self, fields: &mut RedactionFields<'_, '_>, name: &str) {
fields.json(name, self.as_ref());
}
})+
};
}
json_text!(String, str, Cow<'_, str>);
impl<T: RedactJsonValue + ?Sized> private::Sealed for &T {}
impl<T: RedactJsonValue + ?Sized> RedactJsonValue for &T {
fn write_redacted_json(&self, fields: &mut RedactionFields<'_, '_>, name: &str) {
(*self).write_redacted_json(fields, name);
}
}
impl<T: RedactJsonValue> private::Sealed for Option<T> {}
impl<T: RedactJsonValue> RedactJsonValue for Option<T> {
fn write_redacted_json(&self, fields: &mut RedactionFields<'_, '_>, name: &str) {
match self {
Some(value) => value.write_redacted_json(fields, name),
None => {
fields.unmarked(name, || Option::<()>::None);
}
}
}
}
impl private::Sealed for Value {}
impl RedactJsonValue for Value {
fn write_redacted_json(&self, fields: &mut RedactionFields<'_, '_>, name: &str) {
fields.json_value(name, self);
}
}