Skip to content

Commit

Permalink
Make the TxFeeDetails tagged #782 (#783)
Browse files Browse the repository at this point in the history
* Make the TxFeeDetails tagged

* Impl the untagged deserialization for TxFeeDetails
  • Loading branch information
sergeyboyko0791 authored Dec 29, 2020
1 parent 014c4ea commit 233b5a4
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ use bigdecimal::BigDecimal;
use common::executor::{spawn, Timer};
use common::mm_ctx::{from_ctx, MmArc};
use common::mm_metrics::MetricsWeak;
use common::mm_number::MmNumber;
use common::{block_on, calc_total_pages, rpc_err_response, rpc_response, HyRes};
use futures::compat::Future01CompatExt;
use futures::lock::Mutex as AsyncMutex;
use futures01::Future;
use gstuff::slurp;
use http::Response;
use rpc::v1::types::Bytes as BytesJson;
use serde::{Deserialize, Deserializer};
use serde_json::{self as json, Value as Json};
use std::collections::hash_map::{HashMap, RawEntryMut};
use std::fmt;
Expand Down Expand Up @@ -76,8 +78,7 @@ use qrc20::{qrc20_addr_from_str, qrc20_coin_from_conf_and_request, Qrc20Coin, Qr
#[doc(hidden)]
#[allow(unused_variables)]
pub mod test_coin;
pub use self::test_coin::TestCoin;
use common::mm_number::MmNumber;
pub use test_coin::TestCoin;

pub trait Transaction: fmt::Debug + 'static {
/// Raw transaction bytes of the transaction
Expand Down Expand Up @@ -287,14 +288,38 @@ pub struct WithdrawRequest {
fee: Option<WithdrawFee>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
/// Please note that no type should have the same structure as another type,
/// because this enum has the `untagged` deserialization.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(tag = "type")]
pub enum TxFeeDetails {
Utxo(UtxoFeeDetails),
Eth(EthTxFeeDetails),
Qrc20(Qrc20FeeDetails),
}

/// Deserialize the TxFeeDetails as an untagged enum.
impl<'de> Deserialize<'de> for TxFeeDetails {
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum TxFeeDetailsUnTagged {
Utxo(UtxoFeeDetails),
Eth(EthTxFeeDetails),
Qrc20(Qrc20FeeDetails),
}

match Deserialize::deserialize(deserializer)? {
TxFeeDetailsUnTagged::Utxo(f) => Ok(TxFeeDetails::Utxo(f)),
TxFeeDetailsUnTagged::Eth(f) => Ok(TxFeeDetails::Eth(f)),
TxFeeDetailsUnTagged::Qrc20(f) => Ok(TxFeeDetails::Qrc20(f)),
}
}
}

impl Into<TxFeeDetails> for EthTxFeeDetails {
fn into(self: EthTxFeeDetails) -> TxFeeDetails { TxFeeDetails::Eth(self) }
}
Expand Down

0 comments on commit 233b5a4

Please sign in to comment.