odradek-protocol 0.1.0

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

/// `MetadataRequest`, versions 0-13.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct MetadataRequest {
    /// The topics to fetch metadata for.
    pub topics: Option<Vec<MetadataRequestTopic>>,
    /// If this is true, the broker may auto-create topics that we requested which do not already exist, if it is configured to do so.
    pub allow_auto_topic_creation: bool,
    /// Whether to include cluster authorized operations.
    pub include_cluster_authorized_operations: bool,
    /// Whether to include topic authorized operations.
    pub include_topic_authorized_operations: bool,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for MetadataRequest {
    fn default() -> Self {
        Self {
            topics: Some(Vec::new()),
            allow_auto_topic_creation: true,
            include_cluster_authorized_operations: false,
            include_topic_authorized_operations: false,
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl MetadataRequest {
    /// This message's api key in Kafka's registry.
    pub const API_KEY: i16 = 3;
    /// 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 = 13;

    /// Encode this `MetadataRequest` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        match &self.topics {
            Some(items) => {
                if is_flexible(version) {
                    wire::put_compact_array_len(buf, Some(items.len()));
                } else {
                    wire::put_array_len(buf, Some(items.len()))?;
                }
                for item in items {
                    item.encode(buf, version)?;
                }
            }
            None => {
                if !(version >= 1) {
                    return Err(EncodeError::NullField("Topics"));
                }
                if is_flexible(version) {
                    wire::put_compact_array_len(buf, None);
                } else {
                    wire::put_array_len(buf, None)?;
                }
            }
        }
        if version >= 4 {
            buf.put_u8(u8::from(self.allow_auto_topic_creation));
        }
        if (8..=10).contains(&version) {
            buf.put_u8(u8::from(self.include_cluster_authorized_operations));
        }
        if version >= 8 {
            buf.put_u8(u8::from(self.include_topic_authorized_operations));
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `MetadataRequest`, 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 `MetadataRequest` 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 `MetadataRequest` 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.topics = {
            let len = if is_flexible(version) {
                wire::get_compact_array_len(buf)?
            } else {
                wire::get_array_len(buf)?
            };
            match len {
                None => {
                    if !(version >= 1) {
                        return Err(DecodeError::InvalidLength(-1));
                    }
                    None
                }
                Some(n) => {
                    let mut items = Vec::new();
                    for _ in 0..n {
                        let mark = buf.remaining();
                        let item = MetadataRequestTopic::decode_with_budget(buf, version, budget)?;
                        budget.progress(mark, buf.remaining())?;
                        budget.push(&mut items, item)?;
                    }
                    Some(items)
                }
            }
        };
        if version >= 4 {
            this.allow_auto_topic_creation = wire::get_bool(buf)?;
        }
        if (8..=10).contains(&version) {
            this.include_cluster_authorized_operations = wire::get_bool(buf)?;
        }
        if version >= 8 {
            this.include_topic_authorized_operations = wire::get_bool(buf)?;
        }
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

impl crate::Message for MetadataRequest {
    const API_KEY: i16 = 3;
    const MIN_VERSION: i16 = 0;
    const MAX_VERSION: i16 = 13;

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

/// `MetadataRequestTopic` (nested in [`MetadataRequest`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct MetadataRequestTopic {
    /// The topic id.
    pub topic_id: [u8; 16],
    /// The topic name.
    pub name: Option<String>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for MetadataRequestTopic {
    fn default() -> Self {
        Self {
            topic_id: [0u8; 16],
            name: Some(String::new()),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl MetadataRequestTopic {
    /// Encode this `MetadataRequestTopic` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if version >= 10 {
            wire::put_uuid(buf, self.topic_id);
        }
        if self.name.is_none() && !(version >= 10) {
            return Err(EncodeError::NullField("Name"));
        }
        if is_flexible(version) {
            wire::put_compact_nullable_string(buf, self.name.as_deref());
        } else {
            wire::put_nullable_string(buf, self.name.as_deref())?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `MetadataRequestTopic`, 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 `MetadataRequestTopic` 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 `MetadataRequestTopic` 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 >= 10 {
            this.topic_id = wire::get_uuid(buf)?;
        }
        this.name = {
            let raw = if is_flexible(version) {
                wire::get_compact_nullable_string(buf)?
            } else {
                wire::get_nullable_string(buf)?
            };
            if raw.is_none() && !(version >= 10) {
                return Err(DecodeError::InvalidLength(-1));
            }
            raw
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}