odradek-protocol 0.1.0

Sans-I/O implementation of the Kafka wire protocol: primitive codecs, message framing, and versioned message types
Documentation
//! `ListOffsetsResponse` — generated from `schemas/ListOffsetsResponse.json`.
//!
//! @generated by `cargo xtask codegen`. Do not edit by hand.

#![allow(
    clippy::field_reassign_with_default,
    clippy::nonminimal_bool,
    clippy::derivable_impls,
    clippy::borrow_deref_ref,
    clippy::double_parens,
    unused_parens
)]

use bytes::{Buf, BufMut};

use crate::budget::{Budget, Limits};
#[allow(unused_imports)]
use crate::error::{DecodeError, EncodeError};
use crate::wire::{self, RawTaggedField};

/// Whether `version` of this message uses the flexible (compact) encoding.
pub fn is_flexible(version: i16) -> bool {
    let _ = version;
    version >= 6
}

/// `ListOffsetsResponse`, versions 1-10.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ListOffsetsResponse {
    /// The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
    pub throttle_time_ms: i32,
    /// Each topic in the response.
    pub topics: Vec<ListOffsetsTopicResponse>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for ListOffsetsResponse {
    fn default() -> Self {
        Self {
            throttle_time_ms: 0,
            topics: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl ListOffsetsResponse {
    /// This message's api key in Kafka's registry.
    pub const API_KEY: i16 = 2;
    /// The lowest schema version this snapshot can speak.
    pub const MIN_VERSION: i16 = 1;
    /// The highest schema version this snapshot can speak.
    pub const MAX_VERSION: i16 = 10;

    /// Encode this `ListOffsetsResponse` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if version >= 2 {
            buf.put_i32(self.throttle_time_ms);
        }
        if is_flexible(version) {
            wire::put_compact_array_len(buf, Some(self.topics.len()));
        } else {
            wire::put_array_len(buf, Some(self.topics.len()))?;
        }
        for item in &self.topics {
            item.encode(buf, version)?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `ListOffsetsResponse`, bounding allocation by the default
    /// [`Limits`] derived from `buf`'s remaining length.
    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        Self::decode_with_limits(buf, version, Limits::default())
    }

    /// Decode one `ListOffsetsResponse` under `limits`.
    pub fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        let mut budget = limits.budget(buf.remaining());
        Self::decode_with_budget(buf, version, &mut budget)
    }

    /// Decode one `ListOffsetsResponse` against an existing `budget`.
    pub fn decode_with_budget(
        buf: &mut impl Buf,
        version: i16,
        budget: &mut Budget,
    ) -> Result<Self, DecodeError> {
        let mut this = Self::default();
        if version >= 2 {
            this.throttle_time_ms = wire::get_i32(buf)?;
        }
        this.topics = {
            let len = if is_flexible(version) {
                wire::get_compact_array_len(buf)?
            } else {
                wire::get_array_len(buf)?
            };
            match len {
                None => return Err(DecodeError::InvalidLength(-1)),
                Some(n) => {
                    let mut items = Vec::new();
                    for _ in 0..n {
                        let mark = buf.remaining();
                        let item =
                            ListOffsetsTopicResponse::decode_with_budget(buf, version, budget)?;
                        budget.progress(mark, buf.remaining())?;
                        budget.push(&mut items, item)?;
                    }
                    items
                }
            }
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

impl crate::Message for ListOffsetsResponse {
    const API_KEY: i16 = 2;
    const MIN_VERSION: i16 = 1;
    const MAX_VERSION: i16 = 10;

    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        ListOffsetsResponse::encode(self, buf, version)
    }
    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        ListOffsetsResponse::decode(buf, version)
    }
    fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        ListOffsetsResponse::decode_with_limits(buf, version, limits)
    }
    fn throttle_time_ms(&self) -> Option<i32> {
        Some(self.throttle_time_ms)
    }
}

/// `ListOffsetsTopicResponse` (nested in [`ListOffsetsResponse`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ListOffsetsTopicResponse {
    /// The topic name.
    pub name: String,
    /// Each partition in the response.
    pub partitions: Vec<ListOffsetsPartitionResponse>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for ListOffsetsTopicResponse {
    fn default() -> Self {
        Self {
            name: String::new(),
            partitions: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl ListOffsetsTopicResponse {
    /// Encode this `ListOffsetsTopicResponse` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if is_flexible(version) {
            wire::put_compact_string(buf, &self.name);
        } else {
            wire::put_string(buf, &self.name)?;
        }
        if is_flexible(version) {
            wire::put_compact_array_len(buf, Some(self.partitions.len()));
        } else {
            wire::put_array_len(buf, Some(self.partitions.len()))?;
        }
        for item in &self.partitions {
            item.encode(buf, version)?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `ListOffsetsTopicResponse`, bounding allocation by the default
    /// [`Limits`] derived from `buf`'s remaining length.
    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        Self::decode_with_limits(buf, version, Limits::default())
    }

    /// Decode one `ListOffsetsTopicResponse` under `limits`.
    pub fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        let mut budget = limits.budget(buf.remaining());
        Self::decode_with_budget(buf, version, &mut budget)
    }

    /// Decode one `ListOffsetsTopicResponse` against an existing `budget`.
    pub fn decode_with_budget(
        buf: &mut impl Buf,
        version: i16,
        budget: &mut Budget,
    ) -> Result<Self, DecodeError> {
        let mut this = Self::default();
        this.name = if is_flexible(version) {
            wire::get_compact_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        this.partitions = {
            let len = if is_flexible(version) {
                wire::get_compact_array_len(buf)?
            } else {
                wire::get_array_len(buf)?
            };
            match len {
                None => return Err(DecodeError::InvalidLength(-1)),
                Some(n) => {
                    let mut items = Vec::new();
                    for _ in 0..n {
                        let mark = buf.remaining();
                        let item =
                            ListOffsetsPartitionResponse::decode_with_budget(buf, version, budget)?;
                        budget.progress(mark, buf.remaining())?;
                        budget.push(&mut items, item)?;
                    }
                    items
                }
            }
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

/// `ListOffsetsPartitionResponse` (nested in [`ListOffsetsResponse`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ListOffsetsPartitionResponse {
    /// The partition index.
    pub partition_index: i32,
    /// The partition error code, or 0 if there was no error.
    pub error_code: i16,
    /// The timestamp associated with the returned offset.
    pub timestamp: i64,
    /// The returned offset.
    pub offset: i64,
    /// The leader epoch associated with the returned offset.
    pub leader_epoch: i32,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for ListOffsetsPartitionResponse {
    fn default() -> Self {
        Self {
            partition_index: 0,
            error_code: 0,
            timestamp: -1,
            offset: -1,
            leader_epoch: -1,
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl ListOffsetsPartitionResponse {
    /// Encode this `ListOffsetsPartitionResponse` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        buf.put_i32(self.partition_index);
        buf.put_i16(self.error_code);
        buf.put_i64(self.timestamp);
        buf.put_i64(self.offset);
        if version >= 4 {
            buf.put_i32(self.leader_epoch);
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `ListOffsetsPartitionResponse`, bounding allocation by the default
    /// [`Limits`] derived from `buf`'s remaining length.
    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        Self::decode_with_limits(buf, version, Limits::default())
    }

    /// Decode one `ListOffsetsPartitionResponse` under `limits`.
    pub fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        let mut budget = limits.budget(buf.remaining());
        Self::decode_with_budget(buf, version, &mut budget)
    }

    /// Decode one `ListOffsetsPartitionResponse` against an existing `budget`.
    pub fn decode_with_budget(
        buf: &mut impl Buf,
        version: i16,
        budget: &mut Budget,
    ) -> Result<Self, DecodeError> {
        let mut this = Self::default();
        this.partition_index = wire::get_i32(buf)?;
        this.error_code = wire::get_i16(buf)?;
        this.timestamp = wire::get_i64(buf)?;
        this.offset = wire::get_i64(buf)?;
        if version >= 4 {
            this.leader_epoch = wire::get_i32(buf)?;
        }
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}