Skip to content

Commit 00c317b

Browse files
authored
Add RpcResponse result helper (#1719)
1 parent 8297e78 commit 00c317b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

utils/scarb-proc-macro-server-types/src/jsonrpc.rs

+18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ pub struct RpcResponse {
1818
pub error: Option<ResponseError>,
1919
}
2020

21+
impl RpcResponse {
22+
/// Converts this response into a [`Result`], containing either the successful result
23+
/// or the error that occurred during the RPC request.
24+
///
25+
/// # Returns
26+
/// * `Ok(serde_json::Value)` if the RPC request was successful and contains a result.
27+
/// * `Err(ResponseError)` if the RPC request failed and contains an error.
28+
pub fn into_result(self) -> Result<serde_json::Value, ResponseError> {
29+
match (self.result, self.error) {
30+
(_, Some(error)) => Err(error),
31+
(Some(response), None) => Ok(response),
32+
(None, None) => Err(ResponseError {
33+
message: "`RpcResponse` is missing both `result` and `error`.".to_string(),
34+
}),
35+
}
36+
}
37+
}
38+
2139
/// Describes errors that occurred during an RPC operation.
2240
///
2341
/// Provides an error message detailing what went wrong during the request.

0 commit comments

Comments
 (0)