1
// This file is part of Substrate.
2

            
3
// Copyright (C) Parity Technologies (UK) Ltd.
4
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5

            
6
// This program is free software: you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published by
8
// the Free Software Foundation, either version 3 of the License, or
9
// (at your option) any later version.
10

            
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
15

            
16
// You should have received a copy of the GNU General Public License
17
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18

            
19
//! Substrate JSON-RPC interface v2.
20
//!
21
//! Specification [document](https://paritytech.github.io/json-rpc-interface-spec/).
22

            
23
#![warn(missing_docs)]
24
#![deny(unused_crate_dependencies)]
25

            
26
use serde::{Deserialize, Serialize};
27
use sp_core::hexdisplay::{AsBytesRef, HexDisplay};
28

            
29
mod common;
30

            
31
pub mod archive;
32
pub mod chain_head;
33
pub mod chain_spec;
34
pub mod transaction;
35

            
36
/// Task executor that is being used by RPC subscriptions.
37
pub type SubscriptionTaskExecutor = std::sync::Arc<dyn sp_core::traits::SpawnNamed>;
38

            
39
/// The result of an RPC method.
40
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
41
#[serde(untagged)]
42
pub enum MethodResult {
43
	/// Method generated a result.
44
	Ok(MethodResultOk),
45
	/// Method encountered an error.
46
	Err(MethodResultErr),
47
}
48

            
49
impl MethodResult {
50
	/// Constructs a successful result.
51
	pub fn ok(result: impl Into<String>) -> MethodResult {
52
		MethodResult::Ok(MethodResultOk { success: true, result: result.into() })
53
	}
54

            
55
	/// Constructs an error result.
56
	pub fn err(error: impl Into<String>) -> MethodResult {
57
		MethodResult::Err(MethodResultErr { success: false, error: error.into() })
58
	}
59
}
60

            
61
/// The successful result of an RPC method.
62
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
63
#[serde(rename_all = "camelCase")]
64
pub struct MethodResultOk {
65
	/// Method was successful.
66
	success: bool,
67
	/// The result of the method.
68
	pub result: String,
69
}
70

            
71
/// The error result of an RPC method.
72
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
73
#[serde(rename_all = "camelCase")]
74
pub struct MethodResultErr {
75
	/// Method encountered an error.
76
	success: bool,
77
	/// The error of the method.
78
	pub error: String,
79
}
80

            
81
/// Util function to encode a value as a hex string
82
pub fn hex_string<Data: AsBytesRef>(data: &Data) -> String {
83
	format!("0x{:?}", HexDisplay::from(data))
84
}
85

            
86
#[cfg(test)]
87
mod tests {
88
	use super::*;
89

            
90
	#[test]
91
	fn method_result_ok() {
92
		let ok = MethodResult::ok("hello");
93

            
94
		let ser = serde_json::to_string(&ok).unwrap();
95
		let exp = r#"{"success":true,"result":"hello"}"#;
96
		assert_eq!(ser, exp);
97

            
98
		let ok_dec: MethodResult = serde_json::from_str(exp).unwrap();
99
		assert_eq!(ok_dec, ok);
100
	}
101

            
102
	#[test]
103
	fn method_result_error() {
104
		let ok = MethodResult::err("hello");
105

            
106
		let ser = serde_json::to_string(&ok).unwrap();
107
		let exp = r#"{"success":false,"error":"hello"}"#;
108
		assert_eq!(ser, exp);
109

            
110
		let ok_dec: MethodResult = serde_json::from_str(exp).unwrap();
111
		assert_eq!(ok_dec, ok);
112
	}
113
}