use crate::value::AMQPValue;
use std::{
borrow,
collections::{btree_map, BTreeMap},
fmt, str,
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum AMQPType {
Boolean,
ShortShortInt,
ShortShortUInt,
ShortInt,
ShortUInt,
LongInt,
LongUInt,
LongLongInt,
LongLongUInt,
Float,
Double,
DecimalValue,
ShortString,
LongString,
FieldArray,
Timestamp,
FieldTable,
ByteArray,
Void,
}
impl AMQPType {
pub fn from_id(id: char) -> Option<AMQPType> {
match id {
't' => Some(AMQPType::Boolean),
'b' => Some(AMQPType::ShortShortInt),
'B' => Some(AMQPType::ShortShortUInt),
's' | 'U' => Some(AMQPType::ShortInt),
'u' => Some(AMQPType::ShortUInt),
'I' => Some(AMQPType::LongInt),
'i' => Some(AMQPType::LongUInt),
'L' | 'l' => Some(AMQPType::LongLongInt),
'f' => Some(AMQPType::Float),
'd' => Some(AMQPType::Double),
'D' => Some(AMQPType::DecimalValue),
'S' => Some(AMQPType::LongString),
'A' => Some(AMQPType::FieldArray),
'T' => Some(AMQPType::Timestamp),
'F' => Some(AMQPType::FieldTable),
'x' => Some(AMQPType::ByteArray),
'V' => Some(AMQPType::Void),
_ => None,
}
}
pub fn get_id(self) -> char {
match self {
AMQPType::Boolean => 't',
AMQPType::ShortShortInt => 'b',
AMQPType::ShortShortUInt => 'B',
AMQPType::ShortInt => 's',
AMQPType::ShortUInt => 'u',
AMQPType::LongInt => 'I',
AMQPType::LongUInt => 'i',
AMQPType::LongLongInt | AMQPType::LongLongUInt => 'l',
AMQPType::Float => 'f',
AMQPType::Double => 'd',
AMQPType::DecimalValue => 'D',
AMQPType::ShortString => '_',
AMQPType::LongString => 'S',
AMQPType::FieldArray => 'A',
AMQPType::Timestamp => 'T',
AMQPType::FieldTable => 'F',
AMQPType::ByteArray => 'x',
AMQPType::Void => 'V',
}
}
}
impl fmt::Display for AMQPType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
pub type Boolean = bool;
pub type ShortShortInt = i8;
pub type ShortShortUInt = u8;
pub type ShortInt = i16;
pub type ShortUInt = u16;
pub type LongInt = i32;
pub type LongUInt = u32;
pub type LongLongInt = i64;
pub type LongLongUInt = u64;
pub type Float = f32;
pub type Double = f64;
pub type Timestamp = LongLongUInt;
pub type Void = ();
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
pub struct ShortString(String);
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
pub struct LongString(String);
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
pub struct FieldArray(Vec<AMQPValue>);
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
pub struct FieldTable(BTreeMap<ShortString, AMQPValue>);
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct ByteArray(Vec<u8>);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct DecimalValue {
pub scale: ShortShortUInt,
pub value: LongUInt,
}
impl<'a> ShortString {
pub fn as_str(&'a self) -> &'a str {
self.0.as_str()
}
pub fn split_whitespace(&'a self) -> str::SplitWhitespace<'a> {
self.0.split_whitespace()
}
}
impl From<String> for ShortString {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for ShortString {
fn from(s: &str) -> Self {
s.to_owned().into()
}
}
impl borrow::Borrow<str> for ShortString {
fn borrow(&self) -> &str {
self.0.borrow()
}
}
impl fmt::Display for ShortString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl<'a> LongString {
pub fn as_str(&'a self) -> &'a str {
self.0.as_str()
}
pub fn split_whitespace(&'a self) -> str::SplitWhitespace<'a> {
self.0.split_whitespace()
}
}
impl From<String> for LongString {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for LongString {
fn from(s: &str) -> Self {
s.to_owned().into()
}
}
impl borrow::Borrow<str> for LongString {
fn borrow(&self) -> &str {
self.0.borrow()
}
}
impl fmt::Display for LongString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl FieldArray {
pub fn as_slice(&self) -> &[AMQPValue] {
self.0.as_slice()
}
pub fn push(&mut self, v: AMQPValue) {
self.0.push(v);
}
}
impl From<Vec<AMQPValue>> for FieldArray {
fn from(v: Vec<AMQPValue>) -> Self {
Self(v)
}
}
impl FieldTable {
pub fn insert(&mut self, k: ShortString, v: AMQPValue) {
self.0.insert(k, v);
}
pub fn contains_key(&self, k: &str) -> bool {
self.0.contains_key(k)
}
pub fn inner(&self) -> &BTreeMap<ShortString, AMQPValue> {
&self.0
}
}
impl<'a> IntoIterator for &'a FieldTable {
type Item = (&'a ShortString, &'a AMQPValue);
type IntoIter = btree_map::Iter<'a, ShortString, AMQPValue>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl From<BTreeMap<ShortString, AMQPValue>> for FieldTable {
fn from(m: BTreeMap<ShortString, AMQPValue>) -> Self {
Self(m)
}
}
impl ByteArray {
pub fn as_slice(&self) -> &[u8] {
self.0.as_slice()
}
pub fn len(&self) -> usize {
self.0.len()
}
}
impl From<Vec<u8>> for ByteArray {
fn from(v: Vec<u8>) -> Self {
Self(v)
}
}
impl From<&[u8]> for ByteArray {
fn from(v: &[u8]) -> Self {
Self(v.to_vec())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_type_from_id() {
assert_eq!(AMQPType::from_id('T'), Some(AMQPType::Timestamp));
assert_eq!(AMQPType::from_id('S'), Some(AMQPType::LongString));
assert_eq!(AMQPType::from_id('s'), Some(AMQPType::ShortInt));
assert_eq!(AMQPType::from_id('U'), Some(AMQPType::ShortInt));
assert_eq!(AMQPType::from_id('l'), Some(AMQPType::LongLongInt));
assert_eq!(AMQPType::from_id('z'), None);
}
#[test]
fn test_type_get_id() {
assert_eq!(AMQPType::LongLongInt.get_id(), 'l');
assert_eq!(AMQPType::LongLongUInt.get_id(), 'l');
assert_eq!(AMQPType::ShortString.get_id(), '_');
}
#[test]
fn test_type_to_string() {
assert_eq!(AMQPType::Boolean.to_string(), "Boolean");
assert_eq!(AMQPType::Void.to_string(), "Void");
}
}