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
use serde_derive::*;
use serde_json::{Map, Value};
use std::collections::btree_map::BTreeMap;
use std::fmt::Display;
pub type RoleMap = BTreeMap<Role, RoleDesc>;
pub type Dict = Map<String, Value>;
#[allow(dead_code)]
pub mod types {
pub const HELLO: u8 = 01;
pub const WELCOME: u8 = 02;
pub const ABORT: u8 = 03;
pub const GOODBYE: u8 = 06;
pub const CHALLENGE: u8 = 04;
pub const AUTHENTICATE: u8 = 05;
pub const ERROR: u8 = 08;
pub const PUBLISH: u8 = 16;
pub const PUBLISHED: u8 = 17;
pub const CALL: u8 = 48;
pub const CANCEL: u8 = 49;
pub const RESULT: u8 = 50;
pub const INTERRUPT: u8 = 69;
}
#[derive(Serialize, Deserialize, Default)]
#[serde(default)]
pub struct RoleDesc {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub features: Vec<String>,
}
#[derive(Serialize, Deserialize, Hash, PartialOrd, PartialEq, Eq, Ord)]
#[serde(rename_all = "lowercase")]
pub enum Role {
Caller,
Callee,
Publisher,
Dealer,
}
#[derive(Debug, Clone)]
pub struct WampError {
pub code: ErrorKind,
pub message: String,
pub extra: Dict,
}
impl Display for WampError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "{}: {}", self.code.uri(), self.message)
}
}
macro_rules! error_kinds {
($(
$(#[$outer:meta])*
$error_code:ident : $uri:expr),+) => {
#[derive(Debug, PartialEq, Clone)]
pub enum ErrorKind {
$(
$(#[$outer])*
#[doc= "\n\n**WAMP uri:**"]
#[doc= $uri]
$error_code
),+
,
Other(String)
}
impl ErrorKind {
pub fn uri(&self) -> &str {
match self {
$(
ErrorKind::$error_code => $uri,
)+
ErrorKind::Other(code) => code
}
}
pub fn from_uri(uri : &str) -> Self {
match uri {
$(
$uri => ErrorKind::$error_code,
)+
uri => ErrorKind::Other(uri.to_string())
}
}
}
};
}
error_kinds! {
InvalidURI : "wamp.error.invalid_uri",
NoSuchProcedure : "wamp.error.no_such_procedure",
ProcedureAlreadyExists : "wamp.error.procedure_already_exists",
NoSuchRegistration : "wamp.error.no_such_registration",
NoSuchSubscription : "wamp.error.no_such_subscription",
InvalidArgument : "wamp.error.invalid_argument",
SystemShutdown : "wamp.error.system_shutdown",
CloseRealm : "wamp.error.close_realm",
GoodbyeAndOut : "wamp.error.goodbye_and_out",
NotAuthorized : "wamp.error.not_authorized",
AuthorizationFailed : "wamp.error.authorization_failed",
NoSuchRealm : "wamp.error.no_such_realm",
NoSuchRole : "wamp.error.no_such_role",
Canceled : "wamp.error.canceled",
OptionNotAllowed : "wamp.error.option_not_allowed",
NoEligibleCallee : "wamp.error.no_eligible_callee",
NetworkFailure : "wamp.error.network_failure"
}
impl serde::Serialize for ErrorKind {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.uri())
}
}
#[derive(Serialize, Deserialize, Default)]
pub struct HelloSpec<'a> {
pub roles: RoleMap,
#[serde(rename = "authmethods")]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub auth_methods: Vec<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub authid: Option<&'a str>,
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_uri() {
assert_eq!(ErrorKind::Canceled.uri(), "wamp.error.canceled");
assert_eq!(
ErrorKind::from_uri("wamp.error.network_failure"),
ErrorKind::NetworkFailure
);
eprintln!(
"json={}",
serde_json::to_string_pretty(&ErrorKind::AuthorizationFailed).unwrap()
);
}
#[test]
fn test_hello() {
let val = HelloSpec {
roles: vec![(Role::Caller, RoleDesc::default())]
.into_iter()
.collect(),
auth_methods: vec![],
authid: None,
};
eprintln!("json={}", serde_json::to_value(&val).unwrap());
}
}