-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcanvas_dispatch.rs
More file actions
192 lines (173 loc) · 5.51 KB
/
Copy pathcanvas_dispatch.rs
File metadata and controls
192 lines (173 loc) · 5.51 KB
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
//! Inbound `canvas.*` JSON-RPC request dispatch helpers.
//!
//! Internal — public-facing trait lives in `crate::canvas`. Each helper
//! deserializes the generated wire request, calls the user-facing
//! [`CanvasHandler`] method, and serializes the result back onto JSON-RPC.
use std::sync::Arc;
use serde::Serialize;
use serde_json::Value;
use tracing::warn;
use crate::canvas::{CanvasError, CanvasHandler};
use crate::generated::api_types::{
CanvasProviderCloseRequest, CanvasProviderInvokeActionRequest, CanvasProviderOpenRequest,
rpc_methods,
};
use crate::{Client, JsonRpcRequest, JsonRpcResponse, error_codes};
async fn respond<T: Serialize>(client: &Client, request_id: u64, result: T) {
let value = match serde_json::to_value(&result) {
Ok(value) => value,
Err(error) => {
warn!(error = %error, "failed to serialize canvas response");
send_error(
client,
request_id,
error_codes::INTERNAL_ERROR,
"serialization failure",
None,
)
.await;
return;
}
};
let _ = client
.send_response(&JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id: request_id,
result: Some(value),
error: None,
})
.await;
}
async fn send_error(
client: &Client,
request_id: u64,
code: i32,
message: &str,
data: Option<Value>,
) {
let _ = client
.send_response(&JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id: request_id,
result: None,
error: Some(crate::JsonRpcError {
code,
message: message.to_string(),
data,
}),
})
.await;
}
async fn send_canvas_error(client: &Client, request_id: u64, error: CanvasError) {
let message = error.message.clone();
let data = Some(serde_json::json!({
"code": error.code,
"message": message,
}));
send_error(
client,
request_id,
error_codes::INTERNAL_ERROR,
&error.message,
data,
)
.await;
}
async fn parse_params<T: serde::de::DeserializeOwned>(
client: &Client,
request: &JsonRpcRequest,
) -> Option<T> {
let params = request
.params
.as_ref()
.cloned()
.unwrap_or(Value::Object(serde_json::Map::new()));
match serde_json::from_value(params) {
Ok(params) => Some(params),
Err(error) => {
send_error(
client,
request.id,
error_codes::INVALID_PARAMS,
&format!("invalid params: {error}"),
None,
)
.await;
None
}
}
}
fn canvas_handler_or_err(
handler: Option<&Arc<dyn CanvasHandler>>,
) -> Result<Arc<dyn CanvasHandler>, CanvasError> {
handler.cloned().ok_or_else(|| {
CanvasError::new(
"canvas_handler_unset",
"No CanvasHandler installed on this session; call SessionConfig::with_canvas_handler before creating the session.",
)
})
}
async fn open(client: &Client, handler: &Arc<dyn CanvasHandler>, request: JsonRpcRequest) {
let Some(params) = parse_params::<CanvasProviderOpenRequest>(client, &request).await else {
return;
};
match handler.on_open(params).await {
Ok(result) => respond(client, request.id, result).await,
Err(error) => send_canvas_error(client, request.id, error).await,
}
}
async fn close(client: &Client, handler: &Arc<dyn CanvasHandler>, request: JsonRpcRequest) {
let Some(params) = parse_params::<CanvasProviderCloseRequest>(client, &request).await else {
return;
};
match handler.on_close(params).await {
Ok(()) => respond(client, request.id, Value::Null).await,
Err(error) => send_canvas_error(client, request.id, error).await,
}
}
async fn invoke_action(client: &Client, handler: &Arc<dyn CanvasHandler>, request: JsonRpcRequest) {
let Some(params) = parse_params::<CanvasProviderInvokeActionRequest>(client, &request).await
else {
return;
};
match handler.on_action(params).await {
Ok(result) => respond(client, request.id, result).await,
Err(error) => send_canvas_error(client, request.id, error).await,
}
}
/// Dispatch a `canvas.*` request to the appropriate handler. Returns `true`
/// if the request was a canvas method, `false` otherwise.
pub(crate) async fn dispatch(
client: &Client,
handler: Option<&Arc<dyn CanvasHandler>>,
request: JsonRpcRequest,
) -> bool {
let method = request.method.as_str();
if !method.starts_with("canvas.") {
return false;
}
let handler = match canvas_handler_or_err(handler) {
Ok(handler) => handler,
Err(error) => {
send_canvas_error(client, request.id, error).await;
return true;
}
};
match method {
rpc_methods::CANVAS_OPEN => open(client, &handler, request).await,
rpc_methods::CANVAS_CLOSE => close(client, &handler, request).await,
rpc_methods::CANVAS_ACTION_INVOKE => invoke_action(client, &handler, request).await,
_ => {
warn!(method = %method, "unknown canvas.* method");
send_error(
client,
request.id,
error_codes::METHOD_NOT_FOUND,
&format!("unknown method: {method}"),
None,
)
.await;
}
}
true
}