use super::LogEvent;
#[derive(Debug)]
pub struct LogEventMergeState {
intermediate_merged_event: LogEvent,
}
impl LogEventMergeState {
pub fn new(first_partial_event: LogEvent) -> Self {
Self {
intermediate_merged_event: first_partial_event,
}
}
pub fn merge_in_next_event(&mut self, incoming: LogEvent, fields: &[impl AsRef<str>]) {
self.intermediate_merged_event.merge(incoming, fields);
}
pub fn merge_in_final_event(
mut self,
incoming: LogEvent,
fields: &[impl AsRef<str>],
) -> LogEvent {
self.merge_in_next_event(incoming, fields);
self.intermediate_merged_event
}
}
#[cfg(test)]
mod test {
use super::*;
fn log_event_with_message(message: &str) -> LogEvent {
LogEvent::from(message)
}
#[test]
fn log_event_merge_state_example() {
let fields = vec!["message".to_string()];
let mut state = LogEventMergeState::new(log_event_with_message("hel"));
state.merge_in_next_event(log_event_with_message("lo "), &fields);
let merged_event = state.merge_in_final_event(log_event_with_message("world"), &fields);
assert_eq!(
merged_event
.get("message")
.unwrap()
.coerce_to_bytes()
.as_ref(),
b"hello world"
);
}
}