amq_protocol_codegen/
specs.rs

1use crate::internal::*;
2
3use amq_protocol_types::*;
4use serde::{Deserialize, Serialize};
5use serde_json::{from_str, Value};
6
7use std::collections::BTreeMap;
8
9/// Structure holding the definition of the protocol
10#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
11pub struct AMQProtocolDefinition {
12    /// The name of the protocol
13    pub name: String,
14    /// The major protocol version
15    pub major_version: ShortShortUInt,
16    /// The minor protocol version
17    pub minor_version: ShortShortUInt,
18    /// The revision of the protocol version
19    pub revision: ShortShortUInt,
20    /// The default port of the protocol
21    pub port: LongUInt,
22    /// The copyright holder of the protocol specification
23    pub copyright: String,
24    /// The domains defined by the protocol specification
25    pub domains: BTreeMap<String, AMQPType>,
26    /// The constants defined by the protocol specification
27    pub constants: Vec<AMQPConstant>,
28    /// The soft errors defined by the protocol specification
29    pub soft_errors: Vec<AMQPConstant>,
30    /// The hard errors defined by the protocol specification
31    pub hard_errors: Vec<AMQPConstant>,
32    /// The classes defined by the protocol specification
33    pub classes: Vec<AMQPClass>,
34}
35
36impl AMQProtocolDefinition {
37    /// Load protocol definition from reference specification
38    pub fn load(metadata: Option<Value>) -> AMQProtocolDefinition {
39        let specs = include_str!(concat!(
40            env!("CARGO_MANIFEST_DIR"),
41            "/specs/amqp-rabbitmq-0.9.1.json"
42        ));
43
44        from_str::<_AMQProtocolDefinition>(specs)
45            .expect("Failed to parse AMQP specs file")
46            .into_specs(&metadata.unwrap_or_default())
47    }
48}
49
50/// A constant as defined in the AMQP specification
51#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
52pub struct AMQPConstant {
53    /// The name of the constant
54    pub name: String,
55    /// The value of the constant
56    pub value: LongUInt,
57    /// The type of the constant
58    #[serde(rename = "type")]
59    pub amqp_type: AMQPType,
60}
61
62/// A class as defined in the AMQP specification
63#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
64pub struct AMQPClass {
65    /// The id of the class
66    pub id: Identifier,
67    /// The methods of the class
68    pub methods: Vec<AMQPMethod>,
69    /// The name of the class
70    pub name: String,
71    /// The properties of the class
72    pub properties: Vec<AMQPProperty>,
73    /// Extra metadata for code generation
74    pub metadata: Value,
75}
76
77/// A method as defined in the AMQP specification
78#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
79pub struct AMQPMethod {
80    /// The id of the method
81    pub id: Identifier,
82    /// The arguments of the method
83    pub arguments: Vec<AMQPArgument>,
84    /// The name of the method
85    pub name: String,
86    /// Whether this method is synchronous or not
87    pub synchronous: Boolean,
88    /// Whether this method carries some content frames with it
89    pub content: Boolean,
90    /// Extra metadata for code generation
91    pub metadata: Value,
92    /// Whether this method is a reply or not
93    pub is_reply: bool,
94    /// Whether all the arguments have force_default or not
95    pub ignore_args: bool,
96    /// Whether this method can be sent from client to server
97    pub c2s: bool,
98    /// Whether this method can be received from server to client
99    pub s2c: bool,
100}
101
102/// An argument as defined in the AMQP specification
103#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
104pub enum AMQPArgument {
105    /// The argument is holding a value
106    Value(AMQPValueArgument),
107    /// The argument is holding flags
108    Flags(AMQPFlagsArgument),
109}
110
111impl AMQPArgument {
112    pub(crate) fn force_default(&self) -> bool {
113        match self {
114            AMQPArgument::Value(v) => v.force_default,
115            AMQPArgument::Flags(f) => f.force_default(),
116        }
117    }
118}
119
120/// An argument holding a value as defined in the AMQP specification
121#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
122pub struct AMQPValueArgument {
123    /// The type of the argument's value
124    #[serde(rename = "type")]
125    pub amqp_type: AMQPType,
126    /// The name of the argument's value
127    pub name: String,
128    /// The default value of the argument's value
129    pub default_value: Option<AMQPValue>,
130    /// The domain of the argument's value
131    pub domain: Option<String>,
132    /// Whether the default value is forced or not
133    pub force_default: bool,
134}
135
136/// An argument holding a flags as defined in the AMQP specification
137#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
138pub struct AMQPFlagsArgument {
139    /// Whether all the flags have force_default or not
140    pub ignore_flags: bool,
141    /// The actual flags
142    pub flags: Vec<AMQPFlagArgument>,
143}
144
145impl AMQPFlagsArgument {
146    pub(crate) fn force_default(&self) -> bool {
147        self.flags.iter().all(|f| f.force_default)
148    }
149}
150
151/// An argument holding a flag as defined in the AMQP specification
152#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
153pub struct AMQPFlagArgument {
154    /// The name of the flag
155    pub name: String,
156    /// The default value for the flag
157    pub default_value: Boolean,
158    /// Whether the default value is forced or not
159    pub force_default: bool,
160}
161
162/// A property as defined in the AMQP specification
163#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
164pub struct AMQPProperty {
165    /// The type of the property
166    #[serde(rename = "type")]
167    pub amqp_type: AMQPType,
168    /// The name of the property
169    pub name: String,
170}