/*
 * Binance Margin Trading WebSocket Market Streams
 *
 * OpenAPI Specification for the Binance Margin Trading WebSocket Market Streams
 *
 * The version of the OpenAPI document: 1.0.0
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

#![allow(unused_imports)]
use crate::margin_trading::websocket_streams::models;
use serde::{Deserialize, Deserializer, Serialize, de::Error};
use serde_json::Value;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "Value")]
pub enum TradeDataStreamEventsResponse {
    #[serde(rename = "outboundAccountPosition")]
    OutboundAccountPosition(Box<models::Outboundaccountposition>),
    #[serde(rename = "balanceUpdate")]
    BalanceUpdate(Box<models::Balanceupdate>),
    #[serde(rename = "listenKeyExpired")]
    ListenKeyExpired(Box<models::Listenkeyexpired>),
    #[serde(rename = "executionReport")]
    ExecutionReport(Box<models::Executionreport>),
    #[serde(rename = "listStatus")]
    ListStatus(Box<models::Liststatus>),
    Other(serde_json::Value),
}

impl Default for TradeDataStreamEventsResponse {
    fn default() -> Self {
        Self::OutboundAccountPosition(Default::default())
    }
}

impl TryFrom<Value> for TradeDataStreamEventsResponse {
    type Error = serde_json::Error;

    fn try_from(v: Value) -> Result<Self, Self::Error> {
        let tag = v
            .get("e")
            .and_then(Value::as_str)
            .ok_or_else(|| serde_json::Error::custom("missing field `e`"))?;

        match tag {
            "outboundAccountPosition" => {
                let payload = serde_json::from_value(v)?;
                Ok(TradeDataStreamEventsResponse::OutboundAccountPosition(
                    Box::new(payload),
                ))
            }

            "balanceUpdate" => {
                let payload = serde_json::from_value(v)?;
                Ok(TradeDataStreamEventsResponse::BalanceUpdate(Box::new(
                    payload,
                )))
            }

            "listenKeyExpired" => {
                let payload = serde_json::from_value(v)?;
                Ok(TradeDataStreamEventsResponse::ListenKeyExpired(Box::new(
                    payload,
                )))
            }

            "executionReport" => {
                let payload = serde_json::from_value(v)?;
                Ok(TradeDataStreamEventsResponse::ExecutionReport(Box::new(
                    payload,
                )))
            }

            "listStatus" => {
                let payload = serde_json::from_value(v)?;
                Ok(TradeDataStreamEventsResponse::ListStatus(Box::new(payload)))
            }

            _ => Ok(TradeDataStreamEventsResponse::Other(v)),
        }
    }
}
