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
// 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.

//! RPC parameters.

use crate::traits::ToRpcParams;
use crate::Error;
use serde::Serialize;
use serde_json::value::RawValue;

/// Helper module for building parameters.
mod params_builder {
	use serde::Serialize;

	/// Initial number of bytes for a parameter length.
	const PARAM_BYTES_CAPACITY: usize = 128;

	/// Generic parameter builder that serializes parameters to bytes.
	/// This produces a JSON compatible String.
	///
	/// The implementation relies on `Vec<u8>` to hold the serialized
	/// parameters in memory for the following reasons:
	///   1. Other serialization methods than `serde_json::to_writer` would internally
	///      have an extra heap allocation for temporarily holding the value in memory.
	///   2. `io::Write` is not implemented for `String` required for serialization.
	#[derive(Debug, Clone)]
	pub(crate) struct ParamsBuilder {
		bytes: Vec<u8>,
		start: char,
		end: char,
	}

	impl ParamsBuilder {
		/// Construct a new [`ParamsBuilder`] with custom start and end tokens.
		/// The inserted values are wrapped by the _start_ and _end_ tokens.
		fn new(start: char, end: char) -> Self {
			ParamsBuilder { bytes: Vec::new(), start, end }
		}

		/// Construct a new [`ParamsBuilder`] for positional parameters equivalent to a JSON array object.
		pub(crate) fn positional() -> Self {
			Self::new('[', ']')
		}

		/// Construct a new [`ParamsBuilder`] for named parameters equivalent to a JSON map object.
		pub(crate) fn named() -> Self {
			Self::new('{', '}')
		}

		/// Initialize the internal vector if it is empty:
		///  - allocate [`PARAM_BYTES_CAPACITY`] to avoid resizing
		///  - add the `start` character.
		///
		/// # Note
		///
		/// Initialization is needed prior to inserting elements.
		fn maybe_initialize(&mut self) {
			if self.bytes.is_empty() {
				self.bytes.reserve(PARAM_BYTES_CAPACITY);
				self.bytes.push(self.start as u8);
			}
		}

		/// Insert a named value (key, value) pair into the builder.
		/// The _name_ and _value_ are delimited by the `:` token.
		pub(crate) fn insert_named<P: Serialize>(&mut self, name: &str, value: P) -> Result<(), serde_json::Error> {
			self.maybe_initialize();

			serde_json::to_writer(&mut self.bytes, name)?;
			self.bytes.push(b':');
			serde_json::to_writer(&mut self.bytes, &value)?;
			self.bytes.push(b',');

			Ok(())
		}

		/// Insert a plain value into the builder.
		pub(crate) fn insert<P: Serialize>(&mut self, value: P) -> Result<(), serde_json::Error> {
			self.maybe_initialize();

			serde_json::to_writer(&mut self.bytes, &value)?;
			self.bytes.push(b',');

			Ok(())
		}

		/// Finish the building process and return a JSON compatible string.
		pub(crate) fn build(mut self) -> Option<String> {
			if self.bytes.is_empty() {
				return None;
			}

			let idx = self.bytes.len() - 1;
			if self.bytes[idx] == b',' {
				self.bytes[idx] = self.end as u8;
			} else {
				self.bytes.push(self.end as u8);
			}

			// Safety: This is safe because JSON does not emit invalid UTF-8.
			Some(unsafe { String::from_utf8_unchecked(self.bytes) })
		}
	}
}

/// Parameter builder that serializes named value parameters to a JSON compatible string.
/// This is the equivalent of a JSON Map object `{ key: value }`.
///
/// # Examples
///
/// ```rust
///
/// use jsonrpsee_core::params::ObjectParams;
///
/// let mut builder = ObjectParams::new();
/// builder.insert("param1", 1);
/// builder.insert("param2", "abc");
///
/// // Use RPC parameters...
/// ```
#[derive(Debug, Clone)]
pub struct ObjectParams(params_builder::ParamsBuilder);

impl ObjectParams {
	/// Construct a new [`ObjectParams`].
	pub fn new() -> Self {
		Self::default()
	}

	/// Insert a named value (key, value) pair into the builder.
	/// The _name_ and _value_ are delimited by the `:` token.
	pub fn insert<P: Serialize>(&mut self, name: &str, value: P) -> Result<(), serde_json::Error> {
		self.0.insert_named(name, value)
	}
}

impl Default for ObjectParams {
	fn default() -> Self {
		Self(params_builder::ParamsBuilder::named())
	}
}

impl ToRpcParams for ObjectParams {
	fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, Error> {
		if let Some(json) = self.0.build() {
			RawValue::from_string(json).map(Some).map_err(Error::ParseError)
		} else {
			Ok(None)
		}
	}
}

/// Parameter builder that serializes plain value parameters to a JSON compatible string.
/// This is the equivalent of a JSON Array object `[ value0, value1, .., valueN ]`.
///
/// # Examples
///
/// ```rust
///
/// use jsonrpsee_core::params::ArrayParams;
///
/// let mut builder = ArrayParams::new();
/// builder.insert("param1");
/// builder.insert(1);
///
/// // Use RPC parameters...
/// ```
#[derive(Debug, Clone)]
pub struct ArrayParams(params_builder::ParamsBuilder);

impl ArrayParams {
	/// Construct a new [`ArrayParams`].
	pub fn new() -> Self {
		Self::default()
	}

	/// Insert a plain value into the builder.
	pub fn insert<P: Serialize>(&mut self, value: P) -> Result<(), serde_json::Error> {
		self.0.insert(value)
	}
}

impl Default for ArrayParams {
	fn default() -> Self {
		Self(params_builder::ParamsBuilder::positional())
	}
}

impl ToRpcParams for ArrayParams {
	fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, Error> {
		if let Some(json) = self.0.build() {
			RawValue::from_string(json).map(Some).map_err(Error::ParseError)
		} else {
			Ok(None)
		}
	}
}

/// Initial number of parameters in a batch request.
const BATCH_PARAMS_NUM_CAPACITY: usize = 4;

/// Request builder that serializes RPC parameters to construct a valid batch parameter.
/// This is the equivalent of chaining multiple RPC requests.
#[derive(Clone, Debug, Default)]
pub struct BatchRequestBuilder<'a>(Vec<(&'a str, Option<Box<RawValue>>)>);

impl<'a> BatchRequestBuilder<'a> {
	/// Construct a new [`BatchRequestBuilder`].
	pub fn new() -> Self {
		Self(Vec::with_capacity(BATCH_PARAMS_NUM_CAPACITY))
	}

	/// Inserts the RPC method with provided parameters into the builder.
	pub fn insert<Params: ToRpcParams>(&mut self, method: &'a str, value: Params) -> Result<(), Error> {
		self.0.push((method, value.to_rpc_params()?));
		Ok(())
	}

	/// Finish the building process and return a valid batch parameter.
	#[allow(clippy::type_complexity)]
	pub fn build(self) -> Result<Vec<(&'a str, Option<Box<RawValue>>)>, Error> {
		if self.0.is_empty() {
			Err(Error::EmptyBatchRequest)
		} else {
			Ok(self.0)
		}
	}

	/// Get an iterator over the batch request.
	pub fn iter(&self) -> impl Iterator<Item = (&'a str, Option<&RawValue>)> {
		self.0.iter().map(|(method, params)| (*method, params.as_deref()))
	}
}

impl<'a> IntoIterator for BatchRequestBuilder<'a> {
	type Item = (&'a str, Option<Box<RawValue>>);
	type IntoIter = std::vec::IntoIter<Self::Item>;

	fn into_iter(self) -> Self::IntoIter {
		self.0.into_iter()
	}
}