crabka-protocol 0.2.0

Apache Kafka wire-protocol codec (4.3.0), with typed RecordBatch and zero-copy borrowed decode
Documentation
// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.

use bytes::{Buf, BufMut};

use crate::primitives::fixed::{
    get_bool, get_i8, get_i16, get_i32, put_bool, put_i8, put_i16, put_i32,
};
use crate::primitives::string_bytes::{
    compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
    string_len,
};
use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};

pub const API_KEY: i16 = 71;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 0;
pub const FLEXIBLE_MIN: i16 = 0;

#[inline]
fn is_flexible(version: i16) -> bool {
    version >= FLEXIBLE_MIN
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct GetTelemetrySubscriptionsResponse {
    pub throttle_time_ms: i32,
    pub error_code: i16,
    pub client_instance_id: crate::primitives::uuid::Uuid,
    pub subscription_id: i32,
    pub accepted_compression_types: Vec<i8>,
    pub push_interval_ms: i32,
    pub telemetry_max_bytes: i32,
    pub delta_temporality: bool,
    pub requested_metrics: Vec<String>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for GetTelemetrySubscriptionsResponse {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::UnsupportedVersion {
                api_key: API_KEY,
                version,
            });
        }
        let flex = is_flexible(version);
        if version >= 0 {
            put_i32(buf, self.throttle_time_ms);
        }
        if version >= 0 {
            put_i16(buf, self.error_code);
        }
        if version >= 0 {
            crate::primitives::uuid::put_uuid(buf, self.client_instance_id);
        }
        if version >= 0 {
            put_i32(buf, self.subscription_id);
        }
        if version >= 0 {
            {
                crate::primitives::array::put_array_len(
                    buf,
                    (self.accepted_compression_types).len(),
                    flex,
                );
                for it in &self.accepted_compression_types {
                    put_i8(buf, *it);
                }
            }
        }
        if version >= 0 {
            put_i32(buf, self.push_interval_ms);
        }
        if version >= 0 {
            put_i32(buf, self.telemetry_max_bytes);
        }
        if version >= 0 {
            put_bool(buf, self.delta_temporality);
        }
        if version >= 0 {
            {
                crate::primitives::array::put_array_len(buf, (self.requested_metrics).len(), flex);
                for it in &self.requested_metrics {
                    if flex {
                        put_compact_string(buf, it);
                    } else {
                        put_string(buf, it);
                    }
                }
            }
        }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = is_flexible(version);
        let mut n: usize = 0;
        if version >= 0 {
            n += 4;
        }
        if version >= 0 {
            n += 2;
        }
        if version >= 0 {
            n += 16;
        }
        if version >= 0 {
            n += 4;
        }
        if version >= 0 {
            n += {
                let prefix = crate::primitives::array::array_len_prefix_len(
                    (self.accepted_compression_types).len(),
                    flex,
                );
                let body: usize = (self.accepted_compression_types).iter().map(|_| 1).sum();
                prefix + body
            };
        }
        if version >= 0 {
            n += 4;
        }
        if version >= 0 {
            n += 4;
        }
        if version >= 0 {
            n += 1;
        }
        if version >= 0 {
            n += {
                let prefix = crate::primitives::array::array_len_prefix_len(
                    (self.requested_metrics).len(),
                    flex,
                );
                let body: usize = (self.requested_metrics)
                    .iter()
                    .map(|it| {
                        if flex {
                            compact_string_len(it)
                        } else {
                            string_len(it)
                        }
                    })
                    .sum();
                prefix + body
            };
        }
        if flex {
            let known_pairs: Vec<(u32, usize)> = Vec::new();
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}
impl Decode<'_> for GetTelemetrySubscriptionsResponse {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::UnsupportedVersion {
                api_key: API_KEY,
                version,
            });
        }
        let flex = is_flexible(version);
        let mut out = Self::default();
        if version >= 0 {
            out.throttle_time_ms = get_i32(buf)?;
        }
        if version >= 0 {
            out.error_code = get_i16(buf)?;
        }
        if version >= 0 {
            out.client_instance_id = crate::primitives::uuid::get_uuid(buf)?;
        }
        if version >= 0 {
            out.subscription_id = get_i32(buf)?;
        }
        if version >= 0 {
            out.accepted_compression_types = {
                let n = crate::primitives::array::get_array_len(buf, flex)?;
                let mut v = Vec::with_capacity(n);
                for _ in 0..n {
                    v.push(get_i8(buf)?);
                }
                v
            };
        }
        if version >= 0 {
            out.push_interval_ms = get_i32(buf)?;
        }
        if version >= 0 {
            out.telemetry_max_bytes = get_i32(buf)?;
        }
        if version >= 0 {
            out.delta_temporality = get_bool(buf)?;
        }
        if version >= 0 {
            out.requested_metrics = {
                let n = crate::primitives::array::get_array_len(buf, flex)?;
                let mut v = Vec::with_capacity(n);
                for _ in 0..n {
                    v.push(if flex {
                        get_compact_string_owned(buf)?
                    } else {
                        get_string_owned(buf)?
                    });
                }
                v
            };
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl GetTelemetrySubscriptionsResponse {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.throttle_time_ms = 1i32;
        }
        if version >= 0 {
            m.error_code = 1i16;
        }
        if version >= 0 {
            m.client_instance_id = crate::primitives::uuid::Uuid([1u8; 16]);
        }
        if version >= 0 {
            m.subscription_id = 1i32;
        }
        if version >= 0 {
            m.accepted_compression_types = vec![1i8];
        }
        if version >= 0 {
            m.push_interval_ms = 1i32;
        }
        if version >= 0 {
            m.telemetry_max_bytes = 1i32;
        }
        if version >= 0 {
            m.delta_temporality = true;
        }
        if version >= 0 {
            m.requested_metrics = vec!["x".to_string()];
        }
        m
    }
}

/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
/// Only includes fields valid for the given version.
#[must_use]
#[allow(unused_comparisons)]
pub fn default_json(version: i16) -> ::serde_json::Value {
    let mut obj = ::serde_json::Map::new();
    obj.insert("throttleTimeMs".to_string(), ::serde_json::json!(0));
    obj.insert("errorCode".to_string(), ::serde_json::json!(0));
    obj.insert(
        "clientInstanceId".to_string(),
        ::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()),
    );
    obj.insert("subscriptionId".to_string(), ::serde_json::json!(0));
    obj.insert(
        "acceptedCompressionTypes".to_string(),
        ::serde_json::Value::Array(vec![]),
    );
    obj.insert("pushIntervalMs".to_string(), ::serde_json::json!(0));
    obj.insert("telemetryMaxBytes".to_string(), ::serde_json::json!(0));
    obj.insert(
        "deltaTemporality".to_string(),
        ::serde_json::Value::Bool(false),
    );
    obj.insert(
        "requestedMetrics".to_string(),
        ::serde_json::Value::Array(vec![]),
    );
    ::serde_json::Value::Object(obj)
}