rinfluxdb-lineprotocol 0.2.0

A library for querying and posting data to InfluxDB
Documentation
// Copyright Claudio Mattera 2021.
// Distributed under the MIT License or Apache 2.0 License at your option.
// See accompanying files License-MIT.txt and License-Apache-2.0, or online at
// https://opensource.org/licenses/MIT
// https://opensource.org/licenses/Apache-2.0

/// Represent a tag value
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TagValue(String);

impl TagValue {
    /// Escape a tag value to [InfluxDB line protocol](https://docs.influxdata.com/influxdb/v1.8/write_protocols/line_protocol_reference/)
    ///
    /// Characters ` `, `,` and `\` are escaped.
    pub fn escape_to_line_protocol(&self) -> String {
        self.0
            .replace(" ", "\\ ")
            .replace(",", "\\,")
            .replace("=", "\\=")
    }
}

impl From<&str> for TagValue {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

impl From<String> for TagValue {
    fn from(s: String) -> Self {
        Self(s)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use quickcheck::{Arbitrary, Gen};

    impl Arbitrary for TagValue {
        fn arbitrary(g: &mut Gen) -> Self {
            let value = String::arbitrary(g);
            TagValue(value)
        }
    }
}