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 crate::primitives::string_bytes::{
    compact_nullable_string_len, get_compact_nullable_string_owned, get_nullable_string_owned,
    nullable_string_len, put_compact_nullable_string, put_nullable_string,
};
use crate::tagged_fields::{
    WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len,
};
use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
use bytes::{Buf, BufMut};
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 BeginTransactionRecord {
    pub name: Option<String>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for BeginTransactionRecord {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::SchemaMismatch(
                "BeginTransactionRecord version out of range",
            ));
        }
        let flex = is_flexible(version);
        if flex {
            let mut tagged = WriteTaggedFields::new();
            if !(self.name.is_none()) {
                let payload = encode_to_bytes(
                    if flex {
                        compact_nullable_string_len(self.name.as_deref())
                    } else {
                        nullable_string_len(self.name.as_deref())
                    },
                    |b| {
                        if flex {
                            put_compact_nullable_string(b, self.name.as_deref());
                        } else {
                            put_nullable_string(b, self.name.as_deref());
                        }
                        Ok(())
                    },
                );
                tagged.add(0, payload);
            }
            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 flex {
            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
            if !(self.name.is_none()) {
                known_pairs.push((
                    0,
                    if flex {
                        compact_nullable_string_len(self.name.as_deref())
                    } else {
                        nullable_string_len(self.name.as_deref())
                    },
                ));
            }
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}
impl Decode<'_> for BeginTransactionRecord {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::SchemaMismatch(
                "BeginTransactionRecord version out of range",
            ));
        }
        let flex = is_flexible(version);
        let mut out = Self::default();
        if flex {
            let mut tag_name = None;
            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
                0 => {
                    tag_name = Some({
                        let b: &mut &[u8] = payload;
                        if flex {
                            get_compact_nullable_string_owned(b)?
                        } else {
                            get_nullable_string_owned(b)?
                        }
                    });
                    Ok(true)
                }
                _ => Ok(false),
            })?;
            if let Some(v) = tag_name {
                out.name = v;
            }
        }
        Ok(out)
    }
}
#[cfg(test)]
impl BeginTransactionRecord {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.name = Some("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("name".to_string(), ::serde_json::Value::Null);
    ::serde_json::Value::Object(obj)
}