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
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>;