Skip to main content

deribit_http/model/
margin_model.rs

1//! Margin model types for Deribit API
2//!
3//! This module contains types for margin model configuration.
4
5use serde::{Deserialize, Serialize};
6
7/// Available margin models
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9#[serde(rename_all = "snake_case")]
10pub enum MarginModel {
11    /// Cross Portfolio Margin
12    CrossPm,
13    /// Segregated Portfolio Margin
14    SegregatedPm,
15    /// Cross Standard Margin
16    CrossSm,
17    /// Segregated Standard Margin
18    SegregatedSm,
19}
20
21impl MarginModel {
22    /// Returns the margin model as a string for API requests
23    #[must_use]
24    pub fn as_str(&self) -> &'static str {
25        match self {
26            Self::CrossPm => "cross_pm",
27            Self::SegregatedPm => "segregated_pm",
28            Self::CrossSm => "cross_sm",
29            Self::SegregatedSm => "segregated_sm",
30        }
31    }
32}
33
34impl std::fmt::Display for MarginModel {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        write!(f, "{}", self.as_str())
37    }
38}
39
40/// Response for change_margin_model endpoint
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42pub struct ChangeMarginModelResponse {
43    /// The new margin model
44    pub margin_model: String,
45    /// Whether the change was successful
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub success: Option<bool>,
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_margin_model_serialization() {
56        let model = MarginModel::CrossPm;
57        let json = serde_json::to_string(&model).expect("Failed to serialize");
58        assert_eq!(json, "\"cross_pm\"");
59    }
60
61    #[test]
62    fn test_margin_model_deserialization() {
63        let json = "\"segregated_pm\"";
64        let model: MarginModel = serde_json::from_str(json).expect("Failed to parse");
65        assert_eq!(model, MarginModel::SegregatedPm);
66    }
67
68    #[test]
69    fn test_margin_model_as_str() {
70        assert_eq!(MarginModel::CrossSm.as_str(), "cross_sm");
71        assert_eq!(MarginModel::SegregatedSm.as_str(), "segregated_sm");
72    }
73
74    #[test]
75    fn test_change_margin_model_response() {
76        let json = r#"{
77            "margin_model": "cross_pm",
78            "success": true
79        }"#;
80
81        let response: ChangeMarginModelResponse =
82            serde_json::from_str(json).expect("Failed to parse");
83        assert_eq!(response.margin_model, "cross_pm");
84        assert_eq!(response.success, Some(true));
85    }
86}