strut_rabbitmq/util/
amqp_value.rs1use lapin::types::AMQPValue;
2
3pub trait IsEmpty {
6 fn is_empty(&self) -> bool;
10}
11
12impl IsEmpty for AMQPValue {
13 fn is_empty(&self) -> bool {
14 match self {
15 AMQPValue::ShortString(s) => s.as_str().is_empty(),
16 AMQPValue::LongString(s) => s.as_bytes().is_empty(),
17 AMQPValue::FieldArray(a) => a.as_slice().is_empty(),
18 AMQPValue::FieldTable(t) => t.inner().is_empty(),
19 AMQPValue::ByteArray(a) => a.as_slice().is_empty(),
20 AMQPValue::Void => true,
21 _ => false,
22 }
23 }
24}
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29 use crate::util::coerce::Coerce;
30 use crate::util::field_table::push::Push;
31 use crate::util::morph::Morph;
32 use lapin::types::{DecimalValue, FieldTable};
33 use pretty_assertions::assert_eq;
34
35 #[test]
36 fn from_methods() {
37 assert_eq!(AMQPValue::morph(true), AMQPValue::Boolean(true));
38 assert_eq!(
39 AMQPValue::morph("test"),
40 AMQPValue::LongString("test".as_bytes().into()),
41 );
42 assert_eq!(
43 AMQPValue::morph("test".to_string()),
44 AMQPValue::LongString("test".as_bytes().into()),
45 );
46 assert_eq!(AMQPValue::morph(i8::MIN), AMQPValue::ShortShortInt(i8::MIN));
47 assert_eq!(AMQPValue::morph(i16::MIN), AMQPValue::ShortInt(i16::MIN));
48 assert_eq!(AMQPValue::morph(i32::MIN), AMQPValue::LongInt(i32::MIN));
49 assert_eq!(AMQPValue::morph(i64::MIN), AMQPValue::LongLongInt(i64::MIN));
50 assert_eq!(
51 AMQPValue::morph(u8::MAX),
52 AMQPValue::ShortShortUInt(u8::MAX),
53 );
54 assert_eq!(AMQPValue::morph(u16::MAX), AMQPValue::ShortUInt(u16::MAX));
55 assert_eq!(AMQPValue::morph(u32::MAX), AMQPValue::LongUInt(u32::MAX));
56 assert_eq!(AMQPValue::morph(u64::MAX), AMQPValue::Timestamp(u64::MAX));
57 }
58
59 #[test]
60 fn coerce_to_bool() {
61 assert_eq!(AMQPValue::Boolean(true).coerce(), Some(true));
62 assert_eq!(AMQPValue::Boolean(false).coerce(), Some(false));
63 assert_eq!(AMQPValue::ShortShortUInt(1).coerce(), Some(true));
64 assert_eq!(AMQPValue::ShortShortUInt(0).coerce(), Some(false));
65 assert_eq!(AMQPValue::ShortString("true".into()).coerce(), Some(true),);
66 assert_eq!(AMQPValue::ShortString("false".into()).coerce(), Some(false),);
67 }
68
69 #[test]
70 fn coerce_to_string() {
71 assert_eq!(
72 AMQPValue::Boolean(true).coerce(),
73 Some::<String>("true".into()),
74 );
75 assert_eq!(
76 AMQPValue::ShortShortUInt(42).coerce(),
77 Some::<String>("42".into()),
78 );
79 assert_eq!(
80 AMQPValue::ShortString("hello".into()).coerce(),
81 Some::<String>("hello".into()),
82 );
83 }
84
85 #[test]
86 fn coerce_to_integers() {
87 assert_eq!(AMQPValue::ShortShortUInt(42).coerce(), Some::<i8>(42));
88 assert_eq!(AMQPValue::ShortInt(300).coerce(), Some::<i16>(300));
89 assert_eq!(AMQPValue::LongInt(-123456).coerce(), Some::<i32>(-123456));
90 assert_eq!(AMQPValue::LongLongInt(i64::MAX).coerce(), Some(i64::MAX),);
91 assert_eq!(AMQPValue::ShortShortInt(-42).coerce(), None::<u8>);
92 }
93
94 #[test]
95 fn coerce_to_unsigned_integers() {
96 assert_eq!(AMQPValue::ShortShortUInt(42).coerce(), Some::<u8>(42));
97 assert_eq!(AMQPValue::ShortUInt(300).coerce(), Some::<u16>(300));
98 assert_eq!(AMQPValue::LongUInt(123456).coerce(), Some::<u32>(123456));
99 assert_eq!(
100 AMQPValue::Timestamp(i64::MAX as u64).coerce(),
101 Some(i64::MAX as u64),
102 );
103 }
104
105 #[test]
106 fn coerce_to_usize_isize() {
107 assert_eq!(AMQPValue::ShortShortUInt(42).coerce(), Some::<usize>(42));
108 assert_eq!(AMQPValue::ShortShortInt(-42).coerce(), Some::<isize>(-42));
109 }
110
111 #[test]
112 fn invalid_coercions() {
113 assert_eq!(AMQPValue::Boolean(true).coerce(), None::<i32>);
114 assert_eq!(AMQPValue::Boolean(false).coerce(), None::<u64>);
115 assert_eq!(
116 AMQPValue::ShortString("not a number".into()).coerce(),
117 None::<i32>,
118 );
119 }
120
121 #[test]
122 fn is_empty() {
123 assert_eq!(AMQPValue::Boolean(false).is_empty(), false);
124 assert_eq!(AMQPValue::ShortShortInt(0).is_empty(), false);
125 assert_eq!(AMQPValue::ShortShortUInt(0).is_empty(), false);
126 assert_eq!(AMQPValue::ShortInt(0).is_empty(), false);
127 assert_eq!(AMQPValue::ShortUInt(0).is_empty(), false);
128 assert_eq!(AMQPValue::LongInt(0).is_empty(), false);
129 assert_eq!(AMQPValue::LongUInt(0).is_empty(), false);
130 assert_eq!(AMQPValue::LongLongInt(0).is_empty(), false);
131 assert_eq!(AMQPValue::Float(0.0).is_empty(), false);
132 assert_eq!(AMQPValue::Double(0.0).is_empty(), false);
133 assert_eq!(
134 AMQPValue::DecimalValue(DecimalValue { scale: 0, value: 0 }).is_empty(),
135 false,
136 );
137 assert_eq!(AMQPValue::Timestamp(0).is_empty(), false);
138
139 assert_eq!(AMQPValue::ShortString("".into()).is_empty(), true);
140 assert_eq!(AMQPValue::ShortString(" ".into()).is_empty(), false);
141
142 assert_eq!(AMQPValue::LongString("".into()).is_empty(), true);
143 assert_eq!(AMQPValue::LongString(" ".into()).is_empty(), false);
144
145 assert_eq!(AMQPValue::FieldArray(vec![].into()).is_empty(), true);
146 assert_eq!(
147 AMQPValue::FieldArray(vec![AMQPValue::ShortString("".into())].into()).is_empty(),
148 false,
149 );
150
151 assert_eq!(
152 AMQPValue::FieldTable(FieldTable::default()).is_empty(),
153 true,
154 );
155 let mut table = FieldTable::default();
156 table.push("", 0);
157 assert_eq!(AMQPValue::FieldTable(table).is_empty(), false);
158
159 assert_eq!(AMQPValue::ByteArray(vec![].into()).is_empty(), true);
160 assert_eq!(AMQPValue::ByteArray(vec![0].into()).is_empty(), false);
161
162 assert_eq!(AMQPValue::Void.is_empty(), true);
163 }
164}