forked from NethermindEth/nethermind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonRpcResult.cs
More file actions
65 lines (54 loc) · 1.94 KB
/
JsonRpcResult.cs
File metadata and controls
65 lines (54 loc) · 1.94 KB
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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Diagnostics.CodeAnalysis;
namespace Nethermind.JsonRpc
{
public readonly struct JsonRpcResult : IDisposable
{
[MemberNotNullWhen(true, nameof(BatchedResponses))]
[MemberNotNullWhen(false, nameof(SingleResponse))]
[MemberNotNullWhen(false, nameof(Response))]
[MemberNotNullWhen(false, nameof(Report))]
public bool IsCollection { get; }
public IJsonRpcBatchResult? BatchedResponses { get; }
public Entry? SingleResponse { get; }
public JsonRpcResponse? Response => SingleResponse?.Response;
public RpcReport? Report => SingleResponse?.Report;
private JsonRpcResult(IJsonRpcBatchResult batchedResponses)
{
IsCollection = true;
BatchedResponses = batchedResponses;
}
private JsonRpcResult(in Entry singleResult)
{
IsCollection = false;
SingleResponse = singleResult;
}
public static JsonRpcResult Single(JsonRpcResponse response, in RpcReport report)
=> new(new Entry(response, report));
public static JsonRpcResult Single(in Entry entry)
=> new(entry);
public static JsonRpcResult Collection(IJsonRpcBatchResult responses)
=> new(responses);
public readonly struct Entry : IDisposable
{
public JsonRpcResponse Response { get; }
public RpcReport Report { get; }
public Entry(JsonRpcResponse response, RpcReport report)
{
Response = response;
Report = report;
}
public void Dispose()
{
Response?.Dispose();
}
}
public void Dispose()
{
SingleResponse?.Dispose();
BatchedResponses?.Dispose();
}
}
}