Skip to content

Commit

Permalink
TypedScQuery refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Jul 13, 2023
1 parent b403baf commit d0906c9
Showing 1 changed file with 9 additions and 37 deletions.
46 changes: 9 additions & 37 deletions framework/scenario/src/scenario/model/step/typed_sc_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,55 @@ use std::marker::PhantomData;

use crate::multiversx_sc::codec::{CodecFrom, TopEncodeMulti};

use crate::scenario::model::{AddressValue, BytesValue, TxExpect, TxQuery};
use crate::scenario::model::{AddressValue, BytesValue, TxExpect};

use super::ScQueryStep;

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct TypedScQuery<OriginalResult> {
pub id: String,
pub tx_id: Option<String>,
pub comment: Option<String>,
pub tx: Box<TxQuery>,
pub expect: Option<TxExpect>,
pub sc_query_step: ScQueryStep,
_return_type: PhantomData<OriginalResult>,
}

impl<OriginalResult> Default for TypedScQuery<OriginalResult> {
fn default() -> Self {
Self {
id: Default::default(),
tx_id: Default::default(),
comment: Default::default(),
tx: Default::default(),
expect: Default::default(),
_return_type: PhantomData,
}
}
}

impl<OriginalResult> From<TypedScQuery<OriginalResult>> for ScQueryStep {
fn from(typed: TypedScQuery<OriginalResult>) -> Self {
Self {
id: typed.id,
tx_id: typed.tx_id,
explicit_tx_hash: None,
comment: typed.comment,
tx: typed.tx,
expect: typed.expect,
}
typed.sc_query_step
}
}

impl<OriginalResult> From<ScQueryStep> for TypedScQuery<OriginalResult> {
fn from(untyped: ScQueryStep) -> Self {
Self {
id: untyped.id,
tx_id: untyped.tx_id,
comment: untyped.comment,
tx: untyped.tx,
expect: untyped.expect,
sc_query_step: untyped,
_return_type: PhantomData,
}
}
}

impl<OriginalResult> TypedScQuery<OriginalResult> {
pub fn function(mut self, expr: &str) -> Self {
self.tx.function = expr.to_string();
self.sc_query_step.tx.function = expr.to_string();
self
}

pub fn argument<A>(mut self, expr: A) -> Self
where
BytesValue: From<A>,
{
self.tx.arguments.push(BytesValue::from(expr));
self.sc_query_step.tx.arguments.push(BytesValue::from(expr));
self
}

pub fn to<A>(mut self, address: A) -> Self
where
AddressValue: From<A>,
{
self.tx.to = AddressValue::from(address);
self.sc_query_step.tx.to = AddressValue::from(address);
self
}

pub fn expect(mut self, expect: TxExpect) -> Self {
self.expect = Some(expect);
self.sc_query_step.expect = Some(expect);
self
}
}
Expand Down

0 comments on commit d0906c9

Please sign in to comment.