line-protocol 0.1.0

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

#[cfg(feature = "chrono")]
use chrono::{DateTime, TimeZone};

/// Defines a type that can be written as a line protocol timestamp.
pub trait LineProtocolTimestamp {
    /// Writes the value pair with a leading space.
    fn write_value_with_space(&self, buffer: &mut String);
}

impl LineProtocolTimestamp for SystemTime {
    fn write_value_with_space(&self, buffer: &mut String) {
        let timestamp = self.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_nanos();
        write!(buffer, " {}", timestamp).unwrap();
    }
}

#[cfg(feature = "chrono")]
impl<Tz> LineProtocolTimestamp for DateTime<Tz>
where
    Tz: TimeZone,
{
    fn write_value_with_space(&self, buffer: &mut String) {
        let timestamp = self.timestamp_nanos_opt().unwrap();
        write!(buffer, " {}", timestamp).unwrap();
    }
}