Skip to main content

jsonrpsee_core/
lib.rs

1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any
4// person obtaining a copy of this software and associated
5// documentation files (the "Software"), to deal in the
6// Software without restriction, including without
7// limitation the rights to use, copy, modify, merge,
8// publish, distribute, sublicense, and/or sell copies of
9// the Software, and to permit persons to whom the Software
10// is furnished to do so, subject to the following
11// conditions:
12//
13// The above copyright notice and this permission notice
14// shall be included in all copies or substantial portions
15// of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25// DEALINGS IN THE SOFTWARE.
26
27//! Shared utilities for `jsonrpsee`.
28//!
29//! Vendored from [jsonrpsee](https://github.com/paritytech/jsonrpsee), Copyright (c) 2019-2021
30//! Parity Technologies (UK) Ltd., licensed under the MIT License (see
31//! `crates/rpc/jsonrpsee/LICENSE`).
32
33#![cfg_attr(not(test), warn(unused_crate_dependencies))]
34#![cfg_attr(docsrs, feature(doc_cfg))]
35
36/// Error type.
37pub mod error;
38
39/// Traits
40pub mod traits;
41
42#[cfg(feature = "http-helpers")]
43pub mod http_helpers;
44
45#[cfg(feature = "server")]
46pub mod id_providers;
47
48#[cfg(feature = "server")]
49pub mod middleware;
50
51#[cfg(feature = "server")]
52pub mod server;
53
54pub use error::{RegisterMethodError, SubscriptionError};
55
56/// JSON-RPC result.
57pub type RpcResult<T> = std::result::Result<T, jsonrpsee_types::ErrorObjectOwned>;
58
59#[doc(hidden)]
60mod proc_macros_support;
61
62/// Re-exports for proc-macro library to not require any additional
63/// dependencies to be explicitly added by users of the macro.
64#[doc(hidden)]
65pub mod __reexports {
66    // Needed for the params parsing in the proc macro API.
67    #[cfg(feature = "server")]
68    pub use tokio;
69
70    pub use super::proc_macros_support::*;
71}
72
73pub use serde::{de::DeserializeOwned, Serialize};
74pub use serde_json::{
75    value::{to_raw_value as to_json_raw_value, RawValue as JsonRawValue},
76    Value as JsonValue,
77};
78
79/// Ten megabytes.
80pub const TEN_MB_SIZE_BYTES: u32 = 10 * 1024 * 1024;
81
82/// The return type if the subscription wants to return `Result`.
83pub type SubscriptionResult = Result<(), SubscriptionError>;
84
85/// Type erased error.
86pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
87
88/// Skips the leading whitespace of a JSON-RPC message and tells a single request from a batch by
89/// its first byte.
90///
91/// Returns the message from that byte on and whether it is a single request, or `None` if the
92/// first 128 bytes do not lead to a `{` or a `[`. Only the four JSON whitespace characters are
93/// skipped, not for example form feed.
94pub fn classify_message(data: &[u8]) -> Option<(&[u8], bool)> {
95    let start =
96        data.iter().take(128).position(|byte| !matches!(byte, b' ' | b'\t' | b'\n' | b'\r'))?;
97
98    match data[start] {
99        b'{' => Some((&data[start..], true)),
100        b'[' => Some((&data[start..], false)),
101        _ => None,
102    }
103}