use serde::{Deserialize, Serialize}; use serde_byte_array::ByteArray; use serde_test::{assert_de_tokens, assert_ser_tokens, assert_tokens, Token}; #[test] fn test_bytearray() { let empty = ByteArray::new([]); assert_tokens(&empty, &[Token::BorrowedBytes(b"")]); assert_ser_tokens(&empty, &[Token::Bytes(b"")]); assert_ser_tokens(&empty, &[Token::ByteBuf(b"")]); assert_de_tokens(&empty, &[Token::BorrowedStr("")]); let buf = [65, 66, 67]; let bytes = ByteArray::new(buf); assert_tokens(&bytes, &[Token::BorrowedBytes(b"ABC")]); assert_ser_tokens(&bytes, &[Token::Bytes(b"ABC")]); assert_ser_tokens(&bytes, &[Token::ByteBuf(b"ABC")]); assert_de_tokens(&bytes, &[Token::BorrowedStr("ABC")]); } #[test] fn test_bytearray_ref() { let empty = &ByteArray::new([]); assert_tokens(&empty, &[Token::BorrowedBytes(b"")]); assert_ser_tokens(&empty, &[Token::Bytes(b"")]); assert_ser_tokens(&empty, &[Token::ByteBuf(b"")]); assert_de_tokens(&empty, &[Token::BorrowedStr("")]); let buf = [65, 66, 67]; let bytes = &ByteArray::new(buf); assert_tokens(&bytes, &[Token::BorrowedBytes(b"ABC")]); assert_ser_tokens(&bytes, &[Token::Bytes(b"ABC")]); assert_ser_tokens(&bytes, &[Token::ByteBuf(b"ABC")]); assert_de_tokens(&bytes, &[Token::BorrowedStr("ABC")]); } #[derive(Serialize, Deserialize, PartialEq, Debug)] #[serde(transparent)] struct Bytes(#[serde(with = "serde_byte_array")] [u8; N]); #[test] fn test_array() { let empty = Bytes([]); assert_tokens(&empty, &[Token::BorrowedBytes(b"")]); assert_ser_tokens(&empty, &[Token::Bytes(b"")]); assert_ser_tokens(&empty, &[Token::ByteBuf(b"")]); assert_de_tokens(&empty, &[Token::BorrowedStr("")]); let buf = [65, 66, 67]; let bytes = Bytes(buf); assert_tokens(&bytes, &[Token::BorrowedBytes(b"ABC")]); assert_ser_tokens(&bytes, &[Token::Bytes(b"ABC")]); assert_ser_tokens(&bytes, &[Token::ByteBuf(b"ABC")]); assert_de_tokens(&bytes, &[Token::BorrowedStr("ABC")]); } #[derive(Serialize, Deserialize, PartialEq, Debug)] #[serde(transparent)] struct BytesRef<'a, const N: usize>(#[serde(with = "serde_byte_array", borrow)] &'a [u8; N]); #[test] fn test_array_ref() { let empty = BytesRef(&[]); assert_tokens(&empty, &[Token::BorrowedBytes(b"")]); assert_ser_tokens(&empty, &[Token::Bytes(b"")]); assert_ser_tokens(&empty, &[Token::ByteBuf(b"")]); assert_de_tokens(&empty, &[Token::BorrowedStr("")]); let buf = [65, 66, 67]; let bytes = BytesRef(&buf); assert_tokens(&bytes, &[Token::BorrowedBytes(b"ABC")]); assert_ser_tokens(&bytes, &[Token::Bytes(b"ABC")]); assert_ser_tokens(&bytes, &[Token::ByteBuf(b"ABC")]); assert_de_tokens(&bytes, &[Token::BorrowedStr("ABC")]); }