Skip to main content

odradek_protocol/messages/
request_header.rs

1//! `RequestHeader` — generated from `schemas/RequestHeader.json`.
2//!
3//! @generated by `cargo xtask codegen`. Do not edit by hand.
4
5#![allow(
6    clippy::field_reassign_with_default,
7    clippy::nonminimal_bool,
8    clippy::derivable_impls,
9    clippy::borrow_deref_ref,
10    clippy::double_parens,
11    unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21/// Whether `version` of this message uses the flexible (compact) encoding.
22pub fn is_flexible(version: i16) -> bool {
23    let _ = version;
24    version >= 2
25}
26
27/// `RequestHeader`, versions 1-2.
28#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct RequestHeader {
31    /// The API key of this request.
32    pub request_api_key: i16,
33    /// The API version of this request.
34    pub request_api_version: i16,
35    /// The correlation ID of this request.
36    pub correlation_id: i32,
37    /// The client ID string.
38    pub client_id: Option<String>,
39    /// Tagged fields not (yet) known to this crate, kept raw.
40    pub unknown_tagged_fields: Vec<RawTaggedField>,
41}
42
43impl Default for RequestHeader {
44    fn default() -> Self {
45        Self {
46            request_api_key: 0,
47            request_api_version: 0,
48            correlation_id: 0,
49            client_id: Some(String::new()),
50            unknown_tagged_fields: Vec::new(),
51        }
52    }
53}
54
55impl RequestHeader {
56    /// The lowest schema version this snapshot can speak.
57    pub const MIN_VERSION: i16 = 1;
58    /// The highest schema version this snapshot can speak.
59    pub const MAX_VERSION: i16 = 2;
60
61    /// Encode this `RequestHeader` at `version`.
62    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
63        buf.put_i16(self.request_api_key);
64        buf.put_i16(self.request_api_version);
65        buf.put_i32(self.correlation_id);
66        wire::put_nullable_string(buf, self.client_id.as_deref())?;
67        if is_flexible(version) {
68            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
69        }
70        Ok(())
71    }
72
73    /// Decode one `RequestHeader`, bounding allocation by the default
74    /// [`Limits`] derived from `buf`'s remaining length.
75    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
76        Self::decode_with_limits(buf, version, Limits::default())
77    }
78
79    /// Decode one `RequestHeader` under `limits`.
80    pub fn decode_with_limits(
81        buf: &mut impl Buf,
82        version: i16,
83        limits: Limits,
84    ) -> Result<Self, DecodeError> {
85        let mut budget = limits.budget(buf.remaining());
86        Self::decode_with_budget(buf, version, &mut budget)
87    }
88
89    /// Decode one `RequestHeader` against an existing `budget`.
90    pub fn decode_with_budget(
91        buf: &mut impl Buf,
92        version: i16,
93        budget: &mut Budget,
94    ) -> Result<Self, DecodeError> {
95        let mut this = Self::default();
96        this.request_api_key = wire::get_i16(buf)?;
97        this.request_api_version = wire::get_i16(buf)?;
98        this.correlation_id = wire::get_i32(buf)?;
99        this.client_id = wire::get_nullable_string(buf)?;
100        if is_flexible(version) {
101            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
102        }
103        Ok(this)
104    }
105}