odradek-protocol 0.1.0

Sans-I/O implementation of the Kafka wire protocol: primitive codecs, message framing, and versioned message types
Documentation
//! `FindCoordinatorRequest` — generated from `schemas/FindCoordinatorRequest.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 >= 3
}

/// `FindCoordinatorRequest`, versions 0-6.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct FindCoordinatorRequest {
    /// The coordinator key.
    pub key: String,
    /// The coordinator key type. (group, transaction, share).
    pub key_type: i8,
    /// The coordinator keys.
    pub coordinator_keys: Vec<String>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for FindCoordinatorRequest {
    fn default() -> Self {
        Self {
            key: String::new(),
            key_type: 0,
            coordinator_keys: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

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

    /// Encode this `FindCoordinatorRequest` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if version <= 3 {
            if is_flexible(version) {
                wire::put_compact_string(buf, &self.key);
            } else {
                wire::put_string(buf, &self.key)?;
            }
        }
        if version >= 1 {
            buf.put_i8(self.key_type);
        }
        if version >= 4 {
            if is_flexible(version) {
                wire::put_compact_array_len(buf, Some(self.coordinator_keys.len()));
            } else {
                wire::put_array_len(buf, Some(self.coordinator_keys.len()))?;
            }
            for item in &self.coordinator_keys {
                if is_flexible(version) {
                    wire::put_compact_string(buf, item);
                } else {
                    wire::put_string(buf, item)?;
                }
            }
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `FindCoordinatorRequest`, 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 `FindCoordinatorRequest` 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 `FindCoordinatorRequest` 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 <= 3 {
            this.key = if is_flexible(version) {
                wire::get_compact_string(buf)?
            } else {
                wire::get_string(buf)?
            };
        }
        if version >= 1 {
            this.key_type = wire::get_i8(buf)?;
        }
        if version >= 4 {
            this.coordinator_keys = {
                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 {
                            budget.push(
                                &mut items,
                                if is_flexible(version) {
                                    wire::get_compact_string(buf)?
                                } else {
                                    wire::get_string(buf)?
                                },
                            )?;
                        }
                        items
                    }
                }
            };
        }
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

impl crate::Message for FindCoordinatorRequest {
    const API_KEY: i16 = 10;
    const MIN_VERSION: i16 = 0;
    const MAX_VERSION: i16 = 6;

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