use bytes::{Buf, BufMut};
use crate::{
Decode, Encode, ProtocolError, ProtocolRequest, UnknownTaggedFields,
krabka::{
barrier::cut::{BarrierCutTopic, BarrierMissingPartition},
common::{
I8_LEN, I16_LEN, I32_LEN, I64_LEN, array_len, check_version, decode_array,
encode_array, read_unknown_tagged_fields, unknown_tagged_fields_len,
write_unknown_tagged_fields,
},
},
primitives::{
fixed::{get_i8, get_i16, get_i32, get_i64, put_i8, put_i16, put_i32, put_i64},
string_bytes::{
compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
get_compact_string_owned, put_compact_nullable_string, put_compact_string,
},
},
};
pub const API_KEY: i16 = 1013;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 0;
pub const FLEXIBLE_MIN: i16 = 0;
#[inline]
#[must_use]
pub fn is_flexible(version: i16) -> bool {
version >= FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListBarrierCutsRequest {
pub group: String,
pub from_epoch: i64,
pub max_results: i32,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for ListBarrierCutsRequest {
fn default() -> Self {
Self {
group: String::new(),
from_epoch: 0,
max_results: -1,
unknown_tagged_fields: UnknownTaggedFields::default(),
}
}
}
impl Encode for ListBarrierCutsRequest {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
check_version(API_KEY, version, MIN_VERSION, MAX_VERSION)?;
put_compact_string(buf, &self.group);
put_i64(buf, self.from_epoch);
put_i32(buf, self.max_results);
write_unknown_tagged_fields(buf, &self.unknown_tagged_fields);
Ok(())
}
fn encoded_len(&self, _version: i16) -> usize {
compact_string_len(&self.group)
+ I64_LEN
+ I32_LEN
+ unknown_tagged_fields_len(&self.unknown_tagged_fields)
}
}
impl Decode<'_> for ListBarrierCutsRequest {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
check_version(API_KEY, version, MIN_VERSION, MAX_VERSION)?;
Ok(Self {
group: get_compact_string_owned(buf)?,
from_epoch: get_i64(buf)?,
max_results: get_i32(buf)?,
unknown_tagged_fields: read_unknown_tagged_fields(buf)?,
})
}
}
impl ProtocolRequest for ListBarrierCutsRequest {
const API_KEY: i16 = API_KEY;
const MIN_VERSION: i16 = MIN_VERSION;
const MAX_VERSION: i16 = MAX_VERSION;
const LATEST_STABLE_VERSION: i16 = MAX_VERSION;
const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
type Response = ListBarrierCutsResponse;
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BarrierCut {
pub epoch: i64,
pub triggered_at: i64,
pub completed_at: i64,
pub status: i8,
pub topics: Vec<BarrierCutTopic>,
pub missing: Vec<BarrierMissingPartition>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for BarrierCut {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
put_i64(buf, self.epoch);
put_i64(buf, self.triggered_at);
put_i64(buf, self.completed_at);
put_i8(buf, self.status);
encode_array(buf, &self.topics, version)?;
encode_array(buf, &self.missing, version)?;
write_unknown_tagged_fields(buf, &self.unknown_tagged_fields);
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
I64_LEN
+ I64_LEN
+ I64_LEN
+ I8_LEN
+ array_len(&self.topics, version)
+ array_len(&self.missing, version)
+ unknown_tagged_fields_len(&self.unknown_tagged_fields)
}
}
impl Decode<'_> for BarrierCut {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
Ok(Self {
epoch: get_i64(buf)?,
triggered_at: get_i64(buf)?,
completed_at: get_i64(buf)?,
status: get_i8(buf)?,
topics: decode_array(buf, version)?,
missing: decode_array(buf, version)?,
unknown_tagged_fields: read_unknown_tagged_fields(buf)?,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ListBarrierCutsResponse {
pub throttle_time_ms: i32,
pub error_code: i16,
pub error_message: Option<String>,
pub cuts: Vec<BarrierCut>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for ListBarrierCutsResponse {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
check_version(API_KEY, version, MIN_VERSION, MAX_VERSION)?;
put_i32(buf, self.throttle_time_ms);
put_i16(buf, self.error_code);
put_compact_nullable_string(buf, self.error_message.as_deref());
encode_array(buf, &self.cuts, version)?;
write_unknown_tagged_fields(buf, &self.unknown_tagged_fields);
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
I32_LEN
+ I16_LEN
+ compact_nullable_string_len(self.error_message.as_deref())
+ array_len(&self.cuts, version)
+ unknown_tagged_fields_len(&self.unknown_tagged_fields)
}
}
impl Decode<'_> for ListBarrierCutsResponse {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
check_version(API_KEY, version, MIN_VERSION, MAX_VERSION)?;
Ok(Self {
throttle_time_ms: get_i32(buf)?,
error_code: get_i16(buf)?,
error_message: get_compact_nullable_string_owned(buf)?,
cuts: decode_array(buf, version)?,
unknown_tagged_fields: read_unknown_tagged_fields(buf)?,
})
}
}
#[cfg(test)]
impl ListBarrierCutsRequest {
fn populated() -> Self {
Self {
group: "orders".to_string(),
from_epoch: 12,
max_results: 100,
unknown_tagged_fields: crate::krabka::test_support::sample_tagged_fields(),
}
}
}
#[cfg(test)]
impl ListBarrierCutsResponse {
fn populated() -> Self {
Self {
throttle_time_ms: 9,
error_code: 0,
error_message: None,
cuts: vec![
BarrierCut {
epoch: 12,
triggered_at: 1_724_500_000_000,
completed_at: 1_724_500_000_120,
status: crate::krabka::barrier::CUT_STATUS_COMPLETE,
topics: super::test_support::sample_cut_topics(),
missing: Vec::new(),
unknown_tagged_fields: UnknownTaggedFields::default(),
},
BarrierCut {
epoch: 13,
triggered_at: 1_724_500_060_000,
completed_at: 1_724_500_090_000,
status: crate::krabka::barrier::CUT_STATUS_PARTIAL,
topics: super::test_support::sample_cut_topics(),
missing: super::test_support::sample_missing_partitions(),
unknown_tagged_fields: crate::krabka::test_support::sample_tagged_fields(),
},
],
unknown_tagged_fields: crate::krabka::test_support::sample_tagged_fields(),
}
}
}
#[cfg(test)]
mod tests {
use assert2::assert;
use super::*;
use crate::krabka::test_support::{reject_truncated, roundtrip, unsupported_versions};
#[test]
fn default_roundtrips_all_versions() {
for version in MIN_VERSION..=MAX_VERSION {
roundtrip(&ListBarrierCutsRequest::default(), version);
roundtrip(&ListBarrierCutsResponse::default(), version);
}
}
#[test]
fn populated_roundtrips_all_versions() {
for version in MIN_VERSION..=MAX_VERSION {
roundtrip(&ListBarrierCutsRequest::populated(), version);
roundtrip(&ListBarrierCutsResponse::populated(), version);
}
}
#[test]
fn unlimited_is_the_max_results_default() {
assert!(ListBarrierCutsRequest::default().max_results == -1);
}
#[test]
fn truncated_input_is_rejected() {
reject_truncated(&ListBarrierCutsResponse::populated(), MIN_VERSION);
}
#[test]
fn rejects_unsupported_versions() {
for version in unsupported_versions(MIN_VERSION, MAX_VERSION) {
let response = ListBarrierCutsResponse::populated();
assert!(matches!(
response.encode(&mut Vec::<u8>::new(), version),
Err(ProtocolError::UnsupportedVersion {
api_key: API_KEY,
..
})
));
}
}
}