rabbitmq_stream_protocol/commands/
close.rs1use std::io::Write;
10
11use crate::{
12 codec::{Decoder, Encoder},
13 error::{DecodeError, EncodeError},
14 protocol::commands::COMMAND_CLOSE,
15 FromResponse, ResponseCode,
16};
17
18use super::Command;
19
20#[cfg_attr(test, derive(fake::Dummy))]
21#[derive(PartialEq, Eq, Debug)]
22pub struct CloseRequest {
23 correlation_id: u32,
24 closing_code: ResponseCode,
25 closing_reason: String,
26}
27
28impl CloseRequest {
29 pub fn new(correlation_id: u32, closing_code: ResponseCode, closing_reason: String) -> Self {
30 Self {
31 correlation_id,
32 closing_code,
33 closing_reason,
34 }
35 }
36}
37
38impl Encoder for CloseRequest {
39 fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> {
40 self.correlation_id.encode(writer)?;
41 self.closing_code.encode(writer)?;
42 self.closing_reason.as_str().encode(writer)?;
43 Ok(())
44 }
45
46 fn encoded_size(&self) -> u32 {
47 self.correlation_id.encoded_size()
48 + self.closing_code.encoded_size()
49 + self.closing_reason.as_str().encoded_size()
50 }
51}
52
53impl Command for CloseRequest {
54 fn key(&self) -> u16 {
55 COMMAND_CLOSE
56 }
57}
58impl Decoder for CloseRequest {
59 fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
60 let (input, correlation_id) = u32::decode(input)?;
61 let (input, closing_code) = ResponseCode::decode(input)?;
62 let (input, closing_reason) = Option::decode(input)?;
63
64 Ok((
65 input,
66 CloseRequest {
67 correlation_id,
68 closing_code,
69 closing_reason: closing_reason.unwrap(),
70 },
71 ))
72 }
73}
74
75#[cfg_attr(test, derive(fake::Dummy))]
76#[derive(PartialEq, Eq, Debug)]
77pub struct CloseResponse {
78 pub(crate) correlation_id: u32,
79 response_code: ResponseCode,
80}
81
82impl CloseResponse {
83 pub fn new(correlation_id: u32, response_code: ResponseCode) -> Self {
84 Self {
85 correlation_id,
86 response_code,
87 }
88 }
89 pub fn is_ok(&self) -> bool {
90 self.response_code == ResponseCode::Ok
91 }
92}
93
94impl Encoder for CloseResponse {
95 fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> {
96 self.correlation_id.encode(writer)?;
97 self.response_code.encode(writer)?;
98 Ok(())
99 }
100
101 fn encoded_size(&self) -> u32 {
102 self.correlation_id.encoded_size() + self.response_code.encoded_size()
103 }
104}
105
106impl Decoder for CloseResponse {
107 fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
108 let (input, correlation_id) = u32::decode(input)?;
109 let (input, response_code) = ResponseCode::decode(input)?;
110
111 Ok((
112 input,
113 CloseResponse {
114 correlation_id,
115 response_code,
116 },
117 ))
118 }
119}
120
121impl FromResponse for CloseResponse {
122 fn from_response(response: crate::Response) -> Option<Self> {
123 match response.kind {
124 crate::ResponseKind::Close(close) => Some(close),
125 _ => None,
126 }
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use super::CloseRequest;
133 use super::CloseResponse;
134 use crate::commands::tests::command_encode_decode_test;
135
136 #[test]
137 fn close_request_test() {
138 command_encode_decode_test::<CloseRequest>()
139 }
140
141 #[test]
142 fn close_response_test() {
143 command_encode_decode_test::<CloseResponse>()
144 }
145}