use bytes::{Buf, BufMut};
use crate::{
Decode, Encode, ProtocolError, ProtocolRequest, UnknownTaggedFields,
krabka::common::{
I8_LEN, I16_LEN, I32_LEN, I64_LEN, UUID_LEN, check_version, 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,
},
uuid::{Uuid, get_uuid, put_uuid},
},
};
pub const API_KEY: i16 = 1017;
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, Default)]
pub struct ProposeBreakGlassRequest {
pub action: i8,
pub target: String,
pub reason: String,
pub ttl_ms: i64,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for ProposeBreakGlassRequest {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
check_version(API_KEY, version, MIN_VERSION, MAX_VERSION)?;
put_i8(buf, self.action);
put_compact_string(buf, &self.target);
put_compact_string(buf, &self.reason);
put_i64(buf, self.ttl_ms);
write_unknown_tagged_fields(buf, &self.unknown_tagged_fields);
Ok(())
}
fn encoded_len(&self, _version: i16) -> usize {
I8_LEN
+ compact_string_len(&self.target)
+ compact_string_len(&self.reason)
+ I64_LEN
+ unknown_tagged_fields_len(&self.unknown_tagged_fields)
}
}
impl Decode<'_> for ProposeBreakGlassRequest {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
check_version(API_KEY, version, MIN_VERSION, MAX_VERSION)?;
Ok(Self {
action: get_i8(buf)?,
target: get_compact_string_owned(buf)?,
reason: get_compact_string_owned(buf)?,
ttl_ms: get_i64(buf)?,
unknown_tagged_fields: read_unknown_tagged_fields(buf)?,
})
}
}
impl ProtocolRequest for ProposeBreakGlassRequest {
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 = ProposeBreakGlassResponse;
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ProposeBreakGlassResponse {
pub throttle_time_ms: i32,
pub error_code: i16,
pub error_message: Option<String>,
pub proposal_id: Uuid,
pub expires_at_ms: i64,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for ProposeBreakGlassResponse {
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());
put_uuid(buf, self.proposal_id);
put_i64(buf, self.expires_at_ms);
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())
+ UUID_LEN
+ I64_LEN
+ unknown_tagged_fields_len(&self.unknown_tagged_fields)
}
}
impl Decode<'_> for ProposeBreakGlassResponse {
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)?,
proposal_id: get_uuid(buf)?,
expires_at_ms: get_i64(buf)?,
unknown_tagged_fields: read_unknown_tagged_fields(buf)?,
})
}
}
#[cfg(test)]
impl ProposeBreakGlassRequest {
fn populated() -> Self {
Self {
action: 6,
target: "doomed".to_string(),
reason: "topic holds test data only".to_string(),
ttl_ms: 1_800_000,
unknown_tagged_fields: crate::krabka::test_support::sample_tagged_fields(),
}
}
}
#[cfg(test)]
impl ProposeBreakGlassResponse {
fn populated() -> Self {
Self {
throttle_time_ms: 4,
error_code: 0,
error_message: None,
proposal_id: Uuid([
200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215,
]),
expires_at_ms: 1_770_000_180_000,
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_case, unsupported_versions};
#[test]
fn requests_roundtrip() {
let cases = [
("default", ProposeBreakGlassRequest::default()),
("delete a topic", ProposeBreakGlassRequest::populated()),
(
"configured lifetime and no reason",
ProposeBreakGlassRequest {
action: 1,
target: "literal:orders".to_string(),
reason: String::new(),
ttl_ms: 0,
unknown_tagged_fields: UnknownTaggedFields::default(),
},
),
];
for (label, request) in &cases {
for version in MIN_VERSION..=MAX_VERSION {
roundtrip_case(label, request, version);
}
}
}
#[test]
fn responses_roundtrip() {
let cases = [
("default", ProposeBreakGlassResponse::default()),
("accepted proposal", ProposeBreakGlassResponse::populated()),
(
"refusal carries no proposal",
ProposeBreakGlassResponse {
throttle_time_ms: 0,
error_code: 1008,
error_message: Some("principal is not an approver".to_string()),
proposal_id: Uuid::ZERO,
expires_at_ms: 0,
unknown_tagged_fields: UnknownTaggedFields::default(),
},
),
];
for (label, response) in &cases {
for version in MIN_VERSION..=MAX_VERSION {
roundtrip_case(label, response, version);
}
}
}
#[test]
fn a_default_request_asks_for_the_configured_lifetime() {
assert!(ProposeBreakGlassRequest::default().ttl_ms == 0);
}
#[test]
fn truncated_input_is_rejected() {
reject_truncated(&ProposeBreakGlassRequest::populated(), MIN_VERSION);
reject_truncated(&ProposeBreakGlassResponse::populated(), MIN_VERSION);
}
#[test]
fn rejects_unsupported_versions() {
for version in unsupported_versions(MIN_VERSION, MAX_VERSION) {
let request = ProposeBreakGlassRequest::populated();
assert!(matches!(
request.encode(&mut Vec::<u8>::new(), version),
Err(ProtocolError::UnsupportedVersion {
api_key: API_KEY,
..
})
));
let response = ProposeBreakGlassResponse::populated();
assert!(matches!(
response.encode(&mut Vec::<u8>::new(), version),
Err(ProtocolError::UnsupportedVersion {
api_key: API_KEY,
..
})
));
}
}
}