ckb-jsonrpc-types 1.1.1

Wrappers for JSON serialization
Documentation
//! Wrappers for JSON serialization.
mod alert;
mod block_template;
mod blockchain;
mod bytes;
mod cell;
mod debug;
mod experiment;
mod fee_estimator;
mod fee_rate;
mod fixed_bytes;
mod indexer;
mod info;
mod json_schema;
mod net;
mod pool;
mod primitive;
mod proposal_short_id;
mod subscription;
mod terminal;
mod uints;

#[cfg(test)]
mod tests;

pub use self::alert::{Alert, AlertId, AlertMessage, AlertPriority};
pub use self::block_template::{
    BlockTemplate, CellbaseTemplate, TransactionTemplate, UncleTemplate,
};
pub use self::blockchain::{
    Block, BlockEconomicState, BlockFilter, BlockIssuance, BlockResponse, BlockView,
    BlockWithCyclesResponse, CellDep, CellInput, CellOutput, Consensus, DepType, Deployment,
    EpochView, FeeRateStatistics, HardForkFeature, HardForks, Header, HeaderView, MerkleProof,
    MinerReward, OutPoint, ProposalWindow, Ratio, Script, ScriptHashType, SoftFork, Status,
    Transaction, TransactionAndWitnessProof, TransactionProof, TransactionView,
    TransactionWithStatusResponse, TxStatus, UncleBlock, UncleBlockView,
};
pub use self::bytes::JsonBytes;
pub use self::cell::{CellData, CellInfo, CellWithStatus};
pub use self::debug::{ExtraLoggerConfig, MainLoggerConfig};
pub use self::experiment::{DaoWithdrawingCalculationKind, EstimateCycles};
pub use self::fee_estimator::EstimateMode;
pub use self::fee_rate::FeeRateDef;
pub use self::fixed_bytes::Byte32;
pub use self::info::{ChainInfo, DeploymentInfo, DeploymentPos, DeploymentState, DeploymentsInfo};
pub use self::net::{
    BannedAddr, LocalNode, LocalNodeProtocol, NodeAddress, PeerSyncState, RemoteNode,
    RemoteNodeProtocol, SyncState,
};
pub use self::pool::{
    AncestorsScoreSortKey, EntryCompleted, OutputsValidator, PoolTransactionEntry,
    PoolTransactionReject, PoolTxDetailInfo, RawTxPool, TxPoolEntries, TxPoolEntry, TxPoolIds,
    TxPoolInfo,
};
pub use self::proposal_short_id::ProposalShortId;
pub use self::subscription::{LogEntry, LogLevel, Topic};
pub use self::terminal::{
    CellsInfo, Disk, DiskUsage, Global, MiningInfo, Network, NetworkInfo, Overview, PeerInfo,
    SysInfo, TerminalPoolInfo,
};
pub use self::uints::{Uint32, Uint64, Uint128};
pub use ckb_types::core::RationalU256;
pub use indexer::{
    IndexerCell, IndexerCellType, IndexerCellsCapacity, IndexerOrder, IndexerPagination,
    IndexerRange, IndexerScriptType, IndexerSearchKey, IndexerSearchKeyFilter, IndexerSearchMode,
    IndexerTip, IndexerTx, IndexerTxWithCell, IndexerTxWithCells,
};
pub use primitive::{
    AsEpochNumberWithFraction, BlockNumber, Capacity, Cycle, EpochNumber, EpochNumberWithFraction,
    Timestamp, Version,
};
use schemars::JsonSchema;
pub use serde::{Deserialize, Serialize};

use ckb_types::bytes::Bytes;

/// The enum `Either` with variants `Left` and `Right` is a general purpose
/// sum type with two cases.
#[derive(PartialEq, Eq, Hash, Debug, Serialize, Deserialize, Clone, JsonSchema)]
#[serde(untagged)]
pub enum Either<L, R> {
    /// A value of type `L`.
    Left(L),
    /// A value of type `R`.
    Right(R),
}

/// This is a wrapper for JSON serialization to select the format between Json and Hex.
///
/// ## Examples
///
/// `ResponseFormat<BlockView>` returns the block in its Json format or molecule serialized
/// Hex format.
#[derive(PartialEq, Eq, Hash, Debug, Serialize, Deserialize, Clone, JsonSchema)]
#[serde(transparent)]
pub struct ResponseFormat<V> {
    /// The inner value.
    pub inner: Either<V, JsonBytes>,
}

/// The enum `ResponseFormatInnerType` with variants `Json` and `Hex` is used to
/// supply a format choice for the format of `ResponseFormatResponse.transaction`
pub enum ResponseFormatInnerType {
    /// Indicate the json format of `ResponseFormatResponse.transaction`
    Json,
    /// Indicate the hex format of `ResponseFormatResponse.transaction`
    Hex,
}

impl<V> ResponseFormat<V> {
    /// Return json format
    pub fn json(json: V) -> Self {
        ResponseFormat {
            inner: Either::Left(json),
        }
    }

    /// Return molecule serialized hex format
    pub fn hex(raw: Bytes) -> Self {
        ResponseFormat {
            inner: Either::Right(JsonBytes::from_bytes(raw)),
        }
    }
}