Dispatch one server-side event and then return. Just so we have a nicer looking `select` statement in `run` :-)
(
&self,
input: &mut FramedRead<I, JsonRpcCodec>,
plugin: &Plugin<S>,
)
| 933 | /// Dispatch one server-side event and then return. Just so we |
| 934 | /// have a nicer looking `select` statement in `run` :-) |
| 935 | async fn dispatch_one<I>( |
| 936 | &self, |
| 937 | input: &mut FramedRead<I, JsonRpcCodec>, |
| 938 | plugin: &Plugin<S>, |
| 939 | ) -> Result<(), Error> |
| 940 | where |
| 941 | I: Send + AsyncReadExt + Unpin, |
| 942 | { |
| 943 | match input.next().await { |
| 944 | Some(Ok(msg)) => { |
| 945 | trace!("Received a message: {:?}", msg); |
| 946 | match msg { |
| 947 | messages::JsonRpc::Request(_id, _p) => { |
| 948 | todo!( |
| 949 | "This is unreachable until we start filling in messages:Request. Until then the custom dispatcher below is used exclusively." |
| 950 | ); |
| 951 | } |
| 952 | messages::JsonRpc::Notification(_n) => { |
| 953 | todo!( |
| 954 | "As soon as we define the full structure of the messages::Notification we'll get here. Until then the custom dispatcher below is used." |
| 955 | ) |
| 956 | } |
| 957 | messages::JsonRpc::CustomRequest(id, request) => { |
| 958 | trace!("Dispatching custom method {:?}", request); |
| 959 | let method = request |
| 960 | .get("method") |
| 961 | .context("Missing 'method' in request")? |
| 962 | .as_str() |
| 963 | .context("'method' is not a string")?; |
| 964 | let params = request |
| 965 | .get("params") |
| 966 | .context("Missing 'params' field in request")? |
| 967 | .clone(); |
| 968 | |
| 969 | let plugin = plugin.clone(); |
| 970 | let method_str = method.to_string(); |
| 971 | |
| 972 | if let Some(callback) = self.hooks.get(method) { |
| 973 | let call = callback(plugin.clone(), params); |
| 974 | tokio::spawn(async move { |
| 975 | let res = std::panic::AssertUnwindSafe(call).catch_unwind().await; |
| 976 | match res { |
| 977 | Ok(Ok(v)) => { |
| 978 | let _ = plugin |
| 979 | .sender |
| 980 | .send(json!({ |
| 981 | "jsonrpc": "2.0", |
| 982 | "id": id, |
| 983 | "result": v |
| 984 | })) |
| 985 | .await |
| 986 | .context("returning custom response"); |
| 987 | } |
| 988 | Ok(Err(e)) => { |
| 989 | if let Some(fallback) = hook_fallback(&method_str) { |
| 990 | log::error!( |
| 991 | "Hook handler for '{method_str}' failed with \ |
| 992 | error: {e}. Returning safe fallback response to \ |