use std::{borrow::Cow, collections::HashMap};
use crate::LogValue;
pub type LogRecordsIter<'a> = std::collections::hash_map::Iter<'a, Cow<'static, str>, LogValue>;
pub type LogRecordsIntoIter = std::collections::hash_map::IntoIter<Cow<'static, str>, LogValue>;
pub type LogRecord = (Cow<'static, str>, LogValue);
pub type LogRecordRef<'a> = (&'a Cow<'static, str>, &'a LogValue);
#[derive(Debug, Clone, Default)]
pub struct LogRecords(pub(crate) HashMap<Cow<'static, str>, LogValue>);
impl LogRecords {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn field(mut self, key: impl Into<Cow<'static, str>>, value: impl Into<LogValue>) -> Self {
self.insert(key, value);
self
}
pub fn insert(&mut self, key: impl Into<Cow<'static, str>>, value: impl Into<LogValue>) {
self.0.insert(key.into(), value.into());
}
pub fn extend(&mut self, other: impl IntoIterator<Item = LogRecord>) {
self.0.extend(other);
}
#[must_use]
pub fn iter(&self) -> LogRecordsIter<'_> {
self.0.iter()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl<'a> IntoIterator for &'a LogRecords {
type Item = LogRecordRef<'a>;
type IntoIter = LogRecordsIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl IntoIterator for LogRecords {
type Item = LogRecord;
type IntoIter = LogRecordsIntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
#[cfg(test)]
impl LogRecords {
pub(crate) fn find(&self, key: impl AsRef<str>) -> Option<&LogValue> {
self.0.get(&Cow::Owned(key.as_ref().to_owned()))
}
}
#[cfg(test)]
impl std::ops::Index<&str> for LogRecords {
type Output = LogValue;
fn index(&self, index: &str) -> &Self::Output {
self.0
.get(&Cow::Owned(index.to_owned()))
.expect("No record found for the given key")
}
}