line-protocol 0.1.0

Implementation of Influx's line protocol
Documentation
use std::fmt::Write;

/// Defines a type that can be written as a line protocol field.
pub trait LineProtocolField {
    /// Writes the key-value pair with a leading space.
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool;

    /// Writes the key-value pair with a leading comma.
    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str);
}

impl LineProtocolField for bool {
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        write!(buffer, " {}={}", key, self).unwrap();
        true
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        write!(buffer, ",{}={}", key, self).unwrap();
    }
}

impl LineProtocolField for u16 {
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        write!(buffer, " {}={}i", key, self).unwrap();
        true
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        write!(buffer, ",{}={}i", key, self).unwrap();
    }
}

impl LineProtocolField for u32 {
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        write!(buffer, " {}={}i", key, self).unwrap();
        true
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        write!(buffer, ",{}={}i", key, self).unwrap();
    }
}

impl LineProtocolField for i16 {
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        write!(buffer, " {}={}i", key, self).unwrap();
        true
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        write!(buffer, ",{}={}i", key, self).unwrap();
    }
}

impl LineProtocolField for i32 {
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        write!(buffer, " {}={}i", key, self).unwrap();
        true
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        write!(buffer, ",{}={}i", key, self).unwrap();
    }
}

impl LineProtocolField for i64 {
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        write!(buffer, " {}={}i", key, self).unwrap();
        true
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        write!(buffer, ",{}={}i", key, self).unwrap();
    }
}

impl LineProtocolField for f32 {
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        write!(buffer, " {}={}", key, self).unwrap();
        true
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        write!(buffer, ",{}={}", key, self).unwrap();
    }
}

impl LineProtocolField for f64 {
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        write!(buffer, " {}={}", key, self).unwrap();
        true
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        write!(buffer, ",{}={}", key, self).unwrap();
    }
}

impl LineProtocolField for &str {
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        write!(buffer, " {}={:?}", key, self).unwrap();
        true
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        write!(buffer, ",{}={:?}", key, self).unwrap();
    }
}

impl LineProtocolField for str {
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        (&self).write_key_value_with_space(buffer, key)
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        (&self).write_key_value_with_comma(buffer, key)
    }
}

impl LineProtocolField for String {
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        self.as_str().write_key_value_with_space(buffer, key)
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        self.as_str().write_key_value_with_comma(buffer, key)
    }
}

impl<V> LineProtocolField for Option<V>
where
    V: LineProtocolField,
{
    fn write_key_value_with_space(&self, buffer: &mut String, key: &str) -> bool {
        if let Some(value) = self {
            value.write_key_value_with_space(buffer, key)
        } else {
            false
        }
    }

    fn write_key_value_with_comma(&self, buffer: &mut String, key: &str) {
        if let Some(value) = self {
            value.write_key_value_with_comma(buffer, key)
        }
    }
}