cyfs_lib/events/
request.rs

1use crate::*;
2use cyfs_base::*;
3
4use serde_json::{Map, Value};
5use std::fmt;
6
7pub struct RouterEventRequest<REQ>
8where
9    REQ: Send + Sync + 'static + JsonCodec<REQ> + fmt::Display,
10{
11    pub request: REQ,
12}
13
14impl<REQ> fmt::Display for RouterEventRequest<REQ>
15where
16    REQ: Send + Sync + 'static + JsonCodec<REQ> + fmt::Display,
17{
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        write!(f, "request: {}", self.request)?;
20
21        Ok(())
22    }
23}
24
25impl<REQ> RouterEventCategoryInfo for RouterEventRequest<REQ>
26where
27    REQ: Send + Sync + 'static + JsonCodec<REQ> + fmt::Display + RouterEventCategoryInfo,
28{
29    fn category() -> RouterEventCategory {
30        extract_router_event_category::<REQ>()
31    }
32}
33
34pub struct RouterEventResponse<RESP>
35where
36    RESP: Send + Sync + 'static + JsonCodec<RESP> + fmt::Display,
37{
38    pub handled: bool,
39    pub call_next: bool,
40    pub response: Option<BuckyResult<RESP>>,
41}
42
43impl<RESP> fmt::Display for RouterEventResponse<RESP>
44where
45    RESP: Send + Sync + 'static + JsonCodec<RESP> + fmt::Display,
46{
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        write!(f, "handled: {}", self.handled)?;
49        write!(f, "call_next: {}", self.call_next)?;
50
51        if let Some(resp) = &self.response {
52            match resp {
53                Ok(v) => write!(f, "response: {}", v)?,
54                Err(e) => write!(f, "response error: {}", e)?,
55            }
56        }
57
58        Ok(())
59    }
60}
61
62impl<REQ> JsonCodec<RouterEventRequest<REQ>> for RouterEventRequest<REQ>
63where
64    REQ: Send + Sync + 'static + JsonCodec<REQ> + fmt::Display,
65{
66    fn encode_json(&self) -> Map<String, Value> {
67        let mut obj = Map::new();
68
69        obj.insert("request".to_string(), self.request.encode_value());
70
71        obj
72    }
73
74    fn decode_json(req_obj: &Map<String, Value>) -> BuckyResult<Self> {
75        Ok(Self {
76            request: JsonCodecHelper::decode_field(req_obj, "request")?,
77        })
78    }
79}
80
81impl<RESP> JsonCodec<RouterEventResponse<RESP>> for RouterEventResponse<RESP>
82where
83    RESP: Send + Sync + 'static + JsonCodec<RESP> + fmt::Display,
84{
85    fn encode_json(&self) -> Map<String, Value> {
86        let mut obj = Map::new();
87
88        obj.insert("handled".to_string(), Value::Bool(self.handled));
89        obj.insert("call_next".to_string(), Value::Bool(self.call_next));
90
91        if let Some(resp) = &self.response {
92            obj.insert("response".to_string(), resp.encode_value());
93        }
94
95        obj
96    }
97
98    fn decode_json(req_obj: &Map<String, Value>) -> BuckyResult<Self> {
99        Ok(Self {
100            handled: JsonCodecHelper::decode_bool_field(req_obj, "handled")?,
101            call_next: JsonCodecHelper::decode_bool_field(req_obj, "call_next")?,
102            response: JsonCodecHelper::decode_option_field(req_obj, "response")?,
103        })
104    }
105}
106
107pub struct RouterEventResponseHelper;
108
109impl RouterEventResponseHelper {
110    pub fn encode_default() -> String {
111        RouterEventResponse::<TestEventRequest> {
112            handled: false,
113            call_next: true,
114            response: None,
115        }
116        .encode_string()
117    }
118}
119
120// test event
121crate::declare_event_empty_param!(TestEventRequest, TestEvent);
122crate::declare_event_empty_param!(TestEventResponse, TestEvent);
123
124// request
125pub type RouterEventTestEventRequest = RouterEventRequest<TestEventRequest>;
126
127// response
128pub type RouterEventTestEventResult = RouterEventResponse<TestEventResponse>;
129
130// zone role changed
131pub struct ZoneRoleChangedEventRequest {
132    pub current_role: ZoneRole,
133    pub new_role: ZoneRole,
134}
135crate::declare_event_empty_param!(ZoneRoleChangedEventResponse, ZoneRoleChanged);
136
137impl std::fmt::Display for ZoneRoleChangedEventRequest {
138    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
139        write!(f, "current_role={}, new_role={}", self.current_role.as_str(), self.new_role.as_str())
140    }
141}
142
143impl JsonCodec<Self> for ZoneRoleChangedEventRequest {
144    fn encode_json(&self) -> serde_json::Map<String, serde_json::Value> {
145        let mut obj = Map::new();
146        JsonCodecHelper::encode_string_field(&mut obj, "current_role", self.current_role.as_str());
147        JsonCodecHelper::encode_string_field(&mut obj, "new_role", &self.new_role.as_str());
148
149        obj
150    }
151
152    fn decode_json(
153        obj: &serde_json::Map<String, serde_json::Value>,
154    ) -> cyfs_base::BuckyResult<Self> {
155        Ok(Self {
156            current_role: JsonCodecHelper::decode_string_field(obj, "current_role")?,
157            new_role: JsonCodecHelper::decode_string_field(obj, "new_role")?,
158        })
159    }
160}
161
162impl RouterEventCategoryInfo for ZoneRoleChangedEventRequest {
163    fn category() -> RouterEventCategory {
164        RouterEventCategory::ZoneRoleChanged
165    }
166}
167
168// request
169pub type RouterEventZoneRoleChangedEventRequest = RouterEventRequest<ZoneRoleChangedEventRequest>;
170
171// response
172pub type RouterEventZoneRoleChangedEventResult = RouterEventResponse<ZoneRoleChangedEventResponse>;