1#![deny(missing_docs)]
2
3use vector_common::EventDataEq;
4
5use super::{Event, EventMetadata, LogEvent, Metric, TraceEvent};
6
7#[derive(Clone, Copy, Debug, PartialEq)]
10pub enum EventRef<'a> {
11 Log(&'a LogEvent),
13 Metric(&'a Metric),
15 Trace(&'a TraceEvent),
17}
18
19impl<'a> EventRef<'a> {
20 pub fn as_log(self) -> &'a LogEvent {
26 match self {
27 Self::Log(log) => log,
28 _ => panic!("Failed type coercion, {self:?} is not a log reference"),
29 }
30 }
31
32 pub fn into_log(self) -> LogEvent {
38 match self {
39 Self::Log(log) => log.clone(),
40 _ => panic!("Failed type coercion, {self:?} is not a log reference"),
41 }
42 }
43
44 pub fn as_metric(self) -> &'a Metric {
50 match self {
51 Self::Metric(metric) => metric,
52 _ => panic!("Failed type coercion, {self:?} is not a metric reference"),
53 }
54 }
55
56 pub fn into_metric(self) -> Metric {
62 match self {
63 Self::Metric(metric) => metric.clone(),
64 _ => panic!("Failed type coercion, {self:?} is not a metric reference"),
65 }
66 }
67}
68
69impl<'a> From<&'a Event> for EventRef<'a> {
70 fn from(event: &'a Event) -> Self {
71 match event {
72 Event::Log(log) => EventRef::Log(log),
73 Event::Metric(metric) => EventRef::Metric(metric),
74 Event::Trace(trace) => EventRef::Trace(trace),
75 }
76 }
77}
78
79impl<'a> From<&'a LogEvent> for EventRef<'a> {
80 fn from(log: &'a LogEvent) -> Self {
81 Self::Log(log)
82 }
83}
84
85impl<'a> From<&'a Metric> for EventRef<'a> {
86 fn from(metric: &'a Metric) -> Self {
87 Self::Metric(metric)
88 }
89}
90
91impl<'a> From<&'a TraceEvent> for EventRef<'a> {
92 fn from(trace: &'a TraceEvent) -> Self {
93 Self::Trace(trace)
94 }
95}
96
97impl EventDataEq<Event> for EventRef<'_> {
98 fn event_data_eq(&self, that: &Event) -> bool {
99 match (self, that) {
100 (Self::Log(a), Event::Log(b)) => a.event_data_eq(b),
101 (Self::Metric(a), Event::Metric(b)) => a.event_data_eq(b),
102 (Self::Trace(a), Event::Trace(b)) => a.event_data_eq(b),
103 _ => false,
104 }
105 }
106}
107
108#[derive(Debug)]
111pub enum EventMutRef<'a> {
112 Log(&'a mut LogEvent),
114 Metric(&'a mut Metric),
116 Trace(&'a mut TraceEvent),
118}
119
120impl<'a> EventMutRef<'a> {
121 pub fn as_log(self) -> &'a LogEvent {
127 match self {
128 Self::Log(log) => log,
129 _ => panic!("Failed type coercion, {self:?} is not a log reference"),
130 }
131 }
132
133 pub fn into_log(self) -> LogEvent {
139 match self {
140 Self::Log(log) => log.clone(),
141 _ => panic!("Failed type coercion, {self:?} is not a log reference"),
142 }
143 }
144
145 pub fn as_metric(self) -> &'a Metric {
151 match self {
152 Self::Metric(metric) => metric,
153 _ => panic!("Failed type coercion, {self:?} is not a metric reference"),
154 }
155 }
156
157 pub fn into_metric(self) -> Metric {
163 match self {
164 Self::Metric(metric) => metric.clone(),
165 _ => panic!("Failed type coercion, {self:?} is not a metric reference"),
166 }
167 }
168
169 pub fn metadata(&self) -> &EventMetadata {
171 match self {
172 Self::Log(event) => event.metadata(),
173 Self::Metric(event) => event.metadata(),
174 Self::Trace(event) => event.metadata(),
175 }
176 }
177
178 pub fn metadata_mut(&mut self) -> &mut EventMetadata {
180 match self {
181 Self::Log(event) => event.metadata_mut(),
182 Self::Metric(event) => event.metadata_mut(),
183 Self::Trace(event) => event.metadata_mut(),
184 }
185 }
186}
187
188impl<'a> From<&'a mut Event> for EventMutRef<'a> {
189 fn from(event: &'a mut Event) -> Self {
190 match event {
191 Event::Log(event) => event.into(),
192 Event::Metric(event) => event.into(),
193 Event::Trace(event) => event.into(),
194 }
195 }
196}
197
198impl<'a> From<&'a mut LogEvent> for EventMutRef<'a> {
199 fn from(log: &'a mut LogEvent) -> Self {
200 Self::Log(log)
201 }
202}
203
204impl<'a> From<&'a mut Metric> for EventMutRef<'a> {
205 fn from(metric: &'a mut Metric) -> Self {
206 Self::Metric(metric)
207 }
208}
209
210impl<'a> From<&'a mut TraceEvent> for EventMutRef<'a> {
211 fn from(trace: &'a mut TraceEvent) -> Self {
212 Self::Trace(trace)
213 }
214}