use bytes::{Buf, BufMut};
use crate::primitives::fixed::{get_i8, get_i32, get_i64, put_i8, put_i32, put_i64};
use crate::primitives::string_bytes::{
compact_nullable_bytes_len, get_compact_nullable_bytes_owned, get_nullable_bytes_owned,
nullable_bytes_len, put_compact_nullable_bytes, put_nullable_bytes,
};
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 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 RemoteLogSegmentMetadataUpdateRecord {
pub remote_log_segment_id: RemoteLogSegmentIdEntry,
pub broker_id: i32,
pub event_timestamp_ms: i64,
pub custom_metadata: Option<::bytes::Bytes>,
pub remote_log_segment_state: i8,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for RemoteLogSegmentMetadataUpdateRecord {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::SchemaMismatch(
"RemoteLogSegmentMetadataUpdateRecord version out of range",
));
}
let flex = is_flexible(version);
if version >= 0 {
self.remote_log_segment_id.encode(buf, version)?;
}
if version >= 0 {
put_i32(buf, self.broker_id);
}
if version >= 0 {
put_i64(buf, self.event_timestamp_ms);
}
if version >= 0 {
if flex {
put_compact_nullable_bytes(buf, self.custom_metadata.as_deref());
} else {
put_nullable_bytes(buf, self.custom_metadata.as_deref());
}
}
if version >= 0 {
put_i8(buf, self.remote_log_segment_state);
}
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 += self.remote_log_segment_id.encoded_len(version);
}
if version >= 0 {
n += 4;
}
if version >= 0 {
n += 8;
}
if version >= 0 {
n += if flex {
compact_nullable_bytes_len(self.custom_metadata.as_deref())
} else {
nullable_bytes_len(self.custom_metadata.as_deref())
};
}
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 RemoteLogSegmentMetadataUpdateRecord {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::SchemaMismatch(
"RemoteLogSegmentMetadataUpdateRecord version out of range",
));
}
let flex = is_flexible(version);
let mut out = Self::default();
if version >= 0 {
out.remote_log_segment_id = RemoteLogSegmentIdEntry::decode(buf, version)?;
}
if version >= 0 {
out.broker_id = get_i32(buf)?;
}
if version >= 0 {
out.event_timestamp_ms = get_i64(buf)?;
}
if version >= 0 {
out.custom_metadata = if flex {
get_compact_nullable_bytes_owned(buf)?
} else {
get_nullable_bytes_owned(buf)?
};
}
if version >= 0 {
out.remote_log_segment_state = get_i8(buf)?;
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl RemoteLogSegmentMetadataUpdateRecord {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.remote_log_segment_id = RemoteLogSegmentIdEntry::populated(version);
}
if version >= 0 {
m.broker_id = 1i32;
}
if version >= 0 {
m.event_timestamp_ms = 1i64;
}
if version >= 0 {
m.custom_metadata = Some(::bytes::Bytes::from_static(b"x"));
}
if version >= 0 {
m.remote_log_segment_state = 1i8;
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct RemoteLogSegmentIdEntry {
pub topic_id_partition: TopicIdPartitionEntry,
pub id: crate::primitives::uuid::Uuid,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for RemoteLogSegmentIdEntry {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 0;
if version >= 0 {
self.topic_id_partition.encode(buf, version)?;
}
if version >= 0 {
crate::primitives::uuid::put_uuid(buf, self.id);
}
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 0;
let mut n: usize = 0;
if version >= 0 {
n += self.topic_id_partition.encoded_len(version);
}
if version >= 0 {
n += 16;
}
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl Decode<'_> for RemoteLogSegmentIdEntry {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 0;
let mut out = Self::default();
if version >= 0 {
out.topic_id_partition = TopicIdPartitionEntry::decode(buf, version)?;
}
if version >= 0 {
out.id = crate::primitives::uuid::get_uuid(buf)?;
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl RemoteLogSegmentIdEntry {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.topic_id_partition = TopicIdPartitionEntry::populated(version);
}
if version >= 0 {
m.id = crate::primitives::uuid::Uuid([1u8; 16]);
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TopicIdPartitionEntry {
pub name: String,
pub id: crate::primitives::uuid::Uuid,
pub partition: i32,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for TopicIdPartitionEntry {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 0;
if version >= 0 {
if flex {
put_compact_string(buf, &self.name);
} else {
put_string(buf, &self.name);
}
}
if version >= 0 {
crate::primitives::uuid::put_uuid(buf, self.id);
}
if version >= 0 {
put_i32(buf, self.partition);
}
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 0;
let mut n: usize = 0;
if version >= 0 {
n += if flex {
compact_string_len(&self.name)
} else {
string_len(&self.name)
};
}
if version >= 0 {
n += 16;
}
if version >= 0 {
n += 4;
}
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl Decode<'_> for TopicIdPartitionEntry {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 0;
let mut out = Self::default();
if version >= 0 {
out.name = if flex {
get_compact_string_owned(buf)?
} else {
get_string_owned(buf)?
};
}
if version >= 0 {
out.id = crate::primitives::uuid::get_uuid(buf)?;
}
if version >= 0 {
out.partition = get_i32(buf)?;
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl TopicIdPartitionEntry {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.name = "x".to_string();
}
if version >= 0 {
m.id = crate::primitives::uuid::Uuid([1u8; 16]);
}
if version >= 0 {
m.partition = 1i32;
}
m
}
}
#[must_use]
#[allow(unused_comparisons)]
pub fn default_json(version: i16) -> ::serde_json::Value {
let mut obj = ::serde_json::Map::new();
obj.insert("remoteLogSegmentId".to_string(), {
let mut m = ::serde_json::Map::new();
m.insert("topicIdPartition".to_string(), {
let mut m = ::serde_json::Map::new();
m.insert(
"name".to_string(),
::serde_json::Value::String(String::new()),
);
m.insert(
"id".to_string(),
::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()),
);
m.insert("partition".to_string(), ::serde_json::json!(0));
::serde_json::Value::Object(m)
});
m.insert(
"id".to_string(),
::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()),
);
::serde_json::Value::Object(m)
});
obj.insert("brokerId".to_string(), ::serde_json::json!(0));
obj.insert("eventTimestampMs".to_string(), ::serde_json::json!(0));
obj.insert("customMetadata".to_string(), ::serde_json::Value::Null);
obj.insert("remoteLogSegmentState".to_string(), ::serde_json::json!(0));
::serde_json::Value::Object(obj)
}