/*
 * Binance Derivatives Trading Portfolio Margin Pro WebSocket Market Streams
 *
 * OpenAPI Specification for the Binance Derivatives Trading Portfolio Margin Pro 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_pro::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 = "riskLevelChange")]
    RiskLevelChange(Box<models::Risklevelchange>),
    Other(serde_json::Value),
}

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

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