1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
//! A transcription of types from the [`JSON-RPC 2.0` Specification](https://www.jsonrpc.org/specification).
//!
//! > When quoted, the specification will appear as blockquoted text, like so.
//!
//! # Design
//! - All structs are owned (i.e, there is no borrowing of data from the [`Deserializer`](serde::Deserializer)),
//! to facilitate ergonomics.
//! - Appearances of dynamic JSON [`Value`]s are parameterised out, to allow
//! deferred serialization using, i.e [RawValue](https://docs.rs/serde_json/latest/serde_json/value/struct.RawValue.html).
pub mod map;
pub mod params;
use std::{
borrow::Cow,
fmt::{self, Display},
hash::Hash,
ops::RangeInclusive,
str::FromStr,
};
use serde::{
de::{Error as _, Unexpected},
Deserialize, Serialize,
};
use serde_json::{Number, Value};
pub use map::Map;
/// A `JSON-RPC 2.0` request object.
///
/// Note that the `"jsonrpc": "2.0"` member is transparently checked during
/// deserialization, and added during serialization.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Request<T = Value> {
/// > A String containing the name of the method to be invoked.
/// > Method names that begin with the word rpc followed by a period character
/// > (U+002E or ASCII 46) are reserved for rpc-internal methods and extensions
/// > and MUST NOT be used for anything else.
pub method: String,
/// > A Structured value that holds the parameter values to be used during the
/// > invocation of the method.
/// > This member MAY be omitted.
pub params: Option<RequestParameters<T>>,
/// > An identifier established by the Client that MUST contain a String,
/// > Number, or NULL value if included.
/// > If it is not included it is assumed to be a notification.
/// > The value SHOULD normally not be Null and Numbers SHOULD NOT contain fractional parts
pub id: Option<Id>,
}
impl<T> Serialize for Request<T>
where
T: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
#[derive(Serialize)]
struct Serde<'a, T> {
jsonrpc: V2,
method: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
params: Option<&'a RequestParameters<T>>,
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<&'a Id>,
}
let Self { method, params, id } = self;
Serde {
jsonrpc: V2,
method,
params: params.as_ref(),
id: id.as_ref(),
}
.serialize(serializer)
}
}
impl<'de, T> Deserialize<'de> for Request<T>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct Serde<T> {
#[allow(unused)]
jsonrpc: V2,
method: String,
params: Option<RequestParameters<T>>,
#[serde(deserialize_with = "deserialize_some", default)]
id: Option<Id>,
}
Serde::deserialize(deserializer).map(
|Serde {
method, params, id, ..
}| Request { method, params, id },
)
}
}
impl<T> Request<T> {
pub const fn is_notification(&self) -> bool {
self.id.is_none()
}
}
#[test]
fn request() {
do_test::<Request>(
Request {
method: "myMethod".into(),
params: None,
id: None,
},
json!({
"jsonrpc": "2.0",
"method": "myMethod",
}),
);
do_test::<Request>(
Request {
method: "myMethod".into(),
params: None,
id: Some(Id::Null),
},
json!({
"jsonrpc": "2.0",
"method": "myMethod",
"id": null
}),
);
do_test::<Request>(
Request {
method: "myMethod".into(),
params: Some(RequestParameters::ByPosition(vec![
Value::Null,
Value::String("hello".into()),
])),
id: None,
},
json!({
"jsonrpc": "2.0",
"method": "myMethod",
"params": [null, Value::from("hello")]
}),
);
do_test::<Request>(
Request {
method: "myMethod".into(),
params: Some(RequestParameters::ByName(
[
(String::from("hello"), Value::Null),
(String::from("world"), Value::from(1)),
]
.into_iter()
.collect(),
)),
id: None,
},
json!({
"jsonrpc": "2.0",
"method": "myMethod",
"params": {
"hello": null,
"world": 1
}
}),
);
}
/// > A String specifying the version of the JSON-RPC protocol.
/// > MUST be exactly "2.0".
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
struct V2;
impl<'de> Deserialize<'de> for V2 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct SerdeCow<'a>(#[serde(borrow)] Cow<'a, str>);
let SerdeCow(cow) = SerdeCow::deserialize(deserializer)?;
match &*cow {
"2.0" => Ok(Self),
other => Err(D::Error::invalid_value(Unexpected::Str(other), &"2.0")),
}
}
}
impl Serialize for V2 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str("2.0")
}
}
/// > If present, parameters for the rpc call MUST be provided as a Structured value.
/// > Either by-position through an Array or by-name through an Object.
#[derive(Serialize, Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(
untagged,
expecting = "an `Array` of by-position paramaters, or an `Object` of by-name parameters"
)]
pub enum RequestParameters<T = Value> {
/// > params MUST be an Array, containing the values in the Server expected order.
ByPosition(Vec<T>),
/// > params MUST be an Object, with member names that match the Server
/// > expected parameter names.
/// > The absence of expected names MAY result in an error being generated.
/// > The names MUST match exactly, including case, to the method's expected parameters.
ByName(Map<T>),
}
impl<T> RequestParameters<T> {
pub fn len(&self) -> usize {
match self {
RequestParameters::ByPosition(it) => it.len(),
RequestParameters::ByName(it) => it.len(),
}
}
pub fn is_empty(&self) -> bool {
match self {
RequestParameters::ByPosition(it) => it.is_empty(),
RequestParameters::ByName(it) => it.is_empty(),
}
}
}
/// See [`Request::id`].
#[derive(Serialize, Debug, Clone, PartialEq, Eq, Hash, Deserialize, Default)]
#[serde(untagged, expecting = "a string, a number, or null")]
pub enum Id {
String(String),
Number(Number),
#[default]
Null,
}
impl FromStr for Id {
type Err = serde_json::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}
/// A `JSON-RPC 2.0` response object.
///
/// Note that the `"jsonrpc": "2.0"` member is transparently checked during
/// deserialization, and added during serialization.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Response<T = Value, E = Value> {
/// > "result":
/// >
/// > This member is REQUIRED on success.
/// > This member MUST NOT exist if there was an error invoking the method.
/// > The value of this member is determined by the method invoked on the Server.
/// >
/// > "error":
/// >
/// > This member is REQUIRED on error.
/// > This member MUST NOT exist if there was no error triggered during invocation.
pub result: Result<T, Error<E>>,
/// > This member is REQUIRED.
/// > It MUST be the same as the value of the id member in the Request Object.
/// > If there was an error in detecting the id in the Request object
/// > (e.g. Parse error/Invalid Request), it MUST be Null.
pub id: Id,
}
impl<T> Default for Response<T>
where
T: Default,
{
fn default() -> Self {
Self {
result: Ok(T::default()),
id: Default::default(),
}
}
}
/// Distinguish between absent and present but null.
///
/// See <https://github.com/serde-rs/serde/issues/984#issuecomment-314143738>
fn deserialize_some<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
where
T: Deserialize<'de>,
D: serde::de::Deserializer<'de>,
{
Deserialize::deserialize(deserializer).map(Some)
}
impl<T, E> Serialize for Response<T, E>
where
T: Serialize,
E: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
#[derive(Serialize)]
struct Serde<'a, T, E> {
jsonrpc: V2,
#[serde(skip_serializing_if = "Option::is_none")]
result: Option<Option<&'a T>>,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<&'a Error<E>>,
id: &'a Id,
}
let Self { result, id } = self;
let helper = match result {
Ok(ok) => Serde {
jsonrpc: V2,
result: Some(Some(ok)),
error: None,
id,
},
Err(e) => Serde {
jsonrpc: V2,
result: None,
error: Some(e),
id,
},
};
helper.serialize(serializer)
}
}
impl<'de, T, E> Deserialize<'de> for Response<T, E>
where
T: Deserialize<'de>,
E: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(bound(deserialize = "T: Deserialize<'de>, E: Deserialize<'de>"))]
struct Serde<T, E> {
#[allow(unused)]
jsonrpc: V2,
#[serde(default, deserialize_with = "deserialize_some")]
result: Option<Option<T>>,
#[serde(default, deserialize_with = "deserialize_some")]
error: Option<Error<E>>,
id: Id,
}
let Serde {
jsonrpc: _,
error,
result,
id,
} = Serde::deserialize(deserializer)?;
match (result, error) {
(Some(Some(ok)), None) => Ok(Response { result: Ok(ok), id }),
(None, Some(err)) => Ok(Response {
result: Err(err),
id,
}),
(Some(_), Some(_)) => Err(D::Error::custom(
"only ONE of `error` and `result` may be present",
)),
(None, None) => Err(D::Error::custom("must have an `error` or `result` member")),
// we expect this case to error
(Some(None), None) => Ok(Response {
result: Ok(T::deserialize(serde::de::value::UnitDeserializer::new())?),
id,
}),
}
}
}
#[test]
fn response() {
do_test::<Response<(), ()>>(
Response {
result: Ok(()),
id: Id::Null,
},
json!({
"jsonrpc": "2.0",
"result": null,
"id": null
}),
);
do_test::<Response>(
Response {
result: Ok(Value::Null),
id: Id::Null,
},
json!({
"jsonrpc": "2.0",
"result": null,
"id": null
}),
);
}
/// A `JSON-RPC 2.0` error object.
#[derive(Serialize, Debug, Clone, PartialEq, Eq, Default, Deserialize)]
pub struct Error<T = Value> {
/// > A Number that indicates the error type that occurred.
/// > This MUST be an integer.
///
/// See the associated constants for error types defined by the specification.
pub code: i64,
/// > A String providing a short description of the error.
/// > The message SHOULD be limited to a concise single sentence.
pub message: String,
/// > A Primitive or Structured value that contains additional information about the error.
/// > This may be omitted.
/// > The value of this member is defined by the Server
/// > (e.g. detailed error information, nested errors etc.).
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<T>,
}
macro_rules! error_code_and_ctor {
(
$(
$(#[doc = $doc:literal])*
$const_name:ident / $ctor_name:ident = $number:literal;
)*
) => {
impl Error {
$(
$(#[doc = $doc])*
pub const $const_name: i64 = $number;
)*
}
impl<T> Error<T> {
$(
#[doc = concat!("Convenience method for creating a new error with code [`Self::", stringify!($const_name), "`]")]
pub fn $ctor_name(message: impl Display, data: impl Into<Option<T>>) -> Self {
Self::new(Error::$const_name, message, data)
}
)*
/// If [`Self::code`] is one of the predefined errors in the spec,
/// get its associated error message.
pub const fn spec_message(&self) -> Option<&'static str> {
match self.code {
$(
Error::$const_name => {
const LIMBS: &[&'static str] = &[
$($doc),*
];
const LIMB: &str = LIMBS[0];
const MESSAGE: &str = {
let (_quot, rest) = LIMB.as_bytes().split_at(2);
match std::str::from_utf8(rest) {
Ok(it) => it,
Err(_) => panic!()
}
};
Some(MESSAGE)
},
)*
_ => None
}
}
}
}
}
error_code_and_ctor! {
/// > Invalid JSON was received by the server.
/// > An error occurred on the server while parsing the JSON text.
PARSE_ERROR / parse_error = -32700;
/// > The JSON sent is not a valid Request object.
INVALID_REQUEST / invalid_request = -32600;
/// > The method does not exist / is not available.
METHOD_NOT_FOUND / method_not_found = -32601;
/// > Invalid method parameter(s).
INVALID_PARAMS / invalid_params = -32602;
/// > Internal JSON-RPC error.
INTERNAL_ERROR / internal_error = -32603;
}
impl Error {
/// > Reserved for implementation-defined server-errors.
pub const SERVER_ERROR_RANGE: RangeInclusive<i64> = -32099..=-32000;
}
impl<T> Error<T> {
/// Convenience method for creating a new error.
pub fn new(code: i64, message: impl Display, data: impl Into<Option<T>>) -> Self {
Self {
code,
message: message.to_string(),
data: data.into(),
}
}
}
impl<T> fmt::Display for Error<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("code {}", self.code))?;
if let Some(e) = self.spec_message() {
f.write_fmt(format_args!(" ({})", e))?
};
if !self.message.is_empty() {
f.write_fmt(format_args!(": {}", self.message))?
}
Ok(())
}
}
impl<T> std::error::Error for Error<T> where T: fmt::Debug {}
#[derive(Serialize, Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(
untagged,
expecting = "a single response object, or an Array of batched response objects"
)]
/// A response to a [`MaybeBatchedRequest`].
pub enum MaybeBatchedResponse<T> {
Single(Response<T>),
Batch(Vec<Response<T>>),
}
/// > To send several Request objects at the same time, the Client MAY send an Array filled with Request objects.
#[derive(Serialize, Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(
untagged,
expecting = "a single request object, or an Array of batched request objects"
)]
pub enum MaybeBatchedRequest<T> {
Single(Request<T>),
Batch(Vec<Request<T>>),
}
#[cfg(test)]
use serde_json::json;
#[cfg(test)]
#[track_caller]
fn do_test<T>(expected: T, json: Value)
where
T: PartialEq + core::fmt::Debug + serde::de::DeserializeOwned + Serialize,
{
assert_eq!(
expected,
serde_json::from_value(json.clone()).expect("deserialization failed"),
"deserialization mismatch"
);
assert_eq!(
serde_json::to_value(expected).expect("serialization failed"),
json,
"serialization mismatch"
);
}