#![deny(missing_docs)]
use vector_common::EventDataEq;
use super::{Event, EventMetadata, LogEvent, Metric, TraceEvent};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum EventRef<'a> {
Log(&'a LogEvent),
Metric(&'a Metric),
Trace(&'a TraceEvent),
}
impl<'a> EventRef<'a> {
pub fn as_log(self) -> &'a LogEvent {
match self {
Self::Log(log) => log,
_ => panic!("Failed type coercion, {self:?} is not a log reference"),
}
}
pub fn into_log(self) -> LogEvent {
match self {
Self::Log(log) => log.clone(),
_ => panic!("Failed type coercion, {self:?} is not a log reference"),
}
}
pub fn as_metric(self) -> &'a Metric {
match self {
Self::Metric(metric) => metric,
_ => panic!("Failed type coercion, {self:?} 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,
}
}
}
#[derive(Debug)]
pub enum EventMutRef<'a> {
Log(&'a mut LogEvent),
Metric(&'a mut Metric),
Trace(&'a mut TraceEvent),
}
impl<'a> EventMutRef<'a> {
pub fn as_log(self) -> &'a LogEvent {
match self {
Self::Log(log) => log,
_ => panic!("Failed type coercion, {self:?} is not a log reference"),
}
}
pub fn into_log(self) -> LogEvent {
match self {
Self::Log(log) => log.clone(),
_ => panic!("Failed type coercion, {self:?} is not a log reference"),
}
}
pub fn as_metric(self) -> &'a Metric {
match self {
Self::Metric(metric) => metric,
_ => panic!("Failed type coercion, {self:?} 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"),
}
}
pub fn metadata(&self) -> &EventMetadata {
match self {
Self::Log(event) => event.metadata(),
Self::Metric(event) => event.metadata(),
Self::Trace(event) => event.metadata(),
}
}
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)
}
}