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
use crate::*;
use cyfs_base::*;
use serde_json::{Map, Value};
use std::fmt;
pub struct RouterEventRequest<REQ>
where
REQ: Send + Sync + 'static + JsonCodec<REQ> + fmt::Display,
{
pub request: REQ,
}
impl<REQ> fmt::Display for RouterEventRequest<REQ>
where
REQ: Send + Sync + 'static + JsonCodec<REQ> + fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "request: {}", self.request)?;
Ok(())
}
}
impl<REQ> RouterEventCategoryInfo for RouterEventRequest<REQ>
where
REQ: Send + Sync + 'static + JsonCodec<REQ> + fmt::Display + RouterEventCategoryInfo,
{
fn category() -> RouterEventCategory {
extract_router_event_category::<REQ>()
}
}
pub struct RouterEventResponse<RESP>
where
RESP: Send + Sync + 'static + JsonCodec<RESP> + fmt::Display,
{
pub handled: bool,
pub call_next: bool,
pub response: Option<BuckyResult<RESP>>,
}
impl<RESP> fmt::Display for RouterEventResponse<RESP>
where
RESP: Send + Sync + 'static + JsonCodec<RESP> + fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "handled: {}", self.handled)?;
write!(f, "call_next: {}", self.call_next)?;
if let Some(resp) = &self.response {
match resp {
Ok(v) => write!(f, "response: {}", v)?,
Err(e) => write!(f, "response error: {}", e)?,
}
}
Ok(())
}
}
impl<REQ> JsonCodec<RouterEventRequest<REQ>> for RouterEventRequest<REQ>
where
REQ: Send + Sync + 'static + JsonCodec<REQ> + fmt::Display,
{
fn encode_json(&self) -> Map<String, Value> {
let mut obj = Map::new();
obj.insert("request".to_string(), self.request.encode_value());
obj
}
fn decode_json(req_obj: &Map<String, Value>) -> BuckyResult<Self> {
Ok(Self {
request: JsonCodecHelper::decode_field(req_obj, "request")?,
})
}
}
impl<RESP> JsonCodec<RouterEventResponse<RESP>> for RouterEventResponse<RESP>
where
RESP: Send + Sync + 'static + JsonCodec<RESP> + fmt::Display,
{
fn encode_json(&self) -> Map<String, Value> {
let mut obj = Map::new();
obj.insert("handled".to_string(), Value::Bool(self.handled));
obj.insert("call_next".to_string(), Value::Bool(self.call_next));
if let Some(resp) = &self.response {
obj.insert("response".to_string(), resp.encode_value());
}
obj
}
fn decode_json(req_obj: &Map<String, Value>) -> BuckyResult<Self> {
Ok(Self {
handled: JsonCodecHelper::decode_bool_field(req_obj, "handled")?,
call_next: JsonCodecHelper::decode_bool_field(req_obj, "call_next")?,
response: JsonCodecHelper::decode_option_field(req_obj, "response")?,
})
}
}
pub struct RouterEventResponseHelper;
impl RouterEventResponseHelper {
pub fn encode_default() -> String {
RouterEventResponse::<TestEventRequest> {
handled: false,
call_next: true,
response: None,
}
.encode_string()
}
}
crate::declare_event_empty_param!(TestEventRequest, TestEvent);
crate::declare_event_empty_param!(TestEventResponse, TestEvent);
pub type RouterEventTestEventRequest = RouterEventRequest<TestEventRequest>;
pub type RouterEventTestEventResult = RouterEventResponse<TestEventResponse>;
pub struct ZoneRoleChangedEventRequest {
pub current_role: ZoneRole,
pub new_role: ZoneRole,
}
crate::declare_event_empty_param!(ZoneRoleChangedEventResponse, ZoneRoleChanged);
impl std::fmt::Display for ZoneRoleChangedEventRequest {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "current_role={}, new_role={}", self.current_role.as_str(), self.new_role.as_str())
}
}
impl JsonCodec<Self> for ZoneRoleChangedEventRequest {
fn encode_json(&self) -> serde_json::Map<String, serde_json::Value> {
let mut obj = Map::new();
JsonCodecHelper::encode_string_field(&mut obj, "current_role", self.current_role.as_str());
JsonCodecHelper::encode_string_field(&mut obj, "new_role", &self.new_role.as_str());
obj
}
fn decode_json(
obj: &serde_json::Map<String, serde_json::Value>,
) -> cyfs_base::BuckyResult<Self> {
Ok(Self {
current_role: JsonCodecHelper::decode_string_field(obj, "current_role")?,
new_role: JsonCodecHelper::decode_string_field(obj, "new_role")?,
})
}
}
impl RouterEventCategoryInfo for ZoneRoleChangedEventRequest {
fn category() -> RouterEventCategory {
RouterEventCategory::ZoneRoleChanged
}
}
pub type RouterEventZoneRoleChangedEventRequest = RouterEventRequest<ZoneRoleChangedEventRequest>;
pub type RouterEventZoneRoleChangedEventResult = RouterEventResponse<ZoneRoleChangedEventResponse>;