cuprate_rpc_types/
rpc_call.rs

1//! RPC call metadata.
2
3//---------------------------------------------------------------------------------------------------- Import
4
5//---------------------------------------------------------------------------------------------------- RpcCall
6/// Metadata about an RPC call.
7///
8/// This trait describes some metadata about RPC requests.
9///
10/// It is implemented on all request types within:
11/// - [`crate::json`]
12/// - [`crate::other`]
13/// - [`crate::bin`]
14///
15/// See also [`RpcCallValue`] for a dynamic by-value version of this trait.
16pub trait RpcCall {
17    /// Is `true` if this RPC method should
18    /// only be allowed on local servers.
19    ///
20    /// If this is `false`, it should be
21    /// okay to execute the method even on restricted
22    /// RPC servers.
23    ///
24    /// ```rust
25    /// use cuprate_rpc_types::{RpcCall, json::*};
26    ///
27    /// // Allowed method, even on restricted RPC servers (18089).
28    /// assert!(!GetBlockCountRequest::IS_RESTRICTED);
29    ///
30    /// // Restricted methods, only allowed
31    /// // for unrestricted RPC servers (18081).
32    /// assert!(GetConnectionsRequest::IS_RESTRICTED);
33    /// ```
34    const IS_RESTRICTED: bool;
35
36    /// Is `true` if this RPC method has no inputs, i.e. it is a `struct` with no fields.
37    ///
38    /// ```rust
39    /// use cuprate_rpc_types::{RpcCall, json::*};
40    ///
41    /// assert!(GetBlockCountRequest::IS_EMPTY);
42    /// assert!(!OnGetBlockHashRequest::IS_EMPTY);
43    /// ```
44    const IS_EMPTY: bool;
45}
46
47//---------------------------------------------------------------------------------------------------- RpcCallValue
48/// By-value version of [`RpcCall`].
49///
50/// This trait is a mirror of [`RpcCall`],
51/// except it takes `self` by value instead
52/// of being a `const` property.
53///
54/// This exists for `enum`s where requests must be dynamically
55/// `match`ed like [`JsonRpcRequest`](crate::json::JsonRpcRequest).
56///
57/// All types that implement [`RpcCall`] automatically implement [`RpcCallValue`].
58pub trait RpcCallValue {
59    /// Same as [`RpcCall::IS_RESTRICTED`].
60    ///
61    /// ```rust
62    /// use cuprate_rpc_types::{RpcCallValue, json::*};
63    ///
64    /// assert!(!GetBlockCountRequest::default().is_restricted());
65    /// assert!(GetConnectionsRequest::default().is_restricted());
66    /// ```
67    fn is_restricted(&self) -> bool;
68
69    /// Same as [`RpcCall::IS_EMPTY`].
70    ///
71    /// ```rust
72    /// use cuprate_rpc_types::{RpcCallValue, json::*};
73    ///
74    /// assert!(GetBlockCountRequest::default().is_empty());
75    /// assert!(!OnGetBlockHashRequest::default().is_empty());
76    /// ```
77    fn is_empty(&self) -> bool;
78}
79
80impl<T: RpcCall> RpcCallValue for T {
81    #[inline]
82    fn is_restricted(&self) -> bool {
83        Self::IS_RESTRICTED
84    }
85
86    #[inline]
87    fn is_empty(&self) -> bool {
88        Self::IS_EMPTY
89    }
90}
91
92//---------------------------------------------------------------------------------------------------- Tests
93#[cfg(test)]
94mod test {
95    // use super::*;
96}