odradek-protocol 0.1.0

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

/// `JoinGroupResponse`, versions 0-9.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct JoinGroupResponse {
    /// 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,
    /// The error code, or 0 if there was no error.
    pub error_code: i16,
    /// The generation ID of the group.
    pub generation_id: i32,
    /// The group protocol name.
    pub protocol_type: Option<String>,
    /// The group protocol selected by the coordinator.
    pub protocol_name: Option<String>,
    /// The leader of the group.
    pub leader: String,
    /// True if the leader must skip running the assignment.
    pub skip_assignment: bool,
    /// The member ID assigned by the group coordinator.
    pub member_id: String,
    /// The group members.
    pub members: Vec<JoinGroupResponseMember>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for JoinGroupResponse {
    fn default() -> Self {
        Self {
            throttle_time_ms: 0,
            error_code: 0,
            generation_id: -1,
            protocol_type: None,
            protocol_name: Some(String::new()),
            leader: String::new(),
            skip_assignment: false,
            member_id: String::new(),
            members: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

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

    /// Encode this `JoinGroupResponse` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if version >= 2 {
            buf.put_i32(self.throttle_time_ms);
        }
        buf.put_i16(self.error_code);
        buf.put_i32(self.generation_id);
        if version >= 7 {
            if is_flexible(version) {
                wire::put_compact_nullable_string(buf, self.protocol_type.as_deref());
            } else {
                wire::put_nullable_string(buf, self.protocol_type.as_deref())?;
            }
        }
        if self.protocol_name.is_none() && !(version >= 7) {
            return Err(EncodeError::NullField("ProtocolName"));
        }
        if is_flexible(version) {
            wire::put_compact_nullable_string(buf, self.protocol_name.as_deref());
        } else {
            wire::put_nullable_string(buf, self.protocol_name.as_deref())?;
        }
        if is_flexible(version) {
            wire::put_compact_string(buf, &self.leader);
        } else {
            wire::put_string(buf, &self.leader)?;
        }
        if version >= 9 {
            buf.put_u8(u8::from(self.skip_assignment));
        }
        if is_flexible(version) {
            wire::put_compact_string(buf, &self.member_id);
        } else {
            wire::put_string(buf, &self.member_id)?;
        }
        if is_flexible(version) {
            wire::put_compact_array_len(buf, Some(self.members.len()));
        } else {
            wire::put_array_len(buf, Some(self.members.len()))?;
        }
        for item in &self.members {
            item.encode(buf, version)?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `JoinGroupResponse`, 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 `JoinGroupResponse` 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 `JoinGroupResponse` 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.error_code = wire::get_i16(buf)?;
        this.generation_id = wire::get_i32(buf)?;
        if version >= 7 {
            this.protocol_type = if is_flexible(version) {
                wire::get_compact_nullable_string(buf)?
            } else {
                wire::get_nullable_string(buf)?
            };
        }
        this.protocol_name = {
            let raw = if is_flexible(version) {
                wire::get_compact_nullable_string(buf)?
            } else {
                wire::get_nullable_string(buf)?
            };
            if raw.is_none() && !(version >= 7) {
                return Err(DecodeError::InvalidLength(-1));
            }
            raw
        };
        this.leader = if is_flexible(version) {
            wire::get_compact_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        if version >= 9 {
            this.skip_assignment = wire::get_bool(buf)?;
        }
        this.member_id = if is_flexible(version) {
            wire::get_compact_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        this.members = {
            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 =
                            JoinGroupResponseMember::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 JoinGroupResponse {
    const API_KEY: i16 = 11;
    const MIN_VERSION: i16 = 0;
    const MAX_VERSION: i16 = 9;

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

/// `JoinGroupResponseMember` (nested in [`JoinGroupResponse`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct JoinGroupResponseMember {
    /// The group member ID.
    pub member_id: String,
    /// The unique identifier of the consumer instance provided by end user.
    pub group_instance_id: Option<String>,
    /// The group member metadata.
    pub metadata: bytes::Bytes,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for JoinGroupResponseMember {
    fn default() -> Self {
        Self {
            member_id: String::new(),
            group_instance_id: None,
            metadata: bytes::Bytes::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl JoinGroupResponseMember {
    /// Encode this `JoinGroupResponseMember` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if is_flexible(version) {
            wire::put_compact_string(buf, &self.member_id);
        } else {
            wire::put_string(buf, &self.member_id)?;
        }
        if version >= 5 {
            if is_flexible(version) {
                wire::put_compact_nullable_string(buf, self.group_instance_id.as_deref());
            } else {
                wire::put_nullable_string(buf, self.group_instance_id.as_deref())?;
            }
        }
        if is_flexible(version) {
            wire::put_compact_bytes(buf, &self.metadata);
        } else {
            wire::put_bytes(buf, &self.metadata)?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `JoinGroupResponseMember`, 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 `JoinGroupResponseMember` 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 `JoinGroupResponseMember` 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.member_id = if is_flexible(version) {
            wire::get_compact_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        if version >= 5 {
            this.group_instance_id = if is_flexible(version) {
                wire::get_compact_nullable_string(buf)?
            } else {
                wire::get_nullable_string(buf)?
            };
        }
        this.metadata = {
            let raw = if is_flexible(version) {
                wire::get_compact_nullable_bytes(buf)?
            } else {
                wire::get_nullable_bytes(buf)?
            };
            match raw {
                Some(v) => v,
                None => return Err(DecodeError::InvalidLength(-1)),
            }
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}