/*
 * Binance Derivatives Trading USDS Futures WebSocket Market Streams
 *
 * OpenAPI Specification for the Binance Derivatives Trading USDS Futures 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_usds_futures::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 = "ACCOUNT_CONFIG_UPDATE")]
    AccountConfigUpdate(Box<models::AccountConfigUpdate>),
    #[serde(rename = "ALGO_UPDATE")]
    AlgoUpdate(Box<models::AlgoUpdate>),
    #[serde(rename = "ACCOUNT_UPDATE")]
    AccountUpdate(Box<models::AccountUpdate>),
    #[serde(rename = "CONDITIONAL_ORDER_TRIGGER_REJECT")]
    ConditionalOrderTriggerReject(Box<models::ConditionalOrderTriggerReject>),
    #[serde(rename = "GRID_UPDATE")]
    GridUpdate(Box<models::GridUpdate>),
    #[serde(rename = "MARGIN_CALL")]
    MarginCall(Box<models::MarginCall>),
    #[serde(rename = "ORDER_TRADE_UPDATE")]
    OrderTradeUpdate(Box<models::OrderTradeUpdate>),
    #[serde(rename = "STRATEGY_UPDATE")]
    StrategyUpdate(Box<models::StrategyUpdate>),
    #[serde(rename = "TRADE_LITE")]
    TradeLite(Box<models::TradeLite>),
    #[serde(rename = "listenKeyExpired")]
    ListenKeyExpired(Box<models::Listenkeyexpired>),
    Other(serde_json::Value),
}

impl Default for UserDataStreamEventsResponse {
    fn default() -> Self {
        Self::AccountConfigUpdate(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 {
            "ACCOUNT_CONFIG_UPDATE" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::AccountConfigUpdate(Box::new(
                    payload,
                )))
            }

            "ALGO_UPDATE" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::AlgoUpdate(Box::new(payload)))
            }

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

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

            "GRID_UPDATE" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::GridUpdate(Box::new(payload)))
            }

            "MARGIN_CALL" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::MarginCall(Box::new(payload)))
            }

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

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

            "TRADE_LITE" => {
                let payload = serde_json::from_value(v)?;
                Ok(UserDataStreamEventsResponse::TradeLite(Box::new(payload)))
            }

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

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