/
githubmirror
/
lapce
Обзор
Документация
Войти
/
githubmirror
/
lapce
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v0.0.6
rpc/src/parse.rs
51 строка
1 KB
Dongdong Zhou
proxy get completion
18 ноя 2020, 01:22
18 ноя 2020, 01:22
8d473ab
Код
Авторство
О чём код?
use anyhow::{anyhow, Result}; use serde::de::DeserializeOwned; use serde_json::Value; #[derive(Debug, Clone)] pub struct RpcObject(pub Value); pub type RequestId = u64; #[derive(Debug, Clone, PartialEq)] /// An RPC call, which may be either a notification or a request. pub enum Call<N, R> { /// An id and an RPC Request Request(RequestId, R), /// An RPC Notification Notification(N), } impl RpcObject { pub fn get_id(&self) -> Option<RequestId> { self.0.get("id").and_then(Value::as_u64) } pub fn is_response(&self) -> bool { self.0.get("id").is_some() && self.0.get("method").is_none() } pub fn into_rpc<N, R>(self) -> Result<Call<N, R>> where N: DeserializeOwned, R: DeserializeOwned, { let id = self.get_id(); match id { Some(id) => match serde_json::from_value::<R>(self.0) { Ok(resp) => Ok(Call::Request(id, resp)), Err(err) => Err(anyhow!(err)), }, None => { let result = serde_json::from_value::<N>(self.0)?; Ok(Call::Notification(result)) } } } } impl From<Value> for RpcObject { fn from(v: Value) -> RpcObject { RpcObject(v) } }