rinfluxdb_lineprotocol/
tag_value.rs

1// Copyright Claudio Mattera 2021.
2// Distributed under the MIT License or Apache 2.0 License at your option.
3// See accompanying files License-MIT.txt and License-Apache-2.0, or online at
4// https://opensource.org/licenses/MIT
5// https://opensource.org/licenses/Apache-2.0
6
7/// Represent a tag value
8#[derive(Clone, Debug, PartialEq, Eq, Hash)]
9pub struct TagValue(String);
10
11impl TagValue {
12    /// Escape a tag value to [InfluxDB line protocol](https://docs.influxdata.com/influxdb/v1.8/write_protocols/line_protocol_reference/)
13    ///
14    /// Characters ` `, `,` and `\` are escaped.
15    pub fn escape_to_line_protocol(&self) -> String {
16        self.0
17            .replace(" ", "\\ ")
18            .replace(",", "\\,")
19            .replace("=", "\\=")
20    }
21}
22
23impl From<&str> for TagValue {
24    fn from(s: &str) -> Self {
25        Self(s.to_string())
26    }
27}
28
29impl From<String> for TagValue {
30    fn from(s: String) -> Self {
31        Self(s)
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use quickcheck::{Arbitrary, Gen};
39
40    impl Arbitrary for TagValue {
41        fn arbitrary(g: &mut Gen) -> Self {
42            let value = String::arbitrary(g);
43            TagValue(value)
44        }
45    }
46}