Skip to content

Commit

Permalink
test ser/de of LazyValue into/from Value
Browse files Browse the repository at this point in the history
  • Loading branch information
minghuaw committed Oct 2, 2024
1 parent 87675ec commit 746eb1e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 22 additions & 1 deletion serde_amqp/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<'de> Deserialize<'de> for LazyValue {
mod tests {
use crate::{
described::Described, descriptor::Descriptor, format_code::EncodingCodes, from_slice,
primitives::Array, to_vec,
from_value, primitives::Array, to_value, to_vec, Value,
};

use super::*;
Expand Down Expand Up @@ -195,6 +195,17 @@ mod tests {
assert_eq!(serialized, expected_bytes);
}

#[test]
fn test_serialize_into_value() {
let src = "Hello, World!";
let bytes = to_vec(&src).unwrap();
let mut reader = SliceReader::new(&bytes);
let lazy_value = LazyValue::from_reader(&mut reader).unwrap();

let value = to_value(&lazy_value).unwrap();
assert_eq!(value, Value::String(src.into()));
}

#[test]
fn test_deserialize() {
let src = "Hello, World!";
Expand All @@ -217,4 +228,14 @@ mod tests {
assert_eq!(lazy_value.as_slice(), &expected_bytes);
}
}

#[test]
fn test_deserialize_from_value() {
let src = "Hello, World!";
let value = Value::String(src.into());
let lazy: LazyValue = from_value(value).unwrap();

let expected = to_vec(&src).unwrap();
assert_eq!(lazy.as_slice(), &expected);
}
}
6 changes: 4 additions & 2 deletions serde_amqp/src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ where
Ok(bytes)
}

/// Read bytes of a primitive type or calls `op` if the encoded type is a described type
pub(crate) fn read_primitive_bytes_or_else<'de, R, F>(
reader: &mut R,
f: F,
op: F,
) -> Result<Vec<u8>, Error>
where
R: Read<'de>,
Expand All @@ -122,12 +123,13 @@ where
Ok(Category::Variable(width)) => read_encoded_len_bytes(reader, width)?,
Ok(Category::Compound(width)) => read_encoded_len_bytes(reader, width)?,
Ok(Category::Array(width)) => read_encoded_len_bytes(reader, width)?,
Err(_is_described) => f(reader)?,
Err(_is_described) => op(reader)?,
};

Ok(bytes)
}

/// Read bytes of a described type
pub(crate) fn read_described_bytes<'de, R>(reader: &mut R) -> Result<Vec<u8>, Error>
where
R: Read<'de>,
Expand Down

0 comments on commit 746eb1e

Please sign in to comment.