/*
 * 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 RiskDataStreamEventsResponse {
    #[serde(rename = "USER_LIABILITY_CHANGE")]
    UserLiabilityChange(Box<models::UserLiabilityChange>),
    #[serde(rename = "MARGIN_LEVEL_STATUS_CHANGE")]
    MarginLevelStatusChange(Box<models::MarginLevelStatusChange>),
    Other(serde_json::Value),
}

impl Default for RiskDataStreamEventsResponse {
    fn default() -> Self {
        Self::UserLiabilityChange(Default::default())
    }
}

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

            "MARGIN_LEVEL_STATUS_CHANGE" => {
                let payload = serde_json::from_value(v)?;
                Ok(RiskDataStreamEventsResponse::MarginLevelStatusChange(
                    Box::new(payload),
                ))
            }

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