jsonrpc.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //! JSON-RPC 2.0 primitives
  2. use rand::Rng;
  3. use serde::{Deserialize, Serialize};
  4. use serde_json::{json, Value};
  5. /// JSON-RPC error codes.
  6. /// The error codes from and including -32768 to -32000 are reserved for pre-defined errors.
  7. #[derive(Debug, Clone)]
  8. pub enum ErrorCode {
  9. ParseError,
  10. InvalidRequest,
  11. MethodNotFound,
  12. InvalidParams,
  13. InternalError,
  14. ServerError(i64),
  15. InvalidId,
  16. }
  17. impl ErrorCode {
  18. pub fn code(&self) -> i64 {
  19. match *self {
  20. Self::ParseError => -32700,
  21. Self::InvalidRequest => -32600,
  22. Self::MethodNotFound => -32601,
  23. Self::InvalidParams => -32602,
  24. Self::InternalError => -32603,
  25. // -32000 to -32099
  26. Self::ServerError(c) => c,
  27. Self::InvalidId => -32001,
  28. }
  29. }
  30. pub fn desc(&self) -> String {
  31. let desc = match *self {
  32. Self::ParseError => "Parse error",
  33. Self::InvalidRequest => "Invalid request",
  34. Self::MethodNotFound => "Method not found",
  35. Self::InvalidParams => "Invalid params",
  36. Self::InternalError => "Internal error",
  37. Self::ServerError(_) => "",
  38. Self::InvalidId => "Request ID mismatch",
  39. };
  40. desc.to_string()
  41. }
  42. }
  43. /// Wrapping enum around the possible JSON-RPC object types.
  44. #[derive(Clone, Debug, Serialize, Deserialize)]
  45. #[serde(untagged)]
  46. pub enum JsonResult {
  47. Response(JsonResponse),
  48. Error(JsonError),
  49. Notification(JsonNotification),
  50. }
  51. impl From<JsonResponse> for JsonResult {
  52. fn from(resp: JsonResponse) -> Self {
  53. Self::Response(resp)
  54. }
  55. }
  56. impl From<JsonError> for JsonResult {
  57. fn from(err: JsonError) -> Self {
  58. Self::Error(err)
  59. }
  60. }
  61. impl From<JsonNotification> for JsonResult {
  62. fn from(notif: JsonNotification) -> Self {
  63. Self::Notification(notif)
  64. }
  65. }
  66. /// A JSON-RPC request object.
  67. #[derive(Clone, Debug, Serialize, Deserialize)]
  68. pub struct JsonRequest {
  69. /// JSON-RPC version
  70. pub jsonrpc: Value,
  71. /// Request ID
  72. pub id: Value,
  73. /// Request method
  74. pub method: Value,
  75. /// Request parameters
  76. pub params: Value,
  77. }
  78. impl JsonRequest {
  79. pub fn new(method: &str, parameters: Value) -> Self {
  80. let mut rng = rand::thread_rng();
  81. Self {
  82. jsonrpc: json!("2.0"),
  83. id: json!(rng.gen::<u64>()),
  84. method: json!(method),
  85. params: parameters,
  86. }
  87. }
  88. }
  89. /// A JSON-RPC notification object.
  90. #[derive(Clone, Debug, Serialize, Deserialize)]
  91. pub struct JsonNotification {
  92. /// JSON-RPC version
  93. pub jsonrpc: Value,
  94. /// Notification method
  95. pub method: Value,
  96. /// Notification parameters
  97. pub params: Value,
  98. }
  99. impl JsonNotification {
  100. pub fn new(method: &str, parameters: Value) -> Self {
  101. Self { jsonrpc: json!("2.0"), method: json!(method), params: parameters }
  102. }
  103. }
  104. /// A JSON-RPC response object.
  105. #[derive(Clone, Debug, Serialize, Deserialize)]
  106. pub struct JsonResponse {
  107. /// JSON-RPC version
  108. pub jsonrpc: Value,
  109. /// Request ID
  110. pub id: Value,
  111. /// Response result
  112. pub result: Value,
  113. }
  114. impl JsonResponse {
  115. pub fn new(result: Value, id: Value) -> Self {
  116. Self { jsonrpc: json!("2.0"), id, result }
  117. }
  118. }
  119. /// A JSON-RPC error object.
  120. #[derive(Clone, Debug, Serialize, Deserialize)]
  121. pub struct JsonError {
  122. /// JSON-RPC version
  123. pub jsonrpc: Value,
  124. /// Request ID
  125. pub id: Value,
  126. /// JSON-RPC error (code and message)
  127. pub error: JsonErrorVal,
  128. }
  129. /// A JSON-RPC error value (code and message)
  130. #[derive(Clone, Debug, Serialize, Deserialize)]
  131. pub struct JsonErrorVal {
  132. /// Error code
  133. pub code: Value,
  134. /// Error message
  135. pub message: Value,
  136. }
  137. impl JsonError {
  138. pub fn new(c: ErrorCode, m: Option<String>, id: Value) -> Self {
  139. let error = JsonErrorVal {
  140. code: json!(c.code()),
  141. message: if m.is_none() { json!(c.desc()) } else { json!(m.unwrap()) },
  142. };
  143. Self { jsonrpc: json!("2.0"), error, id }
  144. }
  145. }