Skip to main content

line_protocol/
tag.rs

1use std::fmt::Write;
2
3/// Defines a type that can be written as a line protocol tag.
4pub trait LineProtocolTag {
5    /// Writes the key-value pair with a leading comma.
6    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str);
7}
8
9/// If your string contains spaces, it will be double-quoted.
10impl LineProtocolTag for &str {
11    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
12        if self.contains(char::is_whitespace) {
13            write!(buffer, ",{}={:?}", key, self).unwrap();
14        } else {
15            write!(buffer, ",{}={}", key, self).unwrap();
16        }
17    }
18}
19
20/// If your string contains spaces, it will be double-quoted.
21impl LineProtocolTag for str {
22    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
23        (&self).write_key_value_with_comma(buffer, key);
24    }
25}
26
27/// If your string contains spaces, it will be double-quoted.
28impl LineProtocolTag for String {
29    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
30        self.as_str().write_key_value_with_comma(buffer, key);
31    }
32}
33
34impl<V> LineProtocolTag for Option<V>
35where
36    V: LineProtocolTag,
37{
38    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
39        if let Some(value) = self {
40            value.write_key_value_with_comma(buffer, key)
41        }
42    }
43}