1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
//! Value type for untyped AMQP1.0 data structures.
use indexmap::IndexMap;
use ordered_float::OrderedFloat;
use serde::Serialize;
use serde_bytes::ByteBuf;
use std::{
collections::{BTreeMap, HashMap},
hash::Hash,
};
use crate::{
described::Described,
format_code::EncodingCodes,
primitives::{Array, Dec128, Dec32, Dec64, OrderedMap, Symbol, Timestamp, Uuid},
Error,
};
pub(crate) mod de;
pub(crate) mod ser;
/// Primitive type definitions
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum Value {
/// Described type
///
/// Box is used to reduce the memory size of the Value type.
Described(Box<Described<Value>>),
/// Indicates an empty value
///
/// encoding code = 0x40,
/// category = fixed, width = 0,
/// label = "the null value"
#[default]
Null,
/// Represents a true or false value
///
/// encoding code = 0x56
/// category = fixed, width = 1
/// label = "boolean with the octet 0x00 being false and octet 0x01 being true"
///
/// encoding name = "true", encoding code = 0x41
/// category = fixed, width = 0
/// label = "the boolean value true"
///
/// encoding name = "false", encoding code = 0x42
/// category = fixed, width = 0
/// label = "the boolean value false"
Bool(bool),
/// Integer in the range 0 to 2^8-1 inclusive
///
/// encoding code = 0x50,
/// category = fixed, width = 1
/// label = "8-bit unsigned integer"
Ubyte(u8),
/// Integer in the range 0 to 2^16-1 inclusive
///
/// encoding code = 0x60,
/// category = fixed, width = 2
/// label = "16-bit unsigned integer in network byte order"
/// (AKA. Big-Endian, rust uses BigEndian by default)
Ushort(u16),
/// Integer in the range 0 to 2^32-1 inclusive
///
/// encoding code = 0x70,
/// category = fixed, width = 4
/// label = "32-bit unsigned integer in network byte order"
/// (AKA. Big-Endian, rust uses BigEndian by default)
///
/// encoding name = "smalluint", encoding code = 0x52
/// category = fixed, width = 1
/// label = "unsigned integer value in the range 0 to 255 inclusive"
///
/// encoding name = "uint0", encoding code = 0x43
/// category = fixed, width = 0
/// label = "the uint value 0"
Uint(u32),
/// Integer in the range 0 to 2^64-1 inclusive
///
/// encoding code = 0x80,
/// category = fixed, width = 8
/// label = "64-bit unsigned integer in network byte order"
/// (AKA. Big-Endian, rust uses BigEndian by default)
///
/// encoding name = "smallulong", encoding code = 0x53
/// category = fixed, width = 1
/// label = "unsigned long value in the range 0 to 255 inclusive"
///
/// encoding name = "ulong0", encoding code = 0x44
/// category = fixed, width = 0
/// label = "the ulong value 0"
Ulong(u64),
/// Integer in the range -(2^7) to 2^7-1 inclusive
///
/// encoding code = 0x51,
/// category = fixed, width = 1
/// label = "8-bit two's-complement integer"
Byte(i8),
/// Integer in the range -(2^15) to 2^15-1 inclusive
///
/// encoding code = 0x61,
/// category = fixed, width = 2
/// label = "16-bit two’s-complement integer in network byte order"
Short(i16),
/// Integer in the range -(2^31) to 2^31-1 inclusive
///
/// encoding code = 0x71,
/// category = fixed, width = 4
/// label = "32-bit two’s-complement integer in network byte order"
///
/// encoding name = "smallint", encoding code = 0x54
/// category = fixed, width = 1
/// label = "8-bit two’s-complement integer"
Int(i32),
/// Integer in the range -(2^63) to 2^63-1 inclusive
///
/// encoding code = 0x81,
/// category = fixed, width = 8
/// label = "64-bit two’s-complement integer in network byte order"
///
/// encoding name = "smalllong", encoding code = 0x55
/// category = fixed, width = 1
/// label = "8-bit two’s-complement integer"
Long(i64),
/// 32-bit floating point number (IEEE 754-2008 binary32)
///
/// encoding name = "ieee-754", encoding code = 0x72
/// category = fixed, width = 4
/// label = "IEEE 754-2008 binary32"
Float(OrderedFloat<f32>),
/// 64-bit floating point number (IEEE 754-2008 binary64).
///
/// encoding name = "ieee-754", encoding code = 0x82
/// category = fixed, width = 8
/// label = "IEEE 754-2008 binary64"
Double(OrderedFloat<f64>),
/// 32-bit decimal number (IEEE 754-2008 decimal32).
///
/// encoding name = "ieee-754", encoding code = 0x74
/// category = fixed, width = 4
/// label = "IEEE 754-2008 decimal32 using the Binary Integer Decimal encoding"
Decimal32(Dec32),
/// 64-bit decimal number (IEEE 754-2008 decimal64).
///
/// encoding name = "ieee-754", encoding code = 0x84
/// category = fixed, width = 8
/// label = "IEEE 754-2008 decimal64 using the Binary Integer Decimal encoding"
Decimal64(Dec64),
/// 128-bit decimal number (IEEE 754-2008 decimal128).
///
/// encoding name = "ieee-754", encoding code = 0x94
/// category = fixed, width = 16
/// label = "IEEE 754-2008 decimal128 using the Binary Integer Decimal encoding"
Decimal128(Dec128),
/// A single Unicode character
///
/// encoding name = "utf32", encoding code = 0x73
/// category = fixed, width = 4,
/// label = "a UTF-32BE encoded Unicode character"
Char(char),
/// An absolute point in time
///
/// encoding name = "ms64", code = 0x83,
/// category = fixed, width = 8
/// label = "64-bit two’s-complement integer representing milliseconds since the unix epoch"
Timestamp(Timestamp),
/// A universally unique identifier as defined by RFC-4122 in section 4.1.2
///
/// encoding code = 0x98,
/// category = fixed, width = 16,
/// label="UUID as defined in section 4.1.2 of RFC-4122"
Uuid(Uuid),
/// A sequence of octets.
///
/// encoding name = "vbin8", encoding code = 0xa0
/// category = variable, width = 1
/// label = "up to 2^8 - 1 octets of binary data"
///
/// encoding name = "vbin32", encoding code = 0xb0,
/// category = variable, width = 4,
/// label="up to 2^32 - 1 octets of binary data"
Binary(ByteBuf),
/// A sequence of Unicode characters.
///
/// encoding name = "str8-utf8", encoding code = 0xa1,
/// category = variable, width = 1
/// label = "up to 2^8 - 1 octets worth of UTF-8 Unicode (with no byte order mark)"
///
/// encoding name = "str32-utf8", encoding code = 0xb1
/// category = variable, width = 4
/// label="up to 2^32 - 1 octets worth of UTF-8 Unicode (with no byte order mark)"
String(String),
/// Symbolic values from a constrained domain.
///
/// encoding name = "sym8", encoding code = 0xa3,
/// category = variable, width = 1
/// label="up to 2^8 - 1 seven bit ASCII characters representing a symbolic value"
///
/// encoding name = "sym32", encoding code = 0xb3
/// category = variable, width = 4
/// label="up to 2^32 - 1 seven bit ASCII characters representing a symbolic value"
///
/// Symbols are values from a constrained domain.
/// Although the set of possible domains is open-ended,
/// typically the both number and size of symbols in use for any
/// given application will be small, e.g. small enough that it is reasonable
/// to cache all the distinct values. Symbols are encoded as ASCII characters ASCII.
Symbol(Symbol),
/// A sequence of polymorphic values.
///
/// encoding name = "list0", encoding code = 0x45
/// category = fixed, width = 0,
/// label="the empty list (i.e. the list with no elements)"
///
/// encoding name = "list8", encoding code = 0xc0
/// category = compound, width = 1
/// label="up to 2^8 - 1 list elements with total size less than 2^8 octets
///
/// encoding name = "list32", encoding code = 0xd0
/// category = compound, width = 4
/// label="up to 2^32 - 1 list elements with total size less than 2^32 octets"
List(Vec<Value>),
/// A polymorphic mapping from distinct keys to values.
///
/// encoding name = "map8", encoding code = 0xc1,
/// category = compound, width = 1
/// label="up to 2^8 - 1 octets of encoded map data"
///
/// encoding name = "map32", encoding code = 0xd1,
/// category = compound, width = 4
/// label="up to 2^32 - 1 octets of encoded map data
///
/// Map encodings MUST contain an even number of items (i.e. an equal number of keys and values).
/// A map in which there exist two identical key values is invalid. Unless known to be otherwise,
/// maps MUST be considered to be ordered, that is, the order of the key-value pairs is semantically
/// important and two maps which are different only in the order in which their key-value pairs are
/// encoded are not equal.
///
/// Note: Can only use BTreeMap as it must be considered to be ordered
Map(OrderedMap<Value, Value>),
/// A sequence of values of a single type.
///
/// encoding name = "array8", encoding code = 0xe0
/// category = array, width = 1,
/// label="up to 2^8 - 1 array elements with total size less than 2^8 octets"
///
/// encoding name = "array32", encoding code = 0xf0,
/// category = array, width = 4
/// label="up to 2^32 - 1 array elements with total size less than 2^32 octets"
Array(Array<Value>),
}
impl Value {
/// Get the format code of the value type
pub fn format_code(&self) -> u8 {
let code = match *self {
Value::Described(_) => EncodingCodes::DescribedType,
Value::Null => EncodingCodes::Null,
Value::Bool(_) => EncodingCodes::Boolean,
Value::Ubyte(_) => EncodingCodes::Ubyte,
Value::Ushort(_) => EncodingCodes::Ushort,
Value::Uint(_) => EncodingCodes::Uint,
Value::Ulong(_) => EncodingCodes::Ulong,
Value::Byte(_) => EncodingCodes::Byte,
Value::Short(_) => EncodingCodes::Short,
Value::Int(_) => EncodingCodes::Int,
Value::Long(_) => EncodingCodes::Long,
Value::Float(_) => EncodingCodes::Float,
Value::Double(_) => EncodingCodes::Double,
Value::Decimal32(_) => EncodingCodes::Decimal32,
Value::Decimal64(_) => EncodingCodes::Decimal64,
Value::Decimal128(_) => EncodingCodes::Decimal128,
Value::Char(_) => EncodingCodes::Char,
Value::Timestamp(_) => EncodingCodes::Timestamp,
Value::Uuid(_) => EncodingCodes::Uuid,
Value::Binary(_) => EncodingCodes::Vbin32,
Value::String(_) => EncodingCodes::Str32,
Value::Symbol(_) => EncodingCodes::Sym32,
Value::List(_) => EncodingCodes::List32,
Value::Map(_) => EncodingCodes::Map32,
Value::Array(_) => EncodingCodes::Array32,
};
code as u8
}
/// Try to convert a serializable value to a Value
pub fn try_from_serializable<T: Serialize>(value: T) -> Result<Self, Error> {
use crate::value::ser::Serializer;
value.serialize(&mut Serializer::new())
}
}
macro_rules! impl_from_for_value {
($variant:ident, $variant_ty:ty) => {
impl From<$variant_ty> for Value {
fn from(val: $variant_ty) -> Self {
Self::$variant(val)
}
}
};
($($variant:ident, $variant_ty:ty),*) => {
$(impl_from_for_value!($variant, $variant_ty);)*
}
}
impl_from_for_value! {
Bool, bool,
Ubyte, u8,
Ushort, u16,
Uint, u32,
Ulong, u64,
Byte, i8,
Short, i16,
Int, i32,
Long, i64,
Float, OrderedFloat<f32>,
Double, OrderedFloat<f64>,
Decimal32, Dec32,
Decimal64, Dec64,
Decimal128, Dec128,
Char, char,
Timestamp, Timestamp,
Uuid, Uuid,
Binary, ByteBuf,
String, String,
Symbol, Symbol
}
impl<T> From<Described<T>> for Value
where
T: Into<Value>,
{
fn from(value: Described<T>) -> Self {
let described: Described<Value> = Described {
descriptor: value.descriptor,
value: value.value.into(),
};
Self::Described(Box::new(described))
}
}
impl From<f32> for Value {
fn from(val: f32) -> Self {
Self::Float(OrderedFloat::from(val))
}
}
impl From<f64> for Value {
fn from(val: f64) -> Self {
Self::Double(OrderedFloat::from(val))
}
}
impl From<&str> for Value {
fn from(val: &str) -> Self {
Self::String(val.to_string())
}
}
impl<T> From<Array<T>> for Value
where
T: Into<Value>,
{
fn from(values: Array<T>) -> Self {
let v = values.0.into_iter().map(Into::into).collect();
Self::Array(Array(v))
}
}
impl<T> From<Vec<T>> for Value
where
T: Into<Value>,
{
fn from(values: Vec<T>) -> Self {
Value::List(values.into_iter().map(Into::into).collect())
}
}
impl<K, V> From<BTreeMap<K, V>> for Value
where
K: Into<Value>,
V: Into<Value>,
{
fn from(map: BTreeMap<K, V>) -> Self {
let map: IndexMap<_, _> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
Value::Map(OrderedMap::from(map))
}
}
impl<K, V> From<OrderedMap<K, V>> for Value
where
K: Into<Value>,
V: Into<Value>,
{
fn from(map: OrderedMap<K, V>) -> Self {
let map = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
Value::Map(map)
}
}
macro_rules! impl_try_from_for_value_variant {
($variant:ident, $variant_ty:ty) => {
impl TryFrom<Value> for $variant_ty {
type Error = Value;
fn try_from(val: Value) -> Result<Self, Self::Error> {
match val {
Value::$variant(inner) => Ok(inner),
_ => Err(val)
}
}
}
};
($($variant:ident, $variant_ty:ty),*) => {
$(impl_try_from_for_value_variant!($variant, $variant_ty);)*
}
}
impl_try_from_for_value_variant! {
Bool, bool,
Ubyte, u8,
Ushort, u16,
Uint, u32,
Ulong, u64,
Byte, i8,
Short, i16,
Int, i32,
Long, i64,
Float, OrderedFloat<f32>,
Double, OrderedFloat<f64>,
Decimal32, Dec32,
Decimal64, Dec64,
Decimal128, Dec128,
Char, char,
Timestamp, Timestamp,
Uuid, Uuid,
Binary, ByteBuf,
String, String,
Symbol, Symbol
}
impl<T> TryFrom<Value> for Vec<T>
where
T: TryFrom<Value, Error = Value>,
{
type Error = Value;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::List(val) => val.into_iter().map(TryFrom::try_from).collect(),
_ => Err(value),
}
}
}
impl<T> TryFrom<Value> for Array<T>
where
T: TryFrom<Value, Error = Value>,
{
type Error = Value;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Array(arr) => arr.into_iter().map(TryFrom::try_from).collect(),
_ => Err(value),
}
}
}
impl TryFrom<Value> for f32 {
type Error = Value;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Float(val) => Ok(val.0),
_ => Err(value),
}
}
}
impl TryFrom<Value> for f64 {
type Error = Value;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Double(val) => Ok(val.0),
_ => Err(value),
}
}
}
impl<K, V> TryFrom<Value> for BTreeMap<K, V>
where
K: TryFrom<Value, Error = Value> + Ord,
V: TryFrom<Value, Error = Value>,
{
type Error = Value;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Map(map) => map
.into_iter()
.map(|(k, v)| match (K::try_from(k), V::try_from(v)) {
(Ok(k), Ok(v)) => Ok((k, v)),
(Err(err), _) => Err(err),
(_, Err(err)) => Err(err),
})
.collect(),
_ => Err(value),
}
}
}
impl<K, V> TryFrom<Value> for OrderedMap<K, V>
where
K: TryFrom<Value, Error = Value> + Hash + Eq,
V: TryFrom<Value, Error = Value>,
{
type Error = Value;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Map(map) => map
.into_iter()
.map(|(k, v)| match (K::try_from(k), V::try_from(v)) {
(Ok(k), Ok(v)) => Ok((k, v)),
(Err(err), _) => Err(err),
(_, Err(err)) => Err(err),
})
.collect(),
_ => Err(value),
}
}
}
impl<K, V> TryFrom<Value> for HashMap<K, V>
where
K: TryFrom<Value, Error = Value> + std::hash::Hash + Eq,
V: TryFrom<Value, Error = Value>,
{
type Error = Value;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Map(map) => map
.into_iter()
.map(|(k, v)| match (K::try_from(k), V::try_from(v)) {
(Ok(k), Ok(v)) => Ok((k, v)),
(Err(err), _) => Err(err),
(_, Err(err)) => Err(err),
})
.collect(),
_ => Err(value),
}
}
}
impl<K, V> TryFrom<Value> for IndexMap<K, V>
where
K: TryFrom<Value, Error = Value> + std::hash::Hash + Eq,
V: TryFrom<Value, Error = Value>,
{
type Error = Value;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Map(map) => map
.into_iter()
.map(|(k, v)| match (K::try_from(k), V::try_from(v)) {
(Ok(k), Ok(v)) => Ok((k, v)),
(Err(err), _) => Err(err),
(_, Err(err)) => Err(err),
})
.collect(),
_ => Err(value),
}
}
}
#[cfg(feature = "json")]
impl From<serde_json::Value> for Value {
fn from(value: serde_json::Value) -> Self {
match value {
serde_json::Value::Null => Value::Null,
serde_json::Value::Bool(b) => Value::Bool(b),
serde_json::Value::Number(n) => {
if n.is_i64() {
Value::Long(n.as_i64().expect("serde_json guaranteed this to be i64"))
} else if n.is_u64() {
Value::Ulong(n.as_u64().expect("serde_json guaranteed this to be u64"))
} else {
Value::Double(OrderedFloat(
n.as_f64().expect("serde_json guaranteed this to be f64"),
))
}
}
serde_json::Value::String(s) => Value::String(s),
serde_json::Value::Array(a) => {
let v: Vec<Value> = a.into_iter().map(Value::from).collect();
Value::List(v)
}
serde_json::Value::Object(o) => {
let map: IndexMap<_, _> = o
.into_iter()
.map(|(key, value)| (Value::String(key), Value::from(value)))
.collect();
Value::Map(OrderedMap::from(map))
}
}
}
}
#[cfg(test)]
mod tests {
use ordered_float::OrderedFloat;
use serde::de::DeserializeOwned;
use crate::de::from_reader;
use crate::primitives::OrderedMap;
use crate::ser::to_vec;
use super::Value;
fn assert_eq_from_reader_vs_expected<T>(buf: Vec<u8>, expected: T)
where
T: DeserializeOwned + std::fmt::Debug + PartialEq,
{
let deserialized: T = from_reader(buf.as_slice()).unwrap();
assert_eq!(deserialized, expected)
}
#[test]
fn mem_size_of_value() {
let size = std::mem::size_of::<Value>();
println!("{:?}", size);
}
#[test]
fn test_value_null() {
let expected = Value::Null;
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_bool() {
let expected = Value::Bool(true);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
let expected = Value::Bool(false);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_ubyte() {
let expected = Value::Ubyte(13);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_ushort() {
let expected = Value::Ushort(1313);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_uint() {
// uint0
let expected = Value::Uint(0);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
// smalluint
let expected = Value::Uint(255);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
// uint
let expected = Value::Uint(u32::MAX);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_ulong() {
// ulong0
let expected = Value::Ulong(0);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
// smallulong
let expected = Value::Ulong(255);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
// ulong
let expected = Value::Ulong(u64::MAX);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_byte() {
let expected = Value::Byte(13);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_short() {
let expected = Value::Short(1313);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_int() {
// smallint
let expected = Value::Int(0);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
let expected = Value::Int(255);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
// int
let expected = Value::Int(i32::MAX);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_long() {
// smalllong
let expected = Value::Long(0);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
let expected = Value::Long(255);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
// ulong
let expected = Value::Long(i64::MAX);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_float() {
let expected = Value::Float(OrderedFloat::from(1.313));
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_double() {
let expected = Value::Double(OrderedFloat::from(13.13));
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_decimal32() {
use crate::primitives::Dec32;
let expected = Value::Decimal32(Dec32::from([1, 2, 3, 4]));
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_decimal64() {
use crate::primitives::Dec64;
let expected = Value::Decimal64(Dec64::from([1, 2, 3, 4, 5, 6, 7, 8]));
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_decimal128() {
use crate::primitives::Dec128;
let expected = Value::Decimal128(Dec128::from([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
]));
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_char() {
let expected = Value::Char('a');
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_timestamp() {
use crate::primitives::Timestamp;
let expected = Value::Timestamp(Timestamp::from(13));
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_uuid() {
use crate::primitives::Uuid;
let expected = Value::Uuid(Uuid::from([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
]));
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_binary() {
use serde_bytes::ByteBuf;
let expected = Value::Binary(ByteBuf::from(vec![1, 2, 3, 4]));
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_string() {
let expected = Value::String(String::from("amqp"));
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_symbol() {
use crate::primitives::Symbol;
let expected = Value::Symbol(Symbol::from("amqp"));
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_list() {
let expected = Value::List([1u32, 2, 3, 4].iter().map(|v| Value::Uint(*v)).collect());
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_map() {
let mut map = OrderedMap::new();
map.insert(Value::Uint(13), Value::Bool(true));
map.insert(Value::Uint(45), Value::Bool(false));
let expected = Value::Map(map);
let buf = to_vec(&expected).unwrap();
assert_eq_from_reader_vs_expected(buf, expected);
}
#[test]
fn test_value_array() {
use crate::primitives::Array;
let vec: Vec<Value> = [1i32, 2, 3, 4].iter().map(|val| Value::Int(*val)).collect();
let arr = Array::from(vec);
let buf = to_vec(&arr).unwrap();
let expected = Value::Array(arr);
assert_eq_from_reader_vs_expected(buf, expected);
}
#[cfg(feature = "serde_amqp_derive")]
#[test]
fn test_deserialize_described_value() {
use crate as serde_amqp;
use crate::from_slice;
use crate::macros::SerializeComposite;
#[derive(Debug, SerializeComposite)]
#[amqp_contract(code = "0x00:0x13", encoding = "list")]
struct Foo {
is_fool: bool,
a: i32,
}
let foo = Foo {
is_fool: true,
a: 9,
};
let buf = to_vec(&foo).unwrap();
println!("{:x?}", buf);
let value: Value = from_slice(&buf).unwrap();
println!("{:?}", value);
}
}