use serde::{Deserialize, Serialize};
use std::{
borrow::Cow,
collections::HashMap,
iter::{FromIterator, Iterator},
slice::Iter,
};
#[derive(Debug, Clone, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
pub enum Value<'a> {
String(Cow<'a, str>),
Integer(i64),
Float(f64),
Boolean(bool),
}
#[derive(Debug, Clone, Deserialize, PartialEq, Serialize)]
pub struct Point<'a> {
pub measurement: String,
pub tags: HashMap<String, Value<'a>>,
pub fields: HashMap<String, Value<'a>>,
pub timestamp: Option<i64>,
}
impl<'a> Point<'a> {
pub fn new<T: Into<String>>(measurement: T) -> Point<'a> {
Point {
measurement: measurement.into(),
tags: HashMap::new(),
fields: HashMap::new(),
timestamp: None,
}
}
pub fn add_tag<T: Into<String>, F: Into<Value<'a>>>(mut self, tag: T, value: F) -> Self {
self.tags.insert(tag.into(), value.into());
self
}
pub fn add_field<T: Into<String>, F: Into<Value<'a>>>(mut self, field: T, value: F) -> Self {
self.fields.insert(field.into(), value.into());
self
}
pub fn add_timestamp(mut self, timestamp: i64) -> Self {
self.timestamp = Some(timestamp);
self
}
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Points<'a> {
pub point: Vec<Point<'a>>,
}
impl<'a> Points<'a> {
pub fn new(point: Point) -> Points {
Points { point: vec![point] }
}
pub fn push(mut self, point: Point<'a>) -> Self {
self.point.push(point);
self
}
pub fn create_new(points: Vec<Point>) -> Points {
Points { point: points }
}
}
impl<'a, 'b> IntoIterator for &'a Points<'b> {
type Item = &'a Point<'b>;
type IntoIter = Iter<'a, Point<'b>>;
fn into_iter(self) -> Iter<'a, Point<'b>> {
self.point.iter()
}
}
impl<'a> FromIterator<Point<'a>> for Points<'a> {
fn from_iter<T: IntoIterator<Item = Point<'a>>>(iter: T) -> Self {
let mut points = Vec::new();
for point in iter {
points.push(point);
}
Points { point: points }
}
}
impl<'a> Iterator for Points<'a> {
type Item = Point<'a>;
fn next(&mut self) -> Option<Point<'a>> {
self.point.pop()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Precision {
Nanoseconds,
Microseconds,
Milliseconds,
Seconds,
Minutes,
Hours,
}
impl Precision {
pub fn to_str(&self) -> &str {
match *self {
Precision::Nanoseconds => "ns",
Precision::Microseconds => "us",
Precision::Milliseconds => "ms",
Precision::Seconds => "s",
Precision::Minutes => "m",
Precision::Hours => "h",
}
}
}
#[macro_export]
macro_rules! points {
($($x:expr),+) => {
{
let mut temp_vec = Vec::new();
$(temp_vec.push($x);)*
influxdb_rs::Points{ point: temp_vec }
}
};
}
#[macro_export]
macro_rules! point {
($x:expr) => {{
Point::new($x)
}};
($x:expr, $y:expr, $z:expr) => {{
Point {
measurement: String::from($x),
tags: $y,
fields: $z,
timestamp: None,
}
}};
($x:expr, $y:expr, $z:expr, $a:expr) => {{
Point {
measurement: String::from($x),
tags: $y,
fields: $z,
timestamp: Some($a),
}
}};
}
impl<'a> From<String> for Value<'a> {
fn from(v: String) -> Self {
Self::String(Cow::Owned(v))
}
}
impl<'a> From<&'a str> for Value<'a> {
fn from(v: &'a str) -> Self {
Self::String(Cow::Borrowed(v))
}
}
impl<'a> From<i64> for Value<'a> {
fn from(v: i64) -> Self {
Self::Integer(v)
}
}
impl<'a> From<i32> for Value<'a> {
fn from(v: i32) -> Self {
Self::Integer(v.into())
}
}
impl<'a> From<i16> for Value<'a> {
fn from(v: i16) -> Self {
Self::Integer(v.into())
}
}
impl<'a> From<i8> for Value<'a> {
fn from(v: i8) -> Self {
Self::Integer(v.into())
}
}
impl<'a> From<f64> for Value<'a> {
fn from(v: f64) -> Self {
Self::Float(v)
}
}
impl<'a> From<f32> for Value<'a> {
fn from(v: f32) -> Self {
Self::Float(v.into())
}
}
impl<'a> From<bool> for Value<'a> {
fn from(v: bool) -> Self {
Self::Boolean(v)
}
}