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
use crate::types::*;

use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Enumeration referencing the possible AMQP values depending on the types
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum AMQPValue {
    /// A bool
    Boolean(Boolean),
    /// An i8
    ShortShortInt(ShortShortInt),
    /// A u8
    ShortShortUInt(ShortShortUInt),
    /// An i16
    ShortInt(ShortInt),
    /// A u16
    ShortUInt(ShortUInt),
    /// An i32
    LongInt(LongInt),
    /// A u32
    LongUInt(LongUInt),
    /// An i64
    LongLongInt(LongLongInt),
    /// An f32
    Float(Float),
    /// An f64
    Double(Double),
    /// A decimal value
    DecimalValue(DecimalValue),
    /// A String (deprecated)
    ShortString(ShortString),
    /// A String
    LongString(LongString),
    /// An array of AMQPValue
    FieldArray(FieldArray),
    /// A timestamp (u64)
    Timestamp(Timestamp),
    /// A Map<String, AMQPValue>
    FieldTable(FieldTable),
    /// An array of bytes (RabbitMQ specific)
    ByteArray(ByteArray),
    /// No value
    Void,
}

impl AMQPValue {
    /// Get the AMQPType of an AMQPValue
    pub fn get_type(&self) -> AMQPType {
        match *self {
            AMQPValue::Boolean(_) => AMQPType::Boolean,
            AMQPValue::ShortShortInt(_) => AMQPType::ShortShortInt,
            AMQPValue::ShortShortUInt(_) => AMQPType::ShortShortUInt,
            AMQPValue::ShortInt(_) => AMQPType::ShortInt,
            AMQPValue::ShortUInt(_) => AMQPType::ShortUInt,
            AMQPValue::LongInt(_) => AMQPType::LongInt,
            AMQPValue::LongUInt(_) => AMQPType::LongUInt,
            AMQPValue::LongLongInt(_) => AMQPType::LongLongInt,
            AMQPValue::Float(_) => AMQPType::Float,
            AMQPValue::Double(_) => AMQPType::Double,
            AMQPValue::DecimalValue(_) => AMQPType::DecimalValue,
            AMQPValue::ShortString(_) => AMQPType::ShortString,
            AMQPValue::LongString(_) => AMQPType::LongString,
            AMQPValue::FieldArray(_) => AMQPType::FieldArray,
            AMQPValue::Timestamp(_) => AMQPType::Timestamp,
            AMQPValue::FieldTable(_) => AMQPType::FieldTable,
            AMQPValue::ByteArray(_) => AMQPType::ByteArray,
            AMQPValue::Void => AMQPType::Void,
        }
    }

    /// Convert a serde_json::Value into an AMQPValue
    pub fn try_from(value: &Value, amqp_type: AMQPType) -> Option<AMQPValue> {
        match amqp_type {
            AMQPType::Boolean => value.as_bool().map(AMQPValue::Boolean),
            AMQPType::ShortShortInt => value
                .as_i64()
                .map(|i| AMQPValue::ShortShortInt(i as ShortShortInt)),
            AMQPType::ShortShortUInt => value
                .as_u64()
                .map(|u| AMQPValue::ShortShortUInt(u as ShortShortUInt)),
            AMQPType::ShortInt => value.as_i64().map(|i| AMQPValue::ShortInt(i as ShortInt)),
            AMQPType::ShortUInt => value.as_u64().map(|u| AMQPValue::ShortUInt(u as ShortUInt)),
            AMQPType::LongInt => value.as_i64().map(|i| AMQPValue::LongInt(i as LongInt)),
            AMQPType::LongUInt => value.as_u64().map(|u| AMQPValue::LongUInt(u as LongUInt)),
            AMQPType::LongLongInt => value
                .as_i64()
                .map(|i| AMQPValue::LongLongInt(i as LongLongInt)),
            AMQPType::LongLongUInt => value
                .as_i64()
                .map(|i| AMQPValue::LongLongInt(i as LongLongInt)), /* Not a typo; AMQPValue::LongLongUInt doesn't exist */
            AMQPType::Float => value.as_f64().map(|i| AMQPValue::Float(i as Float)),
            AMQPType::Double => value.as_f64().map(|i| AMQPValue::Double(i as Double)),
            AMQPType::DecimalValue => None,
            AMQPType::ShortString => value
                .as_str()
                .map(ShortString::from)
                .map(AMQPValue::ShortString),
            AMQPType::LongString => value
                .as_str()
                .map(LongString::from)
                .map(AMQPValue::LongString),
            AMQPType::FieldArray => None,
            AMQPType::Timestamp => value.as_u64().map(|t| AMQPValue::Timestamp(t as Timestamp)),
            AMQPType::FieldTable => None,
            AMQPType::ByteArray => None,
            AMQPType::Void => value.as_null().map(|_| AMQPValue::Void),
        }
    }

    /// If the value is bool, returns associated value. Returns None otherwise.
    pub fn as_bool(&self) -> Option<Boolean> {
        match self {
            AMQPValue::Boolean(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is ShortShortInt, returns associated value. Returns None otherwise.
    pub fn as_short_short_int(&self) -> Option<ShortShortInt> {
        match self {
            AMQPValue::ShortShortInt(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is ShortShortUInt, returns associated value. Returns None otherwise.
    pub fn as_short_short_uint(&self) -> Option<ShortShortUInt> {
        match self {
            AMQPValue::ShortShortUInt(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is ShortInt, returns associated value. Returns None otherwise.
    pub fn as_short_int(&self) -> Option<ShortInt> {
        match self {
            AMQPValue::ShortInt(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is ShortUInt, returns associated value. Returns None otherwise.
    pub fn as_short_uint(&self) -> Option<ShortUInt> {
        match self {
            AMQPValue::ShortUInt(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is LongInt, returns associated value. Returns None otherwise.
    pub fn as_long_int(&self) -> Option<LongInt> {
        match self {
            AMQPValue::LongInt(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is LongUInt, returns associated value. Returns None otherwise.
    pub fn as_long_uint(&self) -> Option<LongUInt> {
        match self {
            AMQPValue::LongUInt(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is LongLongInt, returns associated value. Returns None otherwise.
    pub fn as_long_long_int(&self) -> Option<LongLongInt> {
        match self {
            AMQPValue::LongLongInt(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is Float, returns associated value. Returns None otherwise.
    pub fn as_float(&self) -> Option<Float> {
        match self {
            AMQPValue::Float(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is Double, returns associated value. Returns None otherwise.
    pub fn as_double(&self) -> Option<Double> {
        match self {
            AMQPValue::Double(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is DecimalValue, returns associated value. Returns None otherwise.
    pub fn as_decimal_value(&self) -> Option<DecimalValue> {
        match self {
            AMQPValue::DecimalValue(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is ShortString, returns associated value as str. Returns None otherwise.
    pub fn as_short_string(&self) -> Option<&ShortString> {
        match self {
            AMQPValue::ShortString(value) => Some(value),
            _ => None,
        }
    }

    /// If the value is LongString, returns associated value as bytes. Returns None otherwise.
    pub fn as_long_string(&self) -> Option<&LongString> {
        match self {
            AMQPValue::LongString(value) => Some(value),
            _ => None,
        }
    }

    /// If the value is FieldArray, returns associated value. Returns None otherwise.
    pub fn as_array(&self) -> Option<&FieldArray> {
        match self {
            AMQPValue::FieldArray(value) => Some(value),
            _ => None,
        }
    }

    /// If the value is Timestamp, returns associated value. Returns None otherwise.
    pub fn as_timestamp(&self) -> Option<Timestamp> {
        match self {
            AMQPValue::Timestamp(value) => Some(*value),
            _ => None,
        }
    }

    /// If the value is FieldTable, returns associated value. Returns None otherwise.
    pub fn as_field_table(&self) -> Option<&FieldTable> {
        match self {
            AMQPValue::FieldTable(value) => Some(value),
            _ => None,
        }
    }

    /// If the value is ByteArray, returns associated value. Returns None otherwise.
    pub fn as_byte_array(&self) -> Option<&ByteArray> {
        match self {
            AMQPValue::ByteArray(value) => Some(value),
            _ => None,
        }
    }

    /// Returns true if value is Void.
    pub fn as_void(&self) -> Option<()> {
        match self {
            AMQPValue::Void => Some(()),
            _ => None,
        }
    }
}

impl From<Boolean> for AMQPValue {
    fn from(v: Boolean) -> Self {
        AMQPValue::Boolean(v)
    }
}

impl From<ShortShortInt> for AMQPValue {
    fn from(v: ShortShortInt) -> Self {
        AMQPValue::ShortShortInt(v)
    }
}

impl From<ShortShortUInt> for AMQPValue {
    fn from(v: ShortShortUInt) -> Self {
        AMQPValue::ShortShortUInt(v)
    }
}

impl From<ShortInt> for AMQPValue {
    fn from(v: ShortInt) -> Self {
        AMQPValue::ShortInt(v)
    }
}

impl From<ShortUInt> for AMQPValue {
    fn from(v: ShortUInt) -> Self {
        AMQPValue::ShortUInt(v)
    }
}

impl From<LongInt> for AMQPValue {
    fn from(v: LongInt) -> Self {
        AMQPValue::LongInt(v)
    }
}

impl From<LongUInt> for AMQPValue {
    fn from(v: LongUInt) -> Self {
        AMQPValue::LongUInt(v)
    }
}

impl From<LongLongInt> for AMQPValue {
    fn from(v: LongLongInt) -> Self {
        AMQPValue::LongLongInt(v)
    }
}

impl From<Float> for AMQPValue {
    fn from(v: Float) -> Self {
        AMQPValue::Float(v)
    }
}

impl From<Double> for AMQPValue {
    fn from(v: Double) -> Self {
        AMQPValue::Double(v)
    }
}

impl From<DecimalValue> for AMQPValue {
    fn from(v: DecimalValue) -> Self {
        AMQPValue::DecimalValue(v)
    }
}

impl From<ShortString> for AMQPValue {
    fn from(v: ShortString) -> Self {
        AMQPValue::ShortString(v)
    }
}

impl From<LongString> for AMQPValue {
    fn from(v: LongString) -> Self {
        AMQPValue::LongString(v)
    }
}

impl From<FieldArray> for AMQPValue {
    fn from(v: FieldArray) -> Self {
        AMQPValue::FieldArray(v)
    }
}

impl From<Timestamp> for AMQPValue {
    fn from(v: Timestamp) -> Self {
        AMQPValue::Timestamp(v)
    }
}

impl From<FieldTable> for AMQPValue {
    fn from(v: FieldTable) -> Self {
        AMQPValue::FieldTable(v)
    }
}

impl From<ByteArray> for AMQPValue {
    fn from(v: ByteArray) -> Self {
        AMQPValue::ByteArray(v)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use serde_json::Number;

    #[test]
    fn test_from_bool_value() {
        assert_eq!(
            AMQPValue::try_from(&Value::Bool(false), AMQPType::Boolean),
            Some(AMQPValue::Boolean(false))
        );
        assert_eq!(
            AMQPValue::try_from(&Value::Bool(true), AMQPType::Boolean),
            Some(AMQPValue::Boolean(true))
        );
    }

    #[test]
    fn test_from_number_value() {
        assert_eq!(
            AMQPValue::try_from(&Value::Number(Number::from(42)), AMQPType::LongLongUInt),
            Some(AMQPValue::LongLongInt(42))
        );
        assert_eq!(
            AMQPValue::try_from(&Value::Number(Number::from(-42)), AMQPType::LongLongInt),
            Some(AMQPValue::LongLongInt(-42))
        );
        assert_eq!(
            AMQPValue::try_from(
                &Value::Number(Number::from_f64(42.42).unwrap()),
                AMQPType::Double
            ),
            Some(AMQPValue::Double(42.42))
        );
    }

    #[test]
    fn test_from_string_value() {
        assert_eq!(
            AMQPValue::try_from(&Value::String(String::new()), AMQPType::LongString),
            Some(AMQPValue::LongString(LongString::default()))
        );
        assert_eq!(
            AMQPValue::try_from(&Value::String("test".to_string()), AMQPType::LongString),
            Some(AMQPValue::LongString("test".into()))
        );
    }

    #[test]
    fn test_from_null_value() {
        assert_eq!(
            AMQPValue::try_from(&Value::Null, AMQPType::Void),
            Some(AMQPValue::Void)
        );
    }
}