//! Types to deserialize. use serde; use std::fmt; #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(untagged)] pub enum RequestId { Number(u64), String(String), } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct RequestMessage { pub jsonrpc: V2, pub id: Option, pub method: String, pub params: T, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct NotificationMessage { pub jsonrpc: V2, pub method: String, pub params: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ResponseMessage { pub jsonrpc: V2, pub id: Option, pub result: Option, pub error: Option>, } #[derive(Debug, Clone, Serialize, Deserialize)] pub enum Code { ParseError = -32700, InvalidRequest = -32600, MethodNotFound = -32601, InvalidParams = -32602, InternalError = -32603, ServerErrorStart = -32099, ServerErrorEnd = -32000, ServerNotInitialized = -32002, UnknownErrorCode = -32001, RequestCancelled = -32800, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ResponseError { pub code: Code, pub message: String, pub data: Option, } #[derive(Debug, PartialEq, Clone, Copy, Hash, Eq)] pub struct V2; impl serde::Serialize for V2 { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { serializer.serialize_str("2.0") } } impl<'a> serde::Deserialize<'a> for V2 { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'a>, { return deserializer.deserialize_identifier(Visitor); struct Visitor; impl<'a> serde::de::Visitor<'a> for Visitor { type Value = V2; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a string") } fn visit_str(self, value: &str) -> Result where E: serde::de::Error, { match value { "2.0" => Ok(V2), _ => Err(serde::de::Error::custom("invalid version")), } } } } }