|
| 1 | +use std::io::Write; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + codec::{decoder::read_vec, Decoder, Encoder}, |
| 5 | + error::{DecodeError, EncodeError}, |
| 6 | + protocol::commands::COMMAND_EXCHANGE_COMMAND_VERSIONS, |
| 7 | + response::{FromResponse, ResponseCode}, |
| 8 | +}; |
| 9 | + |
| 10 | +use super::Command; |
| 11 | +use byteorder::{BigEndian, WriteBytesExt}; |
| 12 | + |
| 13 | +#[cfg(test)] |
| 14 | +use fake::Fake; |
| 15 | + |
| 16 | +#[cfg_attr(test, derive(fake::Dummy))] |
| 17 | +#[derive(PartialEq, Eq, Debug)] |
| 18 | +pub struct ExchangeCommandVersion(u16, u16, u16); |
| 19 | + |
| 20 | +impl ExchangeCommandVersion { |
| 21 | + pub fn new(key: u16, min_version: u16, max_version: u16) -> Self { |
| 22 | + ExchangeCommandVersion(key, min_version, max_version) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl Encoder for ExchangeCommandVersion { |
| 27 | + fn encoded_size(&self) -> u32 { |
| 28 | + self.0.encoded_size() + self.1.encoded_size() + self.2.encoded_size() |
| 29 | + } |
| 30 | + |
| 31 | + fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> { |
| 32 | + self.0.encode(writer)?; |
| 33 | + self.1.encode(writer)?; |
| 34 | + self.2.encode(writer)?; |
| 35 | + |
| 36 | + Ok(()) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl Decoder for ExchangeCommandVersion { |
| 41 | + fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> { |
| 42 | + let (input, key) = u16::decode(input)?; |
| 43 | + let (input, min_version) = u16::decode(input)?; |
| 44 | + let (input, max_version) = u16::decode(input)?; |
| 45 | + Ok((input, ExchangeCommandVersion(key, min_version, max_version))) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl Encoder for Vec<ExchangeCommandVersion> { |
| 50 | + fn encoded_size(&self) -> u32 { |
| 51 | + 4 + self.iter().fold(0, |acc, v| acc + v.encoded_size()) |
| 52 | + } |
| 53 | + |
| 54 | + fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> { |
| 55 | + writer.write_u32::<BigEndian>(self.len() as u32)?; |
| 56 | + for x in self { |
| 57 | + x.encode(writer)?; |
| 58 | + } |
| 59 | + Ok(()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl Decoder for Vec<ExchangeCommandVersion> { |
| 64 | + fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> { |
| 65 | + let (input, result) = read_vec(input)?; |
| 66 | + Ok((input, result)) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg_attr(test, derive(fake::Dummy))] |
| 71 | +#[derive(PartialEq, Eq, Debug)] |
| 72 | +pub struct ExchangeCommandVersionsRequest { |
| 73 | + pub(crate) correlation_id: u32, |
| 74 | + commands: Vec<ExchangeCommandVersion>, |
| 75 | +} |
| 76 | + |
| 77 | +impl ExchangeCommandVersionsRequest { |
| 78 | + pub fn new(correlation_id: u32, commands: Vec<ExchangeCommandVersion>) -> Self { |
| 79 | + Self { |
| 80 | + correlation_id, |
| 81 | + commands, |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl Encoder for ExchangeCommandVersionsRequest { |
| 87 | + fn encoded_size(&self) -> u32 { |
| 88 | + self.correlation_id.encoded_size() + self.commands.encoded_size() |
| 89 | + } |
| 90 | + |
| 91 | + fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> { |
| 92 | + self.correlation_id.encode(writer)?; |
| 93 | + self.commands.encode(writer)?; |
| 94 | + Ok(()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl Command for ExchangeCommandVersionsRequest { |
| 99 | + fn key(&self) -> u16 { |
| 100 | + COMMAND_EXCHANGE_COMMAND_VERSIONS |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl Decoder for ExchangeCommandVersionsRequest { |
| 105 | + fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> { |
| 106 | + let (input, correlation_id) = u32::decode(input)?; |
| 107 | + let (input, commands) = <Vec<ExchangeCommandVersion>>::decode(input)?; |
| 108 | + Ok(( |
| 109 | + input, |
| 110 | + ExchangeCommandVersionsRequest { |
| 111 | + correlation_id, |
| 112 | + commands, |
| 113 | + }, |
| 114 | + )) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[cfg_attr(test, derive(fake::Dummy))] |
| 119 | +#[derive(PartialEq, Eq, Debug)] |
| 120 | +pub struct ExchangeCommandVersionsResponse { |
| 121 | + pub(crate) correlation_id: u32, |
| 122 | + response_code: ResponseCode, |
| 123 | + commands: Vec<ExchangeCommandVersion>, |
| 124 | +} |
| 125 | + |
| 126 | +impl ExchangeCommandVersionsResponse { |
| 127 | + pub fn new( |
| 128 | + correlation_id: u32, |
| 129 | + response_code: ResponseCode, |
| 130 | + commands: Vec<ExchangeCommandVersion>, |
| 131 | + ) -> Self { |
| 132 | + Self { |
| 133 | + correlation_id, |
| 134 | + response_code, |
| 135 | + commands, |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + pub fn code(&self) -> &ResponseCode { |
| 140 | + &self.response_code |
| 141 | + } |
| 142 | + |
| 143 | + pub fn is_ok(&self) -> bool { |
| 144 | + self.response_code == ResponseCode::Ok |
| 145 | + } |
| 146 | + |
| 147 | + pub fn key_version(&self, key_command: u16) -> (u16, u16) { |
| 148 | + for i in &self.commands { |
| 149 | + match i { |
| 150 | + ExchangeCommandVersion(match_key_command, min_version, max_version) => { |
| 151 | + if *match_key_command == key_command { |
| 152 | + return (*min_version, *max_version); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + (1, 1) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl Encoder for ExchangeCommandVersionsResponse { |
| 163 | + fn encoded_size(&self) -> u32 { |
| 164 | + self.correlation_id.encoded_size() |
| 165 | + + self.response_code.encoded_size() |
| 166 | + + self.commands.encoded_size() |
| 167 | + } |
| 168 | + |
| 169 | + fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> { |
| 170 | + self.correlation_id.encode(writer)?; |
| 171 | + self.response_code.encode(writer)?; |
| 172 | + self.commands.encode(writer)?; |
| 173 | + Ok(()) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +impl Decoder for ExchangeCommandVersionsResponse { |
| 178 | + fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> { |
| 179 | + let (input, correlation_id) = u32::decode(input)?; |
| 180 | + let (input, response_code) = ResponseCode::decode(input)?; |
| 181 | + let (input, commands) = <Vec<ExchangeCommandVersion>>::decode(input)?; |
| 182 | + |
| 183 | + Ok(( |
| 184 | + input, |
| 185 | + ExchangeCommandVersionsResponse { |
| 186 | + correlation_id, |
| 187 | + response_code, |
| 188 | + commands, |
| 189 | + }, |
| 190 | + )) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +impl FromResponse for ExchangeCommandVersionsResponse { |
| 195 | + fn from_response(response: crate::Response) -> Option<Self> { |
| 196 | + match response.kind { |
| 197 | + crate::ResponseKind::ExchangeCommandVersions(exchange_command_versions) => { |
| 198 | + Some(exchange_command_versions) |
| 199 | + } |
| 200 | + _ => None, |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +#[cfg(test)] |
| 206 | +mod tests { |
| 207 | + |
| 208 | + use crate::commands::tests::command_encode_decode_test; |
| 209 | + |
| 210 | + use super::{ExchangeCommandVersionsRequest, ExchangeCommandVersionsResponse}; |
| 211 | + |
| 212 | + #[test] |
| 213 | + fn exchange_command_versions_request_test() { |
| 214 | + command_encode_decode_test::<ExchangeCommandVersionsRequest>(); |
| 215 | + } |
| 216 | + |
| 217 | + #[test] |
| 218 | + fn exchange_command_versions_response_test() { |
| 219 | + command_encode_decode_test::<ExchangeCommandVersionsResponse>(); |
| 220 | + } |
| 221 | +} |
0 commit comments