amqpr_codec/
args.rs

1use std::collections::HashMap;
2use std::ops::Deref;
3use std::convert::From;
4
5use bytes::Bytes;
6
7
8#[derive(PartialEq, Clone, Debug)]
9pub enum FieldArgument {
10    Boolean(bool),
11    SignedOctet(i8),
12    UnsignedOctet(u8),
13    SignedShort(i16),
14    UnsignedShort(u16),
15    SignedLong(i32),
16    UnsignedLong(u32),
17    SignedLongLong(i64),
18    UnsignedLongLong(u64),
19    Float(f32),
20    Double(f64),
21    Decimal(i64), // For now, we do not handle big number which is bigger than max of i64.
22    ShortString(AmqpString),
23    LongString(AmqpString),
24    // Array(), // I can not find any definition of this field.
25    Timestamp(u64),
26    NestedTable(HashMap<AmqpString, FieldArgument>),
27    Void,
28    ByteArray(Vec<u8>), // How can we treat it
29}
30
31
32
33/// String being able to do Zero-cost conversion from `Bytes` or `&'static str`.
34#[derive(Debug, Clone, PartialEq, Eq, Hash)]
35pub struct AmqpString(pub(crate) Bytes);
36
37
38impl Deref for AmqpString {
39    type Target = str;
40
41    fn deref(&self) -> &str {
42        ::std::str::from_utf8(self.0.as_ref()).expect("Non Utf-8 bytes")
43    }
44}
45
46
47impl From<Vec<u8>> for AmqpString {
48    fn from(bytes: Vec<u8>) -> AmqpString {
49        AmqpString(Bytes::from(bytes))
50    }
51}
52
53impl From<String> for AmqpString {
54    fn from(bytes: String) -> AmqpString {
55        AmqpString(Bytes::from(bytes))
56    }
57}
58
59impl<'a> From<&'a [u8]> for AmqpString {
60    fn from(bytes: &'a [u8]) -> AmqpString {
61        AmqpString(Bytes::from(bytes))
62    }
63}
64
65impl From<&'static str> for AmqpString {
66    fn from(bytes: &'static str) -> AmqpString {
67        AmqpString(Bytes::from_static(bytes.as_bytes()))
68    }
69}