| 1 | use sqlx_core::bytes::Bytes; |
| 2 | |
| 3 | use crate::error::Error; |
| 4 | use crate::io::BufExt; |
| 5 | use crate::message::{BackendMessage, BackendMessageFormat}; |
| 6 | |
| 7 | #[derive(Debug)] |
| 8 | pub struct ParameterStatus { |
| 9 | pub name: String, |
| 10 | pub value: String, |
| 11 | } |
| 12 | |
| 13 | impl BackendMessage for ParameterStatus { |
| 14 | const FORMAT: BackendMessageFormat = BackendMessageFormat::ParameterStatus; |
| 15 | |
| 16 | fn decode_body(mut buf: Bytes) -> Result<Self, Error> { |
| 17 | let name = buf.get_str_nul()?; |
| 18 | let value = buf.get_str_nul()?; |
| 19 | |
| 20 | Ok(Self { name, value }) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn test_decode_parameter_status() { |
| 26 | const DATA: &[u8] = b"client_encoding\x00UTF8\x00"; |
| 27 | |
| 28 | let m = ParameterStatus::decode_body(DATA.into()).unwrap(); |
| 29 | |
| 30 | assert_eq!(&m.name, "client_encoding"); |
| 31 | assert_eq!(&m.value, "UTF8") |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn test_decode_empty_parameter_status() { |
| 36 | const DATA: &[u8] = b"\x00\x00"; |
| 37 | |
| 38 | let m = ParameterStatus::decode_body(DATA.into()).unwrap(); |
| 39 | |
| 40 | assert!(m.name.is_empty()); |
| 41 | assert!(m.value.is_empty()); |
| 42 | } |
| 43 | |
| 44 | #[cfg(all(test, not(debug_assertions)))] |
| 45 | #[bench] |
| 46 | fn bench_decode_parameter_status(b: &mut test::Bencher) { |
| 47 | const DATA: &[u8] = b"client_encoding\x00UTF8\x00"; |
| 48 | |
| 49 | b.iter(|| { |
| 50 | ParameterStatus::decode_body(test::black_box(Bytes::from_static(DATA))).unwrap(); |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn test_decode_parameter_status_response() { |
| 56 | const PARAMETER_STATUS_RESPONSE: &[u8] = b"crdb_version\0CockroachDB CCL v21.1.0 (x86_64-unknown-linux-gnu, built 2021/05/17 13:49:40, go1.15.11)\0"; |
| 57 | |
| 58 | let message = ParameterStatus::decode_body(Bytes::from(PARAMETER_STATUS_RESPONSE)).unwrap(); |
| 59 | |
| 60 | assert_eq!(message.name, "crdb_version"); |
| 61 | assert_eq!( |
| 62 | message.value, |
| 63 | "CockroachDB CCL v21.1.0 (x86_64-unknown-linux-gnu, built 2021/05/17 13:49:40, go1.15.11)" |
| 64 | ); |
| 65 | } |