1use std::fmt;
7
8use crate::redact::Bytes;
9
10#[derive(Clone, Eq, PartialEq)]
12pub struct Event {
13 kind: String,
14 payload: Option<Box<[u8]>>,
15}
16
17impl fmt::Debug for Event {
18 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
29 formatter
30 .debug_struct("Event")
31 .field("kind", &self.kind)
32 .field(
33 "payload",
34 &self.payload.as_deref().map(|payload| Bytes(payload.len())),
35 )
36 .finish()
37 }
38}
39
40impl Event {
41 #[must_use]
45 pub const fn new(kind: String, payload: Option<Box<[u8]>>) -> Self {
46 Self { kind, payload }
47 }
48
49 #[must_use]
51 pub fn kind(&self) -> &str {
52 &self.kind
53 }
54
55 #[must_use]
57 pub fn payload(&self) -> Option<&[u8]> {
58 self.payload.as_deref()
59 }
60
61 #[must_use]
63 pub fn into_parts(self) -> (String, Option<Box<[u8]>>) {
64 (self.kind, self.payload)
65 }
66}
67
68#[cfg(feature = "serde")]
74mod serde_impl {
75 use std::fmt::{self, Formatter};
76
77 use serde_core::de::{Deserialize, Deserializer, Error as _, MapAccess, SeqAccess, Visitor};
78 use serde_core::ser::{Serialize, SerializeStruct, Serializer};
79
80 use super::Event;
81 use crate::wire::{self, ByteBuf, Bytes, IdentifierSeed};
82
83 const FIELDS: &[&str] = &["kind", "payload"];
84
85 impl Serialize for Event {
86 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
87 where
88 S: Serializer,
89 {
90 let mut record = serializer.serialize_struct("Event", 2)?;
91 record.serialize_field("kind", &self.kind)?;
92 record.serialize_field("payload", &self.payload.as_deref().map(Bytes))?;
93 record.end()
94 }
95 }
96
97 impl<'de> Deserialize<'de> for Event {
98 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
99 where
100 D: Deserializer<'de>,
101 {
102 deserializer.deserialize_struct("Event", FIELDS, EventVisitor)
103 }
104 }
105
106 struct EventVisitor;
107
108 impl<'de> Visitor<'de> for EventVisitor {
109 type Value = Event;
110
111 fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
112 formatter.write_str("an audit event")
113 }
114
115 fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
116 where
117 A: SeqAccess<'de>,
118 {
119 let kind = wire::element(&mut sequence, 0)?;
120 let payload = wire::element::<Option<ByteBuf>, _>(&mut sequence, 1)?;
121 Ok(Event::new(kind, ByteBuf::into_optional(payload)))
122 }
123
124 fn visit_map<A>(self, mut entries: A) -> Result<Self::Value, A::Error>
125 where
126 A: MapAccess<'de>,
127 {
128 let mut kind = None;
129 let mut payload = None::<Option<ByteBuf>>;
130 while let Some(field) = entries.next_key_seed(IdentifierSeed(FIELDS))? {
131 match field.0.as_str() {
132 "kind" => wire::accept(&mut kind, entries.next_value()?, "kind")?,
133 "payload" => wire::accept(&mut payload, entries.next_value()?, "payload")?,
134 unknown => return Err(A::Error::unknown_field(unknown, FIELDS)),
135 }
136 }
137 Ok(Event::new(
138 wire::require(kind, "kind")?,
139 ByteBuf::into_optional(wire::require(payload, "payload")?),
140 ))
141 }
142 }
143}
144
145#[cfg(test)]
146#[path = "audit_tests.rs"]
147mod audit_tests;