use std::{collections::HashMap, fmt::Display};
use element::{FieldKey, FieldValue, Measurement, TagKey, TagValue};
pub mod builder;
pub mod element;
pub mod error;
pub mod parser;
pub mod traits;
#[derive(Debug, Clone)]
pub struct LineProtocol {
pub measurement: Measurement,
pub tags: Option<HashMap<TagKey, TagValue>>,
pub fields: HashMap<FieldKey, FieldValue>,
pub timestamp: Option<i64>,
}
impl PartialEq for LineProtocol {
fn eq(&self, other: &Self) -> bool {
if self.measurement != other.measurement {
println!("name not equal");
return false;
}
let tags_matches = match (&self.tags, &other.tags) {
(Some(tags1), Some(tags2)) => tags1 == tags2,
(None, None) => true,
_ => return false,
};
let timestamp_matches = match (self.timestamp, other.timestamp) {
(Some(ts1), Some(ts2)) => ts1 == ts2,
(None, None) => true,
_ => return false,
};
tags_matches && timestamp_matches
}
}
impl Display for LineProtocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let lp = match &self.build() {
Ok(lp) => lp.to_string(),
Err(e) => format!("invalid line protocol: {e}"),
};
write!(f, "{}", lp)
}
}
impl LineProtocol {
pub fn get_measurement(&self) -> Measurement {
self.measurement.clone()
}
pub fn get_measurement_ref(&self) -> &Measurement {
&self.measurement
}
pub fn get_measurement_mut(&mut self) -> &mut Measurement {
&mut self.measurement
}
pub fn get_tag<K>(&self, key: K) -> Option<TagValue>
where
K: Into<TagKey>,
{
match &self.tags {
Some(tags) => tags.get(&key.into()).cloned(),
None => None,
}
}
pub fn get_tag_ref<K>(&self, key: K) -> Option<&TagValue>
where
K: Into<TagKey>,
{
match &self.tags {
Some(tags) => tags.get(&key.into()),
None => None,
}
}
pub fn get_tag_mut<K>(&mut self, key: K) -> Option<&mut TagValue>
where
K: Into<TagKey>,
{
match &mut self.tags {
Some(tags) => tags.get_mut(&key.into()),
None => None,
}
}
pub fn get_field<K>(&self, key: K) -> Option<FieldValue>
where
K: Into<FieldKey>,
{
self.fields.get(&key.into()).cloned()
}
pub fn get_field_ref<K>(&self, key: K) -> Option<&FieldValue>
where
K: Into<FieldKey>,
{
self.fields.get(&key.into())
}
pub fn get_field_mut<K>(&mut self, key: K) -> Option<&mut FieldValue>
where
K: Into<FieldKey>,
{
self.fields.get_mut(&key.into())
}
pub fn get_timestamp(&self) -> Option<i64> {
self.timestamp
}
pub fn get_timestamp_ref(&self) -> Option<&i64> {
self.timestamp.as_ref()
}
pub fn get_timestamp_mut(&mut self) -> Option<&mut i64> {
self.timestamp.as_mut()
}
}