kafka_api/records/
record.rs

1// Copyright 2024 tison <wander4096@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[derive(Debug, Default, Clone)]
16pub struct Record {
17    pub len: i32, // varint
18    /// bit 0~7: unused
19    pub attributes: i8,
20    pub timestamp_delta: i64, // varlong
21    pub offset_delta: i32,    // varint
22    pub key_len: i32,         // varint
23    pub key: Option<Vec<u8>>,
24    pub value_len: i32, // varint
25    pub value: Option<Vec<u8>>,
26    pub headers: Vec<Header>,
27}
28
29#[derive(Debug, Default, Clone)]
30pub struct Header {
31    pub key_len: i32, // varint
32    pub key: Option<Vec<u8>>,
33    pub value_len: i32, // varint
34    pub value: Option<Vec<u8>>,
35}
36
37#[derive(Debug, Clone, Copy)]
38pub enum TimestampType {
39    CreateTime,
40    LogAppendTime,
41}
42
43#[derive(Debug, Default, Clone, Copy)]
44pub enum CompressionType {
45    #[default]
46    None,
47    Gzip,
48    Snappy,
49    Lz4,
50    Zstd,
51}
52
53impl From<u8> for CompressionType {
54    fn from(ty: u8) -> Self {
55        match ty {
56            0 => CompressionType::None,
57            1 => CompressionType::Gzip,
58            2 => CompressionType::Snappy,
59            3 => CompressionType::Lz4,
60            4 => CompressionType::Zstd,
61            _ => unreachable!("unknown compression type id: {}", ty),
62        }
63    }
64}