Skip to main content

ckb_jsonrpc_types/
lib.rs

1//! Wrappers for JSON serialization.
2mod alert;
3mod block_template;
4mod blockchain;
5mod bytes;
6mod cell;
7mod debug;
8mod experiment;
9mod fee_estimator;
10mod fee_rate;
11mod fixed_bytes;
12mod indexer;
13mod info;
14mod json_schema;
15mod net;
16mod pool;
17mod primitive;
18mod proposal_short_id;
19mod subscription;
20mod terminal;
21mod uints;
22
23#[cfg(test)]
24mod tests;
25
26pub use self::alert::{Alert, AlertId, AlertMessage, AlertPriority};
27pub use self::block_template::{
28    BlockTemplate, CellbaseTemplate, TransactionTemplate, UncleTemplate,
29};
30pub use self::blockchain::{
31    Block, BlockEconomicState, BlockFilter, BlockIssuance, BlockResponse, BlockView,
32    BlockWithCyclesResponse, CellDep, CellInput, CellOutput, Consensus, DepType, Deployment,
33    EpochView, FeeRateStatistics, HardForkFeature, HardForks, Header, HeaderView, MerkleProof,
34    MinerReward, OutPoint, ProposalWindow, Ratio, Script, ScriptHashType, SoftFork, Status,
35    Transaction, TransactionAndWitnessProof, TransactionProof, TransactionView,
36    TransactionWithStatusResponse, TxStatus, UncleBlock, UncleBlockView,
37};
38pub use self::bytes::JsonBytes;
39pub use self::cell::{CellData, CellInfo, CellWithStatus};
40pub use self::debug::{ExtraLoggerConfig, MainLoggerConfig};
41pub use self::experiment::{DaoWithdrawingCalculationKind, EstimateCycles};
42pub use self::fee_estimator::EstimateMode;
43pub use self::fee_rate::FeeRateDef;
44pub use self::fixed_bytes::Byte32;
45pub use self::info::{ChainInfo, DeploymentInfo, DeploymentPos, DeploymentState, DeploymentsInfo};
46pub use self::net::{
47    BannedAddr, LocalNode, LocalNodeProtocol, NodeAddress, PeerSyncState, RemoteNode,
48    RemoteNodeProtocol, SyncState,
49};
50pub use self::pool::{
51    AncestorsScoreSortKey, EntryCompleted, OutputsValidator, PoolTransactionEntry,
52    PoolTransactionReject, PoolTxDetailInfo, RawTxPool, TxPoolEntries, TxPoolEntry, TxPoolIds,
53    TxPoolInfo,
54};
55pub use self::proposal_short_id::ProposalShortId;
56pub use self::subscription::{LogEntry, LogLevel, Topic};
57pub use self::terminal::{
58    CellsInfo, Disk, DiskUsage, Global, MiningInfo, Network, NetworkInfo, Overview, PeerInfo,
59    SysInfo, TerminalPoolInfo,
60};
61pub use self::uints::{Uint32, Uint64, Uint128};
62pub use ckb_types::core::RationalU256;
63pub use indexer::{
64    IndexerCell, IndexerCellType, IndexerCellsCapacity, IndexerOrder, IndexerPagination,
65    IndexerRange, IndexerScriptType, IndexerSearchKey, IndexerSearchKeyFilter, IndexerSearchMode,
66    IndexerTip, IndexerTx, IndexerTxWithCell, IndexerTxWithCells,
67};
68pub use primitive::{
69    AsEpochNumberWithFraction, BlockNumber, Capacity, Cycle, EpochNumber, EpochNumberWithFraction,
70    Timestamp, Version,
71};
72use schemars::JsonSchema;
73pub use serde::{Deserialize, Serialize};
74
75use ckb_types::bytes::Bytes;
76
77/// The enum `Either` with variants `Left` and `Right` is a general purpose
78/// sum type with two cases.
79#[derive(PartialEq, Eq, Hash, Debug, Serialize, Deserialize, Clone, JsonSchema)]
80#[serde(untagged)]
81pub enum Either<L, R> {
82    /// A value of type `L`.
83    Left(L),
84    /// A value of type `R`.
85    Right(R),
86}
87
88/// This is a wrapper for JSON serialization to select the format between Json and Hex.
89///
90/// ## Examples
91///
92/// `ResponseFormat<BlockView>` returns the block in its Json format or molecule serialized
93/// Hex format.
94#[derive(PartialEq, Eq, Hash, Debug, Serialize, Deserialize, Clone, JsonSchema)]
95#[serde(transparent)]
96pub struct ResponseFormat<V> {
97    /// The inner value.
98    pub inner: Either<V, JsonBytes>,
99}
100
101/// The enum `ResponseFormatInnerType` with variants `Json` and `Hex` is used to
102/// supply a format choice for the format of `ResponseFormatResponse.transaction`
103pub enum ResponseFormatInnerType {
104    /// Indicate the json format of `ResponseFormatResponse.transaction`
105    Json,
106    /// Indicate the hex format of `ResponseFormatResponse.transaction`
107    Hex,
108}
109
110impl<V> ResponseFormat<V> {
111    /// Return json format
112    pub fn json(json: V) -> Self {
113        ResponseFormat {
114            inner: Either::Left(json),
115        }
116    }
117
118    /// Return molecule serialized hex format
119    pub fn hex(raw: Bytes) -> Self {
120        ResponseFormat {
121            inner: Either::Right(JsonBytes::from_bytes(raw)),
122        }
123    }
124}