/*
 * Binance Derivatives Trading Portfolio Margin WebSocket Market Streams
 *
 * OpenAPI Specification for the Binance Derivatives Trading Portfolio Margin 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::derivatives_trading_portfolio_margin::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 UserDataStreamEventsResponse {
    #[serde(rename = "CONDITIONAL_ORDER_TRADE_UPDATE")]
    ConditionalOrderTradeUpdate(Box<models::ConditionalOrderTradeUpdate>),
    #[serde(rename = "ACCOUNT_CONFIG_UPDATE")]
    AccountConfigUpdate(Box<models::AccountConfigUpdate>),
    #[serde(rename = "ACCOUNT_UPDATE")]
    AccountUpdate(Box<models::AccountUpdate>),
    #[serde(rename = "ORDER_TRADE_UPDATE")]
    OrderTradeUpdate(Box<models::OrderTradeUpdate>),
    #[serde(rename = "liabilityChange")]
    LiabilityChange(Box<models::Liabilitychange>),
    #[serde(rename = "outboundAccountPosition")]
    OutboundAccountPosition(Box<models::Outboundaccountposition>),
    #[serde(rename = "balanceUpdate")]
    BalanceUpdate(Box<models::Balanceupdate>),
    #[serde(rename = "executionReport")]
    ExecutionReport(Box<models::Executionreport>),
    #[serde(rename = "openOrderLoss")]
    OpenOrderLoss(Box<models::Openorderloss>),
    #[serde(rename = "listenKeyExpired")]
    ListenKeyExpired(Box<models::Listenkeyexpired>),
    #[serde(rename = "riskLevelChange")]
    RiskLevelChange(Box<models::Risklevelchange>),
    Other(serde_json::Value),
}

impl Default for UserDataStreamEventsResponse {
    fn default() -> Self {
        Self::ConditionalOrderTradeUpdate(Default::default())
    }
}

impl TryFrom<Value> for UserDataStreamEventsResponse {
    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 {
            "CONDITIONAL_ORDER_TRADE_UPDATE" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::ConditionalOrderTradeUpdate(
                    Box::new(payload),
                ))
            }

            "ACCOUNT_CONFIG_UPDATE" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::AccountConfigUpdate(Box::new(
                    payload,
                )))
            }

            "ACCOUNT_UPDATE" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::AccountUpdate(Box::new(
                    payload,
                )))
            }

            "ORDER_TRADE_UPDATE" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::OrderTradeUpdate(Box::new(
                    payload,
                )))
            }

            "liabilityChange" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::LiabilityChange(Box::new(
                    payload,
                )))
            }

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

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

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

            "openOrderLoss" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::OpenOrderLoss(Box::new(
                    payload,
                )))
            }

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

            "riskLevelChange" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::RiskLevelChange(Box::new(
                    payload,
                )))
            }

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