diff options
author | Norbert Preining <norbert@preining.info> | 2019-12-03 03:01:24 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-12-03 03:01:24 +0000 |
commit | b8d4bb76703bcb15578e2b23c5d256532180b894 (patch) | |
tree | bedd1df7a00521a2bd986b3c0289d6556a59e39b /support/texlab/crates/jsonrpc/src/types.rs | |
parent | 02e4625a78a5029e8b5dc2a4ec70193b232f497e (diff) |
CTAN sync 201912030301
Diffstat (limited to 'support/texlab/crates/jsonrpc/src/types.rs')
-rw-r--r-- | support/texlab/crates/jsonrpc/src/types.rs | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/support/texlab/crates/jsonrpc/src/types.rs b/support/texlab/crates/jsonrpc/src/types.rs new file mode 100644 index 0000000000..90116ac793 --- /dev/null +++ b/support/texlab/crates/jsonrpc/src/types.rs @@ -0,0 +1,144 @@ +use serde::{Deserialize, Serialize}; +use serde_repr::*; + +pub const PROTOCOL_VERSION: &str = "2.0"; + +#[derive(Debug, Eq, Hash, PartialEq, Clone, Deserialize, Serialize)] +#[serde(untagged)] +pub enum Id { + Number(u64), + String(String), +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize_repr, Serialize_repr)] +#[repr(i32)] +pub enum ErrorCode { + ParseError = -32700, + InvalidRequest = -32600, + MethodNotFound = -32601, + InvalidParams = -32602, + InternalError = -32603, + ServerNotInitialized = -32002, + UnknownErrorCode = -32001, + RequestCancelled = -32800, +} + +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct Error { + pub code: ErrorCode, + pub message: String, + + #[serde(skip_serializing_if = "serde_json::Value::is_null")] + pub data: serde_json::Value, +} + +impl Error { + pub fn parse_error() -> Self { + Self { + code: ErrorCode::ParseError, + message: "Could not parse the input".to_owned(), + data: serde_json::Value::Null, + } + } + + pub fn method_not_found_error() -> Self { + Self { + code: ErrorCode::MethodNotFound, + message: "Method not found".to_owned(), + data: serde_json::Value::Null, + } + } + + pub fn deserialize_error() -> Self { + Self { + code: ErrorCode::InvalidParams, + message: "Could not deserialize parameter object".to_owned(), + data: serde_json::Value::Null, + } + } + + pub fn internal_error(message: String) -> Self { + Self { + code: ErrorCode::InternalError, + message, + data: serde_json::Value::Null, + } + } +} + +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct Request { + pub jsonrpc: String, + pub method: String, + pub params: serde_json::Value, + pub id: Id, +} + +impl Request { + pub fn new(method: String, params: serde_json::Value, id: Id) -> Self { + Request { + jsonrpc: PROTOCOL_VERSION.to_owned(), + method, + params, + id, + } + } +} + +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct Response { + pub jsonrpc: String, + + #[serde(skip_serializing_if = "Option::is_none")] + pub result: Option<serde_json::Value>, + + #[serde(skip_serializing_if = "Option::is_none")] + pub error: Option<Error>, + + pub id: Option<Id>, +} + +impl Response { + pub fn result(result: serde_json::Value, id: Id) -> Self { + Response { + jsonrpc: PROTOCOL_VERSION.to_owned(), + result: Some(result), + error: None, + id: Some(id), + } + } + + pub fn error(error: Error, id: Option<Id>) -> Self { + Response { + jsonrpc: PROTOCOL_VERSION.to_owned(), + result: None, + error: Some(error), + id, + } + } +} + +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct Notification { + pub jsonrpc: String, + pub method: String, + pub params: serde_json::Value, +} + +impl Notification { + pub fn new(method: String, params: serde_json::Value) -> Self { + Notification { + jsonrpc: PROTOCOL_VERSION.to_owned(), + method, + params, + } + } +} + +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +#[serde(untagged)] +pub enum Message { + Request(Request), + Notification(Notification), + Response(Response), +} |