rsiot/components_config/influxdb_v2/
line_protocol_item.rs1use crate::message::*;
2
3use super::ValueType;
4
5#[derive(Debug)]
7pub struct LineProtocolItem {
8 pub measurement: String,
10
11 pub value: ValueType,
13
14 pub ts: Timestamp,
16}
17
18impl LineProtocolItem {
19 pub fn new(measurement: &str, value: ValueType, ts: &Timestamp) -> Self {
21 Self {
22 measurement: measurement.into(),
23 value,
24 ts: ts.clone(),
25 }
26 }
27}
28
29impl TryFrom<LineProtocolItem> for String {
30 type Error = super::Error;
31
32 fn try_from(line_protocol_item: LineProtocolItem) -> Result<Self, Self::Error> {
33 (&line_protocol_item).try_into()
34 }
35}
36
37impl TryFrom<&LineProtocolItem> for String {
38 type Error = super::Error;
39
40 fn try_from(line_protocol_item: &LineProtocolItem) -> Result<Self, Self::Error> {
41 let measurement = &line_protocol_item.measurement;
42 let value = match line_protocol_item.value {
43 ValueType::bool(value) => value.to_string(),
44 ValueType::f64(value) => value.to_string(),
45 ValueType::f32(value) => value.to_string(),
46 ValueType::i8(value) => format!("{}i", value),
47 ValueType::i16(value) => format!("{}i", value),
48 ValueType::i32(value) => format!("{}i", value),
49 ValueType::u8(value) => format!("{}u", value),
50 ValueType::u16(value) => format!("{}u", value),
51 ValueType::u32(value) => format!("{}u", value),
52 };
53 let ts = line_protocol_item
54 .ts
55 .timestamp_nanos_opt()
56 .ok_or(super::Error::WrongTimestamp(line_protocol_item.ts.clone()))?;
57 let line = format!("{measurement} value={value} {ts}");
58 Ok(line)
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
68 fn test1() {
69 let value = ValueType::bool(false);
70 let ts = Timestamp::default();
71
72 let lpi = LineProtocolItem {
73 measurement: "measurement".to_string(),
74 value,
75 ts,
76 };
77
78 let ans: String = lpi.try_into().unwrap();
79
80 println!("{}", ans);
81 }
82}