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

use amq_protocol_types::*;
use serde_json::from_str;

use std::collections::BTreeMap;

/// Structure holding the definition of the protocol
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct AMQProtocolDefinition {
    /// The name of the protocol
    pub name:          ShortString,
    /// The major protocol version
    pub major_version: ShortShortUInt,
    /// The minor protocol version
    pub minor_version: ShortShortUInt,
    /// The revision of the protocol version
    pub revision:      ShortShortUInt,
    /// The default port of the protocol
    pub port:          LongUInt,
    /// The copyright holder of the protocol specification
    pub copyright:     LongString,
    /// The domains defined by the protocol specification
    pub domains:       BTreeMap<ShortString, AMQPType>,
    /// The constants defined by the protocol specification
    pub constants:     Vec<AMQPConstant>,
    /// The soft errors defined by the protocol specification
    pub soft_errors:   Vec<AMQPConstant>,
    /// The hard errors defined by the protocol specification
    pub hard_errors:   Vec<AMQPConstant>,
    /// The classes defined by the protocol specification
    pub classes:       Vec<AMQPClass>,
}

impl AMQProtocolDefinition {
    /// Load protocol definition from reference specification
    pub fn load() -> AMQProtocolDefinition {
        let specs = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/specs/amqp-rabbitmq-0.9.1.json"));

        from_str::<_AMQProtocolDefinition>(specs).expect("Failed to parse AMQP specs file").into_specs()
    }
}

/// A constant as defined in the AMQP specification
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct AMQPConstant {
    /// The name of the constant
    pub name:      ShortString,
    /// The value of the constant
    pub value:     ShortUInt,
    /// The type of the constant
    #[serde(rename="type")]
    pub amqp_type: AMQPType,
}

/// A class as defined in the AMQP specification
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct AMQPClass {
    /// The id of the class
    pub id:             ShortUInt,
    /// The methods of the class
    pub methods:        Vec<AMQPMethod>,
    /// The name of the class
    pub name:           ShortString,
    /// The properties of the class
    pub properties:     Vec<AMQPProperty>,
}

/// A method as defined in the AMQP specification
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct AMQPMethod {
    /// The id of the method
    pub id:            ShortUInt,
    /// The arguments of the method
    pub arguments:     Vec<AMQPArgument>,
    /// The name of the method
    pub name:          ShortString,
    /// Whether this method is synchronous or not
    pub synchronous:   Boolean,
}

/// An argument as defined in the AMQP specification
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum AMQPArgument {
    /// The argument is holding a value
    Value(AMQPValueArgument),
    /// The argument is holding flags
    Flags(Vec<AMQPFlagArgument>),
}

/// An argument holding a value as defined in the AMQP specification
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct AMQPValueArgument {
    /// The type of the argument's value
    #[serde(rename="type")]
    pub amqp_type:     AMQPType,
    /// The name of the argument's value
    pub name:          ShortString,
    /// The default value of the argument's value
    pub default_value: Option<AMQPValue>,
    /// The domain of the argument's value
    pub domain:        Option<ShortString>,
}

/// An argument holding flags as defined in the AMQP specification
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct AMQPFlagArgument {
    /// The name of the flag
    pub name:          ShortString,
    /// The default value for the flag
    pub default_value: Boolean,
}

/// A property as defined in the AMQP specification
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct AMQPProperty {
    /// The type of the property
    #[serde(rename="type")]
    pub amqp_type: AMQPType,
    /// The name of the property
    pub name:      ShortString,
}