Skip to main content

LineProtocol

Derive Macro LineProtocol 

Source
#[derive(LineProtocol)]
{
    // Attributes available to this derive:
    #[line_protocol]
}
Expand description

Derive macro for LineProtocol.

§Named Struct

This macro supports named structs.

  • #[line_protocol(measurement = "...")] must be defined once on the type.
  • #[line_protocol(tag)] may be used to define tags.
  • #[line_protocol(field)] must be used at least once to define a field.
  • #[line_protocol(timestamp)] may be used once to define the timestamp.
  • #[line_protocol(rename = "...")] to rename the tags/fields.

§Example

use std::time::SystemTime;
use line_protocol::LineProtocol;

#[derive(LineProtocol)]
#[line_protocol(measurement = "power")]
struct PowerMeasurement {
    #[line_protocol(tag)]
    room: String,
    #[line_protocol(tag)]
    outlet: String,
    #[line_protocol(field)]
    current: f32,
    #[line_protocol(field)]
    voltage: f32,
    #[line_protocol(timestamp)]
    timestamp: SystemTime,
}

§Unnamed Struct

This macro supports unnamed structs, although it is not recommended.

  • It works almost the same way as for named structs.
  • #[line_protocol(rename = "...")] must be used on all tags/fields.

§Example

use std::time::SystemTime;
use line_protocol::LineProtocol;

#[derive(LineProtocol)]
#[line_protocol(measurement = "temperature")]
struct TemperatureMeasurement(
    #[line_protocol(tag, rename = "room")]
    String,
    #[line_protocol(field, rename = "temperature")]
    f32,
    #[line_protocol(timestamp)]
    SystemTime,
);

§Notes

If you’re using a String or a &str as a tag, it will get double-quoted if they string contains spaces.