codecs/encoding/format/
native.rs

1use bytes::BytesMut;
2use prost::Message;
3use serde::{Deserialize, Serialize};
4use tokio_util::codec::Encoder;
5use vector_core::{
6    config::DataType,
7    event::{proto, Event, EventArray},
8    schema,
9};
10
11/// Config used to build a `NativeSerializer`.
12#[derive(Debug, Clone, Default, Deserialize, Serialize)]
13pub struct NativeSerializerConfig;
14
15impl NativeSerializerConfig {
16    /// Build the `NativeSerializer` from this configuration.
17    pub const fn build(&self) -> NativeSerializer {
18        NativeSerializer
19    }
20
21    /// The data type of events that are accepted by `NativeSerializer`.
22    pub fn input_type(&self) -> DataType {
23        DataType::all_bits()
24    }
25
26    /// The schema required by the serializer.
27    pub fn schema_requirement(&self) -> schema::Requirement {
28        schema::Requirement::empty()
29    }
30}
31
32/// Serializer that converts an `Event` to bytes using the Vector native protobuf format.
33#[derive(Debug, Clone)]
34pub struct NativeSerializer;
35
36impl Encoder<Event> for NativeSerializer {
37    type Error = vector_common::Error;
38
39    fn encode(&mut self, event: Event, buffer: &mut BytesMut) -> Result<(), Self::Error> {
40        let array = EventArray::from(event);
41        let proto = proto::EventArray::from(array);
42        proto.encode(buffer)?;
43        Ok(())
44    }
45}