use serde::Serialize;
pub use crate::redact::{BasicRedactor, NoopRedactor, Redactor};
pub trait DomainContext {
fn domain_kind(&self) -> &'static str;
fn grouping_key(&self) -> String;
fn to_json(&self) -> serde_json::Value;
}
pub struct Adhoc<T: Serialize> {
pub kind: &'static str,
pub key: String,
pub value: T,
}
impl<T: Serialize> DomainContext for Adhoc<T> {
fn domain_kind(&self) -> &'static str {
self.kind
}
fn grouping_key(&self) -> String {
self.key.clone()
}
fn to_json(&self) -> serde_json::Value {
serde_json::to_value(&self.value).unwrap_or(serde_json::Value::Null)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn adhoc_context_serializes_value() {
let ctx = Adhoc {
kind: "store.test",
key: "kind=0x09".to_owned(),
value: serde_json::json!({ "page_id": 6044 }),
};
assert_eq!(ctx.domain_kind(), "store.test");
assert_eq!(ctx.grouping_key(), "kind=0x09");
assert_eq!(ctx.to_json()["page_id"], 6044);
}
}