This is the meat of the encoding logic. It's a separate function so that errors returned by `?` can be handled in the outer `encode` function.
(&self, msg: BackendMessage, dst: &mut BytesMut)
| 266 | /// This is the meat of the encoding logic. It's a separate function so that errors returned by |
| 267 | /// `?` can be handled in the outer `encode` function. |
| 268 | fn encode_inner(&self, msg: BackendMessage, dst: &mut BytesMut) -> Result<(), io::Error> { |
| 269 | // Write type byte. |
| 270 | let byte = match &msg { |
| 271 | BackendMessage::AuthenticationOk => b'R', |
| 272 | BackendMessage::AuthenticationCleartextPassword |
| 273 | | BackendMessage::AuthenticationSASL |
| 274 | | BackendMessage::AuthenticationSASLContinue(_) |
| 275 | | BackendMessage::AuthenticationSASLFinal(_) => b'R', |
| 276 | BackendMessage::RowDescription(_) => b'T', |
| 277 | BackendMessage::DataRow(_) => b'D', |
| 278 | BackendMessage::CommandComplete { .. } => b'C', |
| 279 | BackendMessage::EmptyQueryResponse => b'I', |
| 280 | BackendMessage::ReadyForQuery(_) => b'Z', |
| 281 | BackendMessage::NoData => b'n', |
| 282 | BackendMessage::ParameterStatus(_, _) => b'S', |
| 283 | BackendMessage::PortalSuspended => b's', |
| 284 | BackendMessage::BackendKeyData { .. } => b'K', |
| 285 | BackendMessage::ParameterDescription(_) => b't', |
| 286 | BackendMessage::ParseComplete => b'1', |
| 287 | BackendMessage::BindComplete => b'2', |
| 288 | BackendMessage::CloseComplete => b'3', |
| 289 | BackendMessage::ErrorResponse(r) => { |
| 290 | if r.severity.is_error() { |
| 291 | b'E' |
| 292 | } else { |
| 293 | b'N' |
| 294 | } |
| 295 | } |
| 296 | BackendMessage::CopyInResponse { .. } => b'G', |
| 297 | BackendMessage::CopyOutResponse { .. } => b'H', |
| 298 | BackendMessage::CopyData(_) => b'd', |
| 299 | BackendMessage::CopyDone => b'c', |
| 300 | }; |
| 301 | dst.put_u8(byte); |
| 302 | |
| 303 | // Write message length placeholder. The true length is filled in later. |
| 304 | let base = dst.len(); |
| 305 | dst.put_u32(0); |
| 306 | |
| 307 | // Write message contents. |
| 308 | match msg { |
| 309 | BackendMessage::CopyInResponse { |
| 310 | overall_format, |
| 311 | column_formats, |
| 312 | } |
| 313 | | BackendMessage::CopyOutResponse { |
| 314 | overall_format, |
| 315 | column_formats, |
| 316 | } => { |
| 317 | dst.put_format_i8(overall_format); |
| 318 | if column_formats.len() > usize::try_from(i16::MAX).expect("i16::MAX is positive") { |
| 319 | return Err(io::Error::new( |
| 320 | io::ErrorKind::InvalidData, |
| 321 | format!( |
| 322 | "{} columns in COPY response, which exceeds {}", |
| 323 | column_formats.len(), |
| 324 | i16::MAX |
| 325 | ), |
no test coverage detected