odradek-protocol 0.1.0

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

/// `SyncGroupRequest`, versions 0-5.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct SyncGroupRequest {
    /// The unique group identifier.
    pub group_id: String,
    /// The generation of the group.
    pub generation_id: i32,
    /// The member ID assigned by the group.
    pub member_id: String,
    /// The unique identifier of the consumer instance provided by end user.
    pub group_instance_id: Option<String>,
    /// The group protocol type.
    pub protocol_type: Option<String>,
    /// The group protocol name.
    pub protocol_name: Option<String>,
    /// Each assignment.
    pub assignments: Vec<SyncGroupRequestAssignment>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for SyncGroupRequest {
    fn default() -> Self {
        Self {
            group_id: String::new(),
            generation_id: 0,
            member_id: String::new(),
            group_instance_id: None,
            protocol_type: None,
            protocol_name: None,
            assignments: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

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

    /// Encode this `SyncGroupRequest` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if is_flexible(version) {
            wire::put_compact_string(buf, &self.group_id);
        } else {
            wire::put_string(buf, &self.group_id)?;
        }
        buf.put_i32(self.generation_id);
        if is_flexible(version) {
            wire::put_compact_string(buf, &self.member_id);
        } else {
            wire::put_string(buf, &self.member_id)?;
        }
        if version >= 3 {
            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 version >= 5 {
            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 version >= 5 {
            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_array_len(buf, Some(self.assignments.len()));
        } else {
            wire::put_array_len(buf, Some(self.assignments.len()))?;
        }
        for item in &self.assignments {
            item.encode(buf, version)?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `SyncGroupRequest`, 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 `SyncGroupRequest` 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 `SyncGroupRequest` 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.group_id = if is_flexible(version) {
            wire::get_compact_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        this.generation_id = wire::get_i32(buf)?;
        this.member_id = if is_flexible(version) {
            wire::get_compact_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        if version >= 3 {
            this.group_instance_id = if is_flexible(version) {
                wire::get_compact_nullable_string(buf)?
            } else {
                wire::get_nullable_string(buf)?
            };
        }
        if version >= 5 {
            this.protocol_type = if is_flexible(version) {
                wire::get_compact_nullable_string(buf)?
            } else {
                wire::get_nullable_string(buf)?
            };
        }
        if version >= 5 {
            this.protocol_name = if is_flexible(version) {
                wire::get_compact_nullable_string(buf)?
            } else {
                wire::get_nullable_string(buf)?
            };
        }
        this.assignments = {
            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 =
                            SyncGroupRequestAssignment::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 SyncGroupRequest {
    const API_KEY: i16 = 14;
    const MIN_VERSION: i16 = 0;
    const MAX_VERSION: i16 = 5;

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

/// `SyncGroupRequestAssignment` (nested in [`SyncGroupRequest`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct SyncGroupRequestAssignment {
    /// The ID of the member to assign.
    pub member_id: String,
    /// The member assignment.
    pub assignment: bytes::Bytes,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for SyncGroupRequestAssignment {
    fn default() -> Self {
        Self {
            member_id: String::new(),
            assignment: bytes::Bytes::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl SyncGroupRequestAssignment {
    /// Encode this `SyncGroupRequestAssignment` 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 is_flexible(version) {
            wire::put_compact_bytes(buf, &self.assignment);
        } else {
            wire::put_bytes(buf, &self.assignment)?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `SyncGroupRequestAssignment`, 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 `SyncGroupRequestAssignment` 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 `SyncGroupRequestAssignment` 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)?
        };
        this.assignment = {
            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)
    }
}