From bcd694a75bca62064d5ef10cb5dfe47dc4f5d0f6 Mon Sep 17 00:00:00 2001 From: "nobu.maeda" Date: Tue, 2 Jan 2024 23:41:36 +0800 Subject: [PATCH] Offer Reject will currently fail without Trade Engine Specifics. Automatically place in a placeholder in that case --- src/common/types.rs | 10 ++++++++++ src/trade_rsp/builder.rs | 19 ++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/common/types.rs b/src/common/types.rs index 84892ef..0325c20 100644 --- a/src/common/types.rs +++ b/src/common/types.rs @@ -36,6 +36,16 @@ impl dyn SerdeGenericTrait { } } +#[derive(Clone, Debug, Serialize, Deserialize)] +pub(crate) struct SerdeGenericsPlaceholder {} + +#[typetag::serde(name = "n3xB-placeholder")] +impl SerdeGenericTrait for SerdeGenericsPlaceholder { + fn any_ref(&self) -> &dyn Any { + self + } +} + #[derive( Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug, EnumString, Display, IntoStaticStr, )] diff --git a/src/trade_rsp/builder.rs b/src/trade_rsp/builder.rs index 2e1b731..55b16c1 100644 --- a/src/trade_rsp/builder.rs +++ b/src/trade_rsp/builder.rs @@ -1,6 +1,6 @@ use crate::common::{ error::{N3xbError, OfferInvalidReason}, - types::{EventIdString, SerdeGenericTrait}, + types::{EventIdString, SerdeGenericTrait, SerdeGenericsPlaceholder}, }; use super::{TradeResponse, TradeResponseStatus}; @@ -47,11 +47,13 @@ impl TradeResponseBuilder { pub fn build(&self) -> Result { let Some(offer_event_id) = self.offer_event_id.as_ref() else { - return Err(N3xbError::Simple("No Offer Event ID defined".to_string())); // TODO: Error handling? + return Err(N3xbError::Simple("No Offer Event ID defined".to_string())); + // TODO: Error handling? }; let Some(trade_response) = self.trade_response.as_ref() else { - return Err(N3xbError::Simple("No Trade Response defined".to_string())); // TODO: Error handling? + return Err(N3xbError::Simple("No Trade Response defined".to_string())); + // TODO: Error handling? }; let trade_response = trade_response.to_owned(); @@ -60,15 +62,18 @@ impl TradeResponseBuilder { // TODO: Error handling? } - let Some(trade_engine_specifics) = self.trade_engine_specifics.as_ref() else { - return Err(N3xbError::Simple("No Trade Engine Specifics defined".to_string())); - }; + let trade_engine_specifics = + if let Some(trade_engine_specifics) = self.trade_engine_specifics.as_ref() { + trade_engine_specifics.to_owned() + } else { + Box::new(SerdeGenericsPlaceholder {}) + }; let trade_rsp = TradeResponse { offer_event_id: offer_event_id.to_owned(), trade_response: trade_response, reject_reason: self.reject_reason.to_owned(), - trade_engine_specifics: trade_engine_specifics.to_owned(), + trade_engine_specifics: trade_engine_specifics, }; Ok(trade_rsp)