use crate::{Array, Key, StringValue, Value};
use std::{borrow::Cow, collections::HashMap, time::SystemTime};
pub trait LogRecord {
fn set_event_name<T>(&mut self, _name: T)
where
T: Into<Cow<'static, str>>,
{
}
fn set_timestamp(&mut self, timestamp: SystemTime);
fn set_observed_timestamp(&mut self, timestamp: SystemTime);
fn set_severity_text(&mut self, text: Cow<'static, str>);
fn set_severity_number(&mut self, number: Severity);
fn set_body(&mut self, body: AnyValue);
fn add_attributes<I, K, V>(&mut self, attributes: I)
where
I: IntoIterator<Item = (K, V)>,
K: Into<Key>,
V: Into<AnyValue>;
fn add_attribute<K, V>(&mut self, key: K, value: V)
where
K: Into<Key>,
V: Into<AnyValue>;
}
#[derive(Debug, Clone, PartialEq)]
pub enum AnyValue {
Int(i64),
Double(f64),
String(StringValue),
Boolean(bool),
Bytes(Vec<u8>),
ListAny(Vec<AnyValue>),
Map(HashMap<Key, AnyValue>),
}
macro_rules! impl_trivial_from {
($t:ty, $variant:path) => {
impl From<$t> for AnyValue {
fn from(val: $t) -> AnyValue {
$variant(val.into())
}
}
};
}
impl_trivial_from!(i8, AnyValue::Int);
impl_trivial_from!(i16, AnyValue::Int);
impl_trivial_from!(i32, AnyValue::Int);
impl_trivial_from!(i64, AnyValue::Int);
impl_trivial_from!(u8, AnyValue::Int);
impl_trivial_from!(u16, AnyValue::Int);
impl_trivial_from!(u32, AnyValue::Int);
impl_trivial_from!(f64, AnyValue::Double);
impl_trivial_from!(f32, AnyValue::Double);
impl_trivial_from!(String, AnyValue::String);
impl_trivial_from!(Cow<'static, str>, AnyValue::String);
impl_trivial_from!(&'static str, AnyValue::String);
impl_trivial_from!(StringValue, AnyValue::String);
impl_trivial_from!(bool, AnyValue::Boolean);
impl<T: Into<AnyValue>> FromIterator<T> for AnyValue {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
AnyValue::ListAny(iter.into_iter().map(Into::into).collect())
}
}
impl<K: Into<Key>, V: Into<AnyValue>> FromIterator<(K, V)> for AnyValue {
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
AnyValue::Map(HashMap::from_iter(
iter.into_iter().map(|(k, v)| (k.into(), v.into())),
))
}
}
impl From<Value> for AnyValue {
fn from(value: Value) -> Self {
match value {
Value::Bool(b) => b.into(),
Value::I64(i) => i.into(),
Value::F64(f) => f.into(),
Value::String(s) => s.into(),
Value::Array(a) => match a {
Array::Bool(b) => AnyValue::from_iter(b),
Array::F64(f) => AnyValue::from_iter(f),
Array::I64(i) => AnyValue::from_iter(i),
Array::String(s) => AnyValue::from_iter(s),
},
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub enum Severity {
Trace = 1,
Trace2 = 2,
Trace3 = 3,
Trace4 = 4,
Debug = 5,
Debug2 = 6,
Debug3 = 7,
Debug4 = 8,
Info = 9,
Info2 = 10,
Info3 = 11,
Info4 = 12,
Warn = 13,
Warn2 = 14,
Warn3 = 15,
Warn4 = 16,
Error = 17,
Error2 = 18,
Error3 = 19,
Error4 = 20,
Fatal = 21,
Fatal2 = 22,
Fatal3 = 23,
Fatal4 = 24,
}
impl Severity {
pub const fn name(&self) -> &'static str {
match &self {
Severity::Trace => "TRACE",
Severity::Trace2 => "TRACE2",
Severity::Trace3 => "TRACE3",
Severity::Trace4 => "TRACE4",
Severity::Debug => "DEBUG",
Severity::Debug2 => "DEBUG2",
Severity::Debug3 => "DEBUG3",
Severity::Debug4 => "DEBUG4",
Severity::Info => "INFO",
Severity::Info2 => "INFO2",
Severity::Info3 => "INFO3",
Severity::Info4 => "INFO4",
Severity::Warn => "WARN",
Severity::Warn2 => "WARN2",
Severity::Warn3 => "WARN3",
Severity::Warn4 => "WARN4",
Severity::Error => "ERROR",
Severity::Error2 => "ERROR2",
Severity::Error3 => "ERROR3",
Severity::Error4 => "ERROR4",
Severity::Fatal => "FATAL",
Severity::Fatal2 => "FATAL2",
Severity::Fatal3 => "FATAL3",
Severity::Fatal4 => "FATAL4",
}
}
}