Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

derive serialize on more types #1797

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions subxt/src/backend/legacy/rpc_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,8 @@ pub type EncodedJustification = Vec<u8>;

/// This contains the runtime version information necessary to make transactions, as obtained from
/// the RPC call `state_getRuntimeVersion`,
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(serde::Serialize))]
pub struct RuntimeVersion {
/// Version of the runtime specification. A full-node will not attempt to use its native
/// runtime in substitute for the on-chain Wasm runtime unless all of `spec_name`,
Expand Down
2 changes: 1 addition & 1 deletion subxt/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ pub enum TransactionStatus<Hash> {

/// A response from calls like [`Backend::storage_fetch_values`] or
/// [`Backend::storage_fetch_descendant_values`].
#[cfg_attr(test, derive(serde::Serialize, Clone, PartialEq, Debug))]
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Debug)]
pub struct StorageResponse {
/// The key.
pub key: Vec<u8>,
Expand Down
127 changes: 46 additions & 81 deletions subxt/src/backend/unstable/rpc_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,9 @@ impl<T: Config> UnstableRpcMethods<T> {
///
/// The stop event indicates that the JSON-RPC server was unable to provide a consistent list of
/// the blocks at the head of the chain.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "event")]
#[cfg_attr(test, derive(Serialize))]
pub enum FollowEvent<Hash> {
/// The latest finalized block.
///
Expand Down Expand Up @@ -363,9 +362,8 @@ pub enum FollowEvent<Hash> {
///
/// This is the first event generated by the `follow` subscription
/// and is submitted only once.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(Serialize))]
#[cfg_attr(test, serde(rename_all = "camelCase"))]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Initialized<Hash> {
/// The hashes of the last finalized blocks.
pub finalized_block_hashes: Vec<Hash>,
Expand Down Expand Up @@ -404,10 +402,9 @@ impl<'de, Hash: Deserialize<'de>> Deserialize<'de> for Initialized<Hash> {

/// The runtime event generated if the `follow` subscription
/// has set the `with_runtime` flag.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "type")]
#[cfg_attr(test, derive(Serialize))]
pub enum RuntimeEvent {
/// The runtime version of this block.
Valid(RuntimeVersionEvent),
Expand All @@ -420,19 +417,17 @@ pub enum RuntimeEvent {
/// This event is generated for:
/// - the first announced block by the follow subscription
/// - blocks that suffered a change in runtime compared with their parents
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct RuntimeVersionEvent {
/// Details about this runtime.
pub spec: RuntimeSpec,
}

/// This contains the runtime version information necessary to make transactions, and is obtained from
/// the "initialized" event of `chainHead_follow` if the `withRuntime` flag is set.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct RuntimeSpec {
/// Opaque string indicating the name of the chain.
pub spec_name: String,
Expand All @@ -459,27 +454,21 @@ pub struct RuntimeSpec {
///
/// **Note:** In Substrate, the keys in the apis field consists of the hexadecimal-encoded 8-bytes blake2
/// hash of the name of the API. For example, the `TaggedTransactionQueue` API is 0xd2bc9897eed08f15.
#[serde(deserialize_with = "hashmap_as_tuple_list::deserialize")]
#[cfg_attr(
test,
serde(serialize_with = "hashmap_as_tuple_list::for_test::serialize")
)]
#[serde(with = "hashmap_as_tuple_list")]
pub apis: HashMap<String, u32>,
}

/// The operation could not be processed due to an error.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct ErrorEvent {
/// Reason of the error.
pub error: String,
}

/// Indicate a new non-finalized block.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct NewBlock<Hash> {
/// The hash of the new block.
pub block_hash: Hash,
Expand All @@ -495,18 +484,16 @@ pub struct NewBlock<Hash> {
}

/// Indicate the block hash of the new best block.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct BestBlockChanged<Hash> {
/// The block hash of the new best block.
pub best_block_hash: Hash,
}

/// Indicate the finalized and pruned block hashes.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct Finalized<Hash> {
/// Block hashes that are finalized.
pub finalized_block_hashes: Vec<Hash>,
Expand All @@ -515,18 +502,16 @@ pub struct Finalized<Hash> {
}

/// Indicate the operation id of the event.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationId {
/// The operation id of the event.
pub operation_id: String,
}

/// The response of the `chainHead_body` method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationBodyDone {
/// The operation id of the event.
pub operation_id: String,
Expand All @@ -535,9 +520,8 @@ pub struct OperationBodyDone {
}

/// The response of the `chainHead_call` method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationCallDone {
/// The operation id of the event.
pub operation_id: String,
Expand All @@ -546,9 +530,8 @@ pub struct OperationCallDone {
}

/// The response of the `chainHead_call` method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationStorageItems {
/// The operation id of the event.
pub operation_id: String,
Expand All @@ -557,9 +540,8 @@ pub struct OperationStorageItems {
}

/// Indicate a problem during the operation.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationError {
/// The operation id of the event.
pub operation_id: String,
Expand All @@ -568,9 +550,8 @@ pub struct OperationError {
}

/// The storage result.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct StorageResult {
/// The hex-encoded key of the result.
pub key: Bytes,
Expand All @@ -580,9 +561,8 @@ pub struct StorageResult {
}

/// The type of the storage query.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub enum StorageResultType {
/// Fetch the value of the provided key.
Value(Bytes),
Expand All @@ -593,10 +573,9 @@ pub enum StorageResultType {
}

/// The method response of `chainHead_body`, `chainHead_call` and `chainHead_storage`.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "result")]
#[cfg_attr(test, derive(Serialize))]
pub enum MethodResponse {
/// The method has started.
Started(MethodResponseStarted),
Expand All @@ -605,9 +584,8 @@ pub enum MethodResponse {
}

/// The `started` result of a method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct MethodResponseStarted {
/// The operation id of the response.
pub operation_id: String,
Expand Down Expand Up @@ -724,10 +702,9 @@ impl<Hash: BlockHash> Stream for TransactionSubscription<Hash> {
}

/// Transaction progress events
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "event")]
#[cfg_attr(test, derive(Serialize))]
pub enum TransactionStatus<Hash> {
/// Transaction is part of the future queue.
Validated,
Expand Down Expand Up @@ -768,17 +745,12 @@ pub enum TransactionStatus<Hash> {
}

/// Details of a block that a transaction is seen in.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct TransactionBlockDetails<Hash> {
/// The block hash.
pub hash: Hash,
/// The index of the transaction in the block.
#[serde(deserialize_with = "unsigned_number_as_string::deserialize")]
#[cfg_attr(
test,
serde(serialize_with = "unsigned_number_as_string::for_test::serialize")
)]
#[serde(with = "unsigned_number_as_string")]
pub index: u64,
}

Expand Down Expand Up @@ -833,17 +805,15 @@ pub(crate) mod unsigned_number_as_string {
Ok(v.into())
}
}
#[cfg(test)]
pub mod for_test {
use serde::ser::Serializer;

/// Serialize a number as string
pub fn serialize<S>(item: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&item.to_string())
}
use serde::ser::Serializer;

/// Serialize a number as string
pub fn serialize<S>(item: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&item.to_string())
}
}

Expand Down Expand Up @@ -921,27 +891,22 @@ pub(crate) mod hashmap_as_tuple_list {
Ok(map)
}
}
#[cfg(test)]
pub mod for_test {
use std::collections::HashMap;
use std::hash::Hash;

use serde::ser::{Serialize, SerializeSeq, Serializer};

/// Serialize hashmap as list of tuples
pub fn serialize<S, K: Eq + Hash + Serialize, V: Serialize>(
item: &HashMap<K, V>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(None)?;
for i in item {
seq.serialize_element(&i)?;
}
seq.end()

use serde::ser::{Serialize, SerializeSeq, Serializer};

/// Serialize hashmap as list of tuples
pub fn serialize<S, K: Eq + Hash + Serialize, V: Serialize>(
item: &HashMap<K, V>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(None)?;
for i in item {
seq.serialize_element(&i)?;
}
seq.end()
}
}

Expand Down
Loading