crabka-protocol 0.3.2

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, put_bool};
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 = 89;
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 StreamsGroupDescribeRequest {
    pub group_ids: Vec<String>,
    pub include_authorized_operations: bool,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for StreamsGroupDescribeRequest {
    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 {
            {
                crate::primitives::array::put_array_len(buf, (self.group_ids).len(), flex);
                for it in &self.group_ids {
                    if flex {
                        put_compact_string(buf, it);
                    } else {
                        put_string(buf, it);
                    }
                }
            }
        }
        if version >= 0 {
            put_bool(buf, self.include_authorized_operations);
        }
        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 += {
                let prefix =
                    crate::primitives::array::array_len_prefix_len((self.group_ids).len(), flex);
                let body: usize = (self.group_ids)
                    .iter()
                    .map(|it| {
                        if flex {
                            compact_string_len(it)
                        } else {
                            string_len(it)
                        }
                    })
                    .sum();
                prefix + body
            };
        }
        if version >= 0 {
            n += 1;
        }
        if flex {
            let known_pairs: Vec<(u32, usize)> = Vec::new();
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}
impl Decode<'_> for StreamsGroupDescribeRequest {
    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.group_ids = {
                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 version >= 0 {
            out.include_authorized_operations = get_bool(buf)?;
        }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
        }
        Ok(out)
    }
}
#[cfg(test)]
impl StreamsGroupDescribeRequest {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.group_ids = vec!["x".to_string()];
        }
        if version >= 0 {
            m.include_authorized_operations = true;
        }
        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("groupIds".to_string(), ::serde_json::Value::Array(vec![]));
    obj.insert(
        "includeAuthorizedOperations".to_string(),
        ::serde_json::Value::Bool(false),
    );
    ::serde_json::Value::Object(obj)
}

impl crate::ProtocolRequest for StreamsGroupDescribeRequest {
    const API_KEY: i16 = API_KEY;
    const MIN_VERSION: i16 = MIN_VERSION;
    const MAX_VERSION: i16 = MAX_VERSION;
    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
    type Response = super::streams_group_describe_response::StreamsGroupDescribeResponse;
}