jsonrpsee_core/traits.rs
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
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use jsonrpsee_types::SubscriptionId;
use serde::Serialize;
use serde_json::value::RawValue;
/// Marker trait for types that can be serialized as JSON compatible strings.
///
/// This trait ensures the correctness of the RPC parameters.
///
/// # Note
///
/// Please consider using the [`crate::params::ArrayParams`] and [`crate::params::ObjectParams`] than
/// implementing this trait.
///
/// # Examples
///
/// ## Implementation for hard-coded strings
///
/// ```rust
/// use jsonrpsee_core::traits::ToRpcParams;
/// use serde_json::value::RawValue;
///
/// struct ManualParam;
///
/// impl ToRpcParams for ManualParam {
/// fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, serde_json::Error> {
/// // Manually define a valid JSONRPC parameter.
/// RawValue::from_string("[1, \"2\", 3]".to_string()).map(Some)
/// }
/// }
/// ```
///
/// ## Implementation for JSON serializable structures
///
/// ```rust
/// use jsonrpsee_core::traits::ToRpcParams;
/// use serde_json::value::RawValue;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct SerParam {
/// param_1: u8,
/// param_2: String,
/// };
///
/// impl ToRpcParams for SerParam {
/// fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, serde_json::Error> {
/// let s = String::from_utf8(serde_json::to_vec(&self)?).expect("Valid UTF8 format");
/// RawValue::from_string(s).map(Some)
/// }
/// }
/// ```
pub trait ToRpcParams {
/// Consume and serialize the type as a JSON raw value.
fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, serde_json::Error>;
}
// To not bound the `ToRpcParams: Serialize` define a custom implementation
// for types which are serializable.
macro_rules! to_rpc_params_impl {
() => {
fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, serde_json::Error> {
let json = serde_json::to_string(&self)?;
RawValue::from_string(json).map(Some)
}
};
}
impl<P: Serialize> ToRpcParams for &[P] {
to_rpc_params_impl!();
}
impl<P: Serialize> ToRpcParams for Vec<P> {
to_rpc_params_impl!();
}
impl<P, const N: usize> ToRpcParams for [P; N]
where
[P; N]: Serialize,
{
to_rpc_params_impl!();
}
macro_rules! tuple_impls {
($($len:expr => ($($n:tt $name:ident)+))+) => {
$(
impl<$($name: Serialize),+> ToRpcParams for ($($name,)+) {
to_rpc_params_impl!();
}
)+
}
}
tuple_impls! {
1 => (0 T0)
2 => (0 T0 1 T1)
3 => (0 T0 1 T1 2 T2)
4 => (0 T0 1 T1 2 T2 3 T3)
5 => (0 T0 1 T1 2 T2 3 T3 4 T4)
6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
}
/// Trait to generate subscription IDs.
pub trait IdProvider: Send + Sync + std::fmt::Debug {
/// Returns the next ID for the subscription.
fn next_id(&self) -> SubscriptionId<'static>;
}
// Implement `IdProvider` for `Box<T>`
//
// It's not implemented for `&'_ T` because
// of the required `'static lifetime`
// Thus, `&dyn IdProvider` won't work.
impl<T: IdProvider + ?Sized> IdProvider for Box<T> {
fn next_id(&self) -> SubscriptionId<'static> {
(**self).next_id()
}
}