odradek-protocol 0.1.0

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

/// `RequestHeader`, versions 1-2.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct RequestHeader {
    /// The API key of this request.
    pub request_api_key: i16,
    /// The API version of this request.
    pub request_api_version: i16,
    /// The correlation ID of this request.
    pub correlation_id: i32,
    /// The client ID string.
    pub client_id: Option<String>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for RequestHeader {
    fn default() -> Self {
        Self {
            request_api_key: 0,
            request_api_version: 0,
            correlation_id: 0,
            client_id: Some(String::new()),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl RequestHeader {
    /// 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 = 2;

    /// Encode this `RequestHeader` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        buf.put_i16(self.request_api_key);
        buf.put_i16(self.request_api_version);
        buf.put_i32(self.correlation_id);
        wire::put_nullable_string(buf, self.client_id.as_deref())?;
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `RequestHeader`, 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 `RequestHeader` 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 `RequestHeader` 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.request_api_key = wire::get_i16(buf)?;
        this.request_api_version = wire::get_i16(buf)?;
        this.correlation_id = wire::get_i32(buf)?;
        this.client_id = wire::get_nullable_string(buf)?;
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}