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)
| 161 | /// This is the meat of the encoding logic. It's a separate function so that errors returned by |
| 162 | /// `?` can be handled in the outer `encode` function. |
| 163 | fn encode_inner(&self, msg: BackendMessage, dst: &mut BytesMut) -> Result<(), io::Error> { |
| 164 | // Write type byte. |
| 165 | let byte = match &msg { |
| 166 | BackendMessage::AuthenticationCleartextPassword => b'R', |
| 167 | BackendMessage::ErrorResponse(r) => { |
| 168 | if r.severity.is_error() { |
| 169 | b'E' |
| 170 | } else { |
| 171 | b'N' |
| 172 | } |
| 173 | } |
| 174 | }; |
| 175 | dst.put_u8(byte); |
| 176 | |
| 177 | // Write message length placeholder. The true length is filled in later. |
| 178 | let base = dst.len(); |
| 179 | dst.put_u32(0); |
| 180 | |
| 181 | // Write message contents. |
| 182 | match msg { |
| 183 | BackendMessage::AuthenticationCleartextPassword => { |
| 184 | dst.put_u32(3); |
| 185 | } |
| 186 | BackendMessage::ErrorResponse(ErrorResponse { |
| 187 | severity, |
| 188 | code, |
| 189 | message, |
| 190 | detail, |
| 191 | hint, |
| 192 | position, |
| 193 | }) => { |
| 194 | dst.put_u8(b'S'); |
| 195 | dst.put_string(severity.as_str()); |
| 196 | dst.put_u8(b'C'); |
| 197 | dst.put_string(code.code()); |
| 198 | dst.put_u8(b'M'); |
| 199 | dst.put_string(&message); |
| 200 | if let Some(detail) = &detail { |
| 201 | dst.put_u8(b'D'); |
| 202 | dst.put_string(detail); |
| 203 | } |
| 204 | if let Some(hint) = &hint { |
| 205 | dst.put_u8(b'H'); |
| 206 | dst.put_string(hint); |
| 207 | } |
| 208 | if let Some(position) = &position { |
| 209 | dst.put_u8(b'P'); |
| 210 | dst.put_string(&position.to_string()); |
| 211 | } |
| 212 | dst.put_u8(b'\0'); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | let len = dst.len() - base; |
| 217 | |
| 218 | // Overwrite length placeholder with true length. |
| 219 | let len = i32::try_from(len).map_err(|_| { |
| 220 | io::Error::new( |