1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#![deny(missing_docs)]

use vector_common::EventDataEq;

use super::{Event, EventMetadata, LogEvent, Metric, TraceEvent};

/// A wrapper for references to inner event types, where reconstituting
/// a full `Event` from a `LogEvent` or `Metric` might be inconvenient.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum EventRef<'a> {
    /// Reference to a `LogEvent`
    Log(&'a LogEvent),
    /// Reference to a `Metric`
    Metric(&'a Metric),
    /// Reference to a `TraceEvent`
    Trace(&'a TraceEvent),
}

impl<'a> EventRef<'a> {
    /// Extract the `LogEvent` reference in this.
    ///
    /// # Panics
    ///
    /// This will panic if this is not a `LogEvent` reference.
    pub fn as_log(self) -> &'a LogEvent {
        match self {
            Self::Log(log) => log,
            _ => panic!("Failed type coercion, {self:?} is not a log reference"),
        }
    }

    /// Convert this reference into a new `LogEvent` by cloning.
    ///
    /// # Panics
    ///
    /// This will panic if this is not a `LogEvent` reference.
    pub fn into_log(self) -> LogEvent {
        match self {
            Self::Log(log) => log.clone(),
            _ => panic!("Failed type coercion, {self:?} is not a log reference"),
        }
    }

    /// Extract the `Metric` reference in this.
    ///
    /// # Panics
    ///
    /// This will panic if this is not a `Metric` reference.
    pub fn as_metric(self) -> &'a Metric {
        match self {
            Self::Metric(metric) => metric,
            _ => panic!("Failed type coercion, {self:?} is not a metric reference"),
        }
    }

    /// Convert this reference into a new `Metric` by cloning.
    ///
    /// # Panics
    ///
    /// This will panic if this is not a `Metric` reference.
    pub fn into_metric(self) -> Metric {
        match self {
            Self::Metric(metric) => metric.clone(),
            _ => panic!("Failed type coercion, {self:?} is not a metric reference"),
        }
    }
}

impl<'a> From<&'a Event> for EventRef<'a> {
    fn from(event: &'a Event) -> Self {
        match event {
            Event::Log(log) => EventRef::Log(log),
            Event::Metric(metric) => EventRef::Metric(metric),
            Event::Trace(trace) => EventRef::Trace(trace),
        }
    }
}

impl<'a> From<&'a LogEvent> for EventRef<'a> {
    fn from(log: &'a LogEvent) -> Self {
        Self::Log(log)
    }
}

impl<'a> From<&'a Metric> for EventRef<'a> {
    fn from(metric: &'a Metric) -> Self {
        Self::Metric(metric)
    }
}

impl<'a> From<&'a TraceEvent> for EventRef<'a> {
    fn from(trace: &'a TraceEvent) -> Self {
        Self::Trace(trace)
    }
}

impl<'a> EventDataEq<Event> for EventRef<'a> {
    fn event_data_eq(&self, that: &Event) -> bool {
        match (self, that) {
            (Self::Log(a), Event::Log(b)) => a.event_data_eq(b),
            (Self::Metric(a), Event::Metric(b)) => a.event_data_eq(b),
            (Self::Trace(a), Event::Trace(b)) => a.event_data_eq(b),
            _ => false,
        }
    }
}

/// A wrapper for mutable references to inner event types, where reconstituting
/// a full `Event` from a `LogEvent` or `Metric` might be inconvenient.
#[derive(Debug)]
pub enum EventMutRef<'a> {
    /// Reference to a `LogEvent`
    Log(&'a mut LogEvent),
    /// Reference to a `Metric`
    Metric(&'a mut Metric),
    /// Reference to a `TraceEvent`
    Trace(&'a mut TraceEvent),
}

impl<'a> EventMutRef<'a> {
    /// Extract the `LogEvent` reference in this.
    ///
    /// # Panics
    ///
    /// This will panic if this is not a `LogEvent` reference.
    pub fn as_log(self) -> &'a LogEvent {
        match self {
            Self::Log(log) => log,
            _ => panic!("Failed type coercion, {self:?} is not a log reference"),
        }
    }

    /// Convert this reference into a new `LogEvent` by cloning.
    ///
    /// # Panics
    ///
    /// This will panic if this is not a `LogEvent` reference.
    pub fn into_log(self) -> LogEvent {
        match self {
            Self::Log(log) => log.clone(),
            _ => panic!("Failed type coercion, {self:?} is not a log reference"),
        }
    }

    /// Extract the `Metric` reference in this.
    ///
    /// # Panics
    ///
    /// This will panic if this is not a `Metric` reference.
    pub fn as_metric(self) -> &'a Metric {
        match self {
            Self::Metric(metric) => metric,
            _ => panic!("Failed type coercion, {self:?} is not a metric reference"),
        }
    }

    /// Convert this reference into a new `Metric` by cloning.
    ///
    /// # Panics
    ///
    /// This will panic if this is not a `Metric` reference.
    pub fn into_metric(self) -> Metric {
        match self {
            Self::Metric(metric) => metric.clone(),
            _ => panic!("Failed type coercion, {self:?} is not a metric reference"),
        }
    }

    /// Access the metadata in this reference.
    pub fn metadata(&self) -> &EventMetadata {
        match self {
            Self::Log(event) => event.metadata(),
            Self::Metric(event) => event.metadata(),
            Self::Trace(event) => event.metadata(),
        }
    }

    /// Access the metadata mutably in this reference.
    pub fn metadata_mut(&mut self) -> &mut EventMetadata {
        match self {
            Self::Log(event) => event.metadata_mut(),
            Self::Metric(event) => event.metadata_mut(),
            Self::Trace(event) => event.metadata_mut(),
        }
    }
}

impl<'a> From<&'a mut Event> for EventMutRef<'a> {
    fn from(event: &'a mut Event) -> Self {
        match event {
            Event::Log(event) => event.into(),
            Event::Metric(event) => event.into(),
            Event::Trace(event) => event.into(),
        }
    }
}

impl<'a> From<&'a mut LogEvent> for EventMutRef<'a> {
    fn from(log: &'a mut LogEvent) -> Self {
        Self::Log(log)
    }
}

impl<'a> From<&'a mut Metric> for EventMutRef<'a> {
    fn from(metric: &'a mut Metric) -> Self {
        Self::Metric(metric)
    }
}

impl<'a> From<&'a mut TraceEvent> for EventMutRef<'a> {
    fn from(trace: &'a mut TraceEvent) -> Self {
        Self::Trace(trace)
    }
}