Struct LineProtocolBuilder

Source
pub struct LineProtocolBuilder<B, S = BeforeMeasurement>
where B: BufMut,
{ /* private fields */ }
Expand description

Implements a line protocol builder.

A LineProtocolBuilder is a statically-typed InfluxDB line protocol builder. It writes one or more lines of line protocol to a bytes::BufMut.

use influxdb_line_protocol::LineProtocolBuilder;
let lp = LineProtocolBuilder::new()
    .measurement("foo")
    .tag("bar", "baz")
    .field("qux", 42.0)
    .close_line();

assert_eq!(lp.build(), b"foo,bar=baz qux=42\n");

LineProtocolBuilder never returns runtime errors. Instead, it employs a type-level state machine to guarantee that users can’t build a syntactically-malformed line protocol batch.

This builder does not check for semantic errors. In particular, it does not check for duplicate tag and field names, nor it does enforce naming restrictions on keys.

Attempts to consume the line protocol before closing a line yield compile-time errors:

let lp = LineProtocolBuilder::new()
    .measurement("foo")
    .tag("bar", "baz")

assert_eq!(lp.build(), b"foo,bar=baz qux=42\n");

and attempts to close_line the line without at least one field also yield compile-time errors:

let lp = LineProtocolBuilder::new()
    .measurement("foo")
    .tag("bar", "baz")
    .close_line();

Tags, if any, must be emitted before fields. This will fail to compile:

let lp = LineProtocolBuilder::new()
    .measurement("foo")
    .field("qux", 42.0);
    .tag("bar", "baz")
    .close_line();

and timestamps, if any, must be specified last before closing the line:

let lp = LineProtocolBuilder::new()
    .measurement("foo")
    .timestamp(1234)
    .field("qux", 42.0);
    .close_line();

(the negative examples part of the documentation is so verbose because it’s the only way to test compilation failures)

Implementations§

Source§

impl LineProtocolBuilder<Vec<u8>, BeforeMeasurement>

Source

pub fn new() -> Self

Creates a new LineProtocolBuilder with an empty buffer.

Source§

impl<B> LineProtocolBuilder<B, BeforeMeasurement>
where B: BufMut,

Source

pub fn new_with(buf: B) -> Self

Like new but appending to an existing BufMut.

Source

pub fn measurement( self, measurement: &str, ) -> LineProtocolBuilder<B, AfterMeasurement>

Provide the measurement name.

It returns a new builder whose type allows only setting tags and fields.

Source

pub fn build(self) -> B

Finish building the line protocol and return the inner buffer.

Source§

impl<B> LineProtocolBuilder<B, AfterMeasurement>
where B: BufMut,

Source

pub fn tag(self, tag_key: &str, tag_value: &str) -> Self

Add a tag (key + value).

Tag keys and tag values will be escaped according to the rules defined in [the special characters documentation].

Source

pub fn field<F>( self, field_key: &str, field_value: F, ) -> LineProtocolBuilder<B, AfterField>
where F: FieldValue,

Add a field (key + value).

Field keys will be escaped according to the rules defined in [the special characters documentation].

Field values will encoded according to the rules defined in [the data types and formats documentation].

This function is called for the first field only. It returns a new builder whose type no longer allows adding tags.

Source§

impl<B> LineProtocolBuilder<B, AfterField>
where B: BufMut,

Source

pub fn field<F: FieldValue>(self, field_key: &str, field_value: F) -> Self

Add a field (key + value).

This function is called for the second and subsequent fields.

Source

pub fn timestamp(self, ts: i64) -> LineProtocolBuilder<B, AfterTimestamp>

Provide a timestamp.

It returns a builder whose type allows only closing the line.

The precision of the timestamp is by default nanoseconds (ns) but the unit can be changed when performing the request that carries the line protocol body. Setting the unit is outside of the scope of a line protocol builder.

Source

pub fn close_line(self) -> LineProtocolBuilder<B, BeforeMeasurement>

Closing a line is required before starting a new one or finishing building the batch.

Source§

impl<B> LineProtocolBuilder<B, AfterTimestamp>
where B: BufMut,

Source

pub fn close_line(self) -> LineProtocolBuilder<B, BeforeMeasurement>

Closing a line is required before starting a new one or finishing building the batch.

Trait Implementations§

Source§

impl<B, S: Debug> Debug for LineProtocolBuilder<B, S>
where B: BufMut + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<B, S: Default> Default for LineProtocolBuilder<B, S>
where B: BufMut + Default,

Source§

fn default() -> LineProtocolBuilder<B, S>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<B, S> Freeze for LineProtocolBuilder<B, S>
where B: Freeze,

§

impl<B, S> RefUnwindSafe for LineProtocolBuilder<B, S>

§

impl<B, S> Send for LineProtocolBuilder<B, S>
where B: Send, S: Send,

§

impl<B, S> Sync for LineProtocolBuilder<B, S>
where B: Sync, S: Sync,

§

impl<B, S> Unpin for LineProtocolBuilder<B, S>
where B: Unpin, S: Unpin,

§

impl<B, S> UnwindSafe for LineProtocolBuilder<B, S>
where B: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.