1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use barter_integration::{error::SocketError, Validator};
use serde::{Deserialize, Serialize};

/// ### Raw Payload Examples
/// See docs: <https://www.bitmex.com/app/wsAPI#Response-Format>
/// #### Subscription response payload
/// ```json
/// {
///     "success": true,
///     "subscribe": "trade:XBTUSD",
///     "request": {
///         "op":"subscribe",
///         "args":[
///             "trade:XBTUSD"
///         ]
///     }
/// }
///```
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
pub struct BitmexSubResponse {
    success: bool,
    subscribe: String,
}

impl Validator for BitmexSubResponse {
    fn validate(self) -> Result<Self, SocketError>
    where
        Self: Sized,
    {
        if self.success {
            Ok(self)
        } else {
            Err(SocketError::Subscribe(format!(
                "received failure subscription response for {} subscription",
                self.subscribe
            )))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    mod de {
        use super::*;

        #[test]
        fn test_bitmex_sub_response() {
            struct TestCase {
                input: &'static str,
                expected: Result<BitmexSubResponse, SocketError>,
            }

            let cases = vec![
                TestCase {
                    // TC0: input response is Subscribed
                    input: r#"
                        {
                            "success": true,
                            "subscribe": "orderBookL2_25:XBTUSD",
                            "request": {
                                "op":"subscribe",
                                "args":[
                                    "orderBookL2_25:XBTUSD"
                                ]
                            }
                        }
                    "#,
                    expected: Ok(BitmexSubResponse {
                        success: true,
                        subscribe: "orderBookL2_25:XBTUSD".to_string(),
                    }),
                },
                TestCase {
                    // TC1: input response is failed subscription
                    input: r#"
                    {
                        "success": false,
                        "subscribe": "orderBookL2_25:XBTUSD"
                    }
                    "#,
                    expected: Ok(BitmexSubResponse {
                        success: false,
                        subscribe: "orderBookL2_25:XBTUSD".to_string(),
                    }),
                },
            ];

            for (index, test) in cases.into_iter().enumerate() {
                let actual = serde_json::from_str::<BitmexSubResponse>(test.input);
                match (actual, test.expected) {
                    (Ok(actual), Ok(expected)) => {
                        assert_eq!(actual, expected, "TC{} failed", index)
                    }
                    (Err(_), Err(_)) => {
                        // Test passed
                    }
                    (actual, expected) => {
                        // Test failed
                        panic!("TC{index} failed because actual != expected. \nActual: {actual:?}\nExpected: {expected:?}\n");
                    }
                }
            }
        }
    }

    #[test]
    fn test_validate_bitmex_sub_response() {
        struct TestCase {
            input_response: BitmexSubResponse,
            is_valid: bool,
        }

        let cases = vec![
            TestCase {
                // TC0: input response is successful subscription
                input_response: BitmexSubResponse {
                    success: true,
                    subscribe: "orderBookL2_25:XBTUSD".to_string(),
                },
                is_valid: true,
            },
            TestCase {
                // TC1: input response is failed subscription
                input_response: BitmexSubResponse {
                    success: false,
                    subscribe: "orderBookL2_25:XBTUSD".to_string(),
                },
                is_valid: false,
            },
        ];

        for (index, test) in cases.into_iter().enumerate() {
            let actual = test.input_response.validate().is_ok();
            assert_eq!(actual, test.is_valid, "TestCase {} failed", index);
        }
    }
}