1use std::fmt::Write;
2
3use crate::field::LineProtocolField;
4use crate::tag::LineProtocolTag;
5use crate::timestamp::LineProtocolTimestamp;
6
7#[derive(Default)]
9pub struct LineProtocolBuilder(String);
10
11impl LineProtocolBuilder {
12 pub fn measurement(mut self, measurement: &str) -> LineProtocolBuilderTags {
13 write!(&mut self.0, "{}", measurement).unwrap();
14 LineProtocolBuilderTags(self.0)
15 }
16}
17
18pub struct LineProtocolBuilderTags(String);
20
21impl LineProtocolBuilderTags {
22 pub fn tag<T>(mut self, key: &str, value: &T) -> LineProtocolBuilderTags
23 where
24 T: LineProtocolTag + ?Sized,
25 {
26 value.write_key_value_with_comma(&mut self.0, key);
27 LineProtocolBuilderTags(self.0)
28 }
29
30 pub fn field<F>(mut self, key: &str, value: &F) -> LineProtocolBuilderFields
31 where
32 F: LineProtocolField + ?Sized,
33 {
34 let inserted = value.write_key_value_with_space(&mut self.0, key);
35 LineProtocolBuilderFields(self.0, inserted)
36 }
37}
38
39pub struct LineProtocolBuilderFields(String, bool);
41
42impl LineProtocolBuilderFields {
43 pub fn field<F>(mut self, key: &str, value: &F) -> LineProtocolBuilderFields
44 where
45 F: LineProtocolField + ?Sized,
46 {
47 let mut inserted = self.1;
48
49 if !self.1 {
50 inserted = value.write_key_value_with_space(&mut self.0, key);
51 } else {
52 value.write_key_value_with_comma(&mut self.0, key);
53 }
54
55 LineProtocolBuilderFields(self.0, inserted)
56 }
57
58 pub fn timestamp<T>(mut self, value: &T) -> LineProtocolBuilderTimestamp
59 where
60 T: LineProtocolTimestamp,
61 {
62 value.write_value_with_space(&mut self.0);
63 LineProtocolBuilderTimestamp(self.0, self.1)
64 }
65
66 pub fn build(self) -> Option<String> {
67 if self.1 { Some(self.0) } else { None }
68 }
69}
70
71pub struct LineProtocolBuilderTimestamp(String, bool);
73
74impl LineProtocolBuilderTimestamp {
75 pub fn build(self) -> Option<String> {
76 if self.1 { Some(self.0) } else { None }
77 }
78}