#![deny(missing_docs)]
#![deny(warnings)]
#![deny(unsafe_code)]
use std::borrow::Cow;
use std::sync::Arc;
pub fn err_other<E>(error: E) -> std::io::Error
where
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
std::io::Error::other(error)
}
#[derive(Debug, Clone)]
pub enum StringType {
String(Cow<'static, str>),
ArcString(Arc<str>),
}
impl StringType {
pub fn into_string(self) -> String {
match self {
StringType::String(s) => s.into_owned(),
StringType::ArcString(s) => s.to_string(),
}
}
}
macro_rules! stringtype_from_impl {
($($f:ty, $i:ident, $b:block,)*) => {$(
impl From<$f> for StringType {
fn from($i: $f) -> Self $b
}
)*};
}
stringtype_from_impl! {
String, f, { StringType::String(Cow::Owned(f)) },
&'static String, f, { StringType::String(Cow::Borrowed(f.as_str())) },
&'static str, f, { StringType::String(Cow::Borrowed(f)) },
Cow<'static, str>, f, { StringType::String(f) },
Arc<str>, f, { StringType::ArcString(f) },
}
#[derive(Debug, Clone)]
pub enum DataType {
Bool(bool),
F64(f64),
I64(i64),
U64(u64),
String(StringType),
}
macro_rules! datatype_from_impl {
($($f:ty, $i:ident, $b:block,)*) => {$(
impl From<$f> for DataType {
fn from($i: $f) -> Self $b
}
)*};
}
datatype_from_impl! {
bool, f, { DataType::Bool(f) },
f64, f, { DataType::F64(f) },
f32, f, { DataType::F64(f as f64) },
i8, f, { DataType::I64(f as i64) },
i16, f, { DataType::I64(f as i64) },
i32, f, { DataType::I64(f as i64) },
i64, f, { DataType::I64(f as i64) },
u8, f, { DataType::U64(f as u64) },
u16, f, { DataType::U64(f as u64) },
u32, f, { DataType::U64(f as u64) },
u64, f, { DataType::U64(f as u64) },
String, f, { DataType::String(f.into()) },
&'static String, f, { DataType::String(f.into()) },
&'static str, f, { DataType::String(f.into()) },
Cow<'static, str>, f, { DataType::String(f.into()) },
Arc<str>, f, { DataType::String(f.into()) },
}
#[derive(Debug)]
pub struct Metric {
pub timestamp: std::time::SystemTime,
pub name: StringType,
pub fields: Vec<(StringType, DataType)>,
pub tags: Vec<(StringType, DataType)>,
}
impl Metric {
pub fn new<N: Into<StringType>>(
timestamp: std::time::SystemTime,
name: N,
) -> Metric {
Self {
timestamp,
name: name.into(),
fields: Vec::new(),
tags: Vec::new(),
}
}
pub fn with_field<N, V>(mut self, name: N, value: V) -> Self
where
N: Into<StringType>,
V: Into<DataType>,
{
self.fields.push((name.into(), value.into()));
self
}
pub fn with_tag<N, V>(mut self, name: N, value: V) -> Self
where
N: Into<StringType>,
V: Into<DataType>,
{
self.tags.push((name.into(), value.into()));
self
}
}
pub trait MetricWriter: 'static + Send + Sync {
fn write_metric(&self, metric: Metric);
}