use std::borrow::Cow;
use crate::{sql_read_bytes::SqlReadBytes, ColumnData};
pub(crate) async fn decode<R>(src: &mut R) -> crate::Result<ColumnData<'static>>
where
R: SqlReadBytes + Unpin,
{
let data = super::plp::decode(src, 0xffff_ffff).await?.map(Cow::Owned);
Ok(ColumnData::Binary(data))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sql_read_bytes::test_utils::IntoSqlReadBytes;
use bytes::{BufMut, BytesMut};
#[tokio::test]
async fn decode_udt_plp_bytes() {
let payload: &[u8] = &[0xde, 0xad, 0xbe, 0xef];
let mut buf = BytesMut::new();
buf.put_u64_le(0xfffffffffffffffe);
buf.put_u32_le(payload.len() as u32);
buf.extend_from_slice(payload);
buf.put_u32_le(0);
let data = decode(&mut buf.into_sql_read_bytes())
.await
.expect("decode must succeed");
match data {
ColumnData::Binary(Some(bytes)) => assert_eq!(bytes.as_ref(), payload),
other => panic!("expected Binary, got {:?}", other),
}
}
#[tokio::test]
async fn decode_udt_null() {
let mut buf = BytesMut::new();
buf.put_u64_le(0xffffffffffffffff);
let data = decode(&mut buf.into_sql_read_bytes())
.await
.expect("decode must succeed");
assert!(matches!(data, ColumnData::Binary(None)));
}
}