vector_core/event/
ref.rs

1#![deny(missing_docs)]
2
3use vector_common::EventDataEq;
4
5use super::{Event, EventMetadata, LogEvent, Metric, TraceEvent};
6
7/// A wrapper for references to inner event types, where reconstituting
8/// a full `Event` from a `LogEvent` or `Metric` might be inconvenient.
9#[derive(Clone, Copy, Debug, PartialEq)]
10pub enum EventRef<'a> {
11    /// Reference to a `LogEvent`
12    Log(&'a LogEvent),
13    /// Reference to a `Metric`
14    Metric(&'a Metric),
15    /// Reference to a `TraceEvent`
16    Trace(&'a TraceEvent),
17}
18
19impl<'a> EventRef<'a> {
20    /// Extract the `LogEvent` reference in this.
21    ///
22    /// # Panics
23    ///
24    /// This will panic if this is not a `LogEvent` reference.
25    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    /// Convert this reference into a new `LogEvent` by cloning.
33    ///
34    /// # Panics
35    ///
36    /// This will panic if this is not a `LogEvent` reference.
37    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    /// Extract the `Metric` reference in this.
45    ///
46    /// # Panics
47    ///
48    /// This will panic if this is not a `Metric` reference.
49    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    /// Convert this reference into a new `Metric` by cloning.
57    ///
58    /// # Panics
59    ///
60    /// This will panic if this is not a `Metric` reference.
61    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/// A wrapper for mutable references to inner event types, where reconstituting
109/// a full `Event` from a `LogEvent` or `Metric` might be inconvenient.
110#[derive(Debug)]
111pub enum EventMutRef<'a> {
112    /// Reference to a `LogEvent`
113    Log(&'a mut LogEvent),
114    /// Reference to a `Metric`
115    Metric(&'a mut Metric),
116    /// Reference to a `TraceEvent`
117    Trace(&'a mut TraceEvent),
118}
119
120impl<'a> EventMutRef<'a> {
121    /// Extract the `LogEvent` reference in this.
122    ///
123    /// # Panics
124    ///
125    /// This will panic if this is not a `LogEvent` reference.
126    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    /// Convert this reference into a new `LogEvent` by cloning.
134    ///
135    /// # Panics
136    ///
137    /// This will panic if this is not a `LogEvent` reference.
138    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    /// Extract the `Metric` reference in this.
146    ///
147    /// # Panics
148    ///
149    /// This will panic if this is not a `Metric` reference.
150    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    /// Convert this reference into a new `Metric` by cloning.
158    ///
159    /// # Panics
160    ///
161    /// This will panic if this is not a `Metric` reference.
162    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    /// Access the metadata in this reference.
170    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    /// Access the metadata mutably in this reference.
179    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}