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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use super::{channel::OkxChannel, market::OkxMarket};
use crate::exchange::subscription::ExchangeSub;
use barter_integration::{error::SocketError, Validator};
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};

// Implement custom Serialize to assist aesthetics of <Okx as Connector>::requests() function.
impl Serialize for ExchangeSub<OkxChannel, OkxMarket> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("OkxSubArg", 2)?;
        state.serialize_field("channel", self.channel.as_ref())?;
        state.serialize_field("instId", self.market.as_ref())?;
        state.end()
    }
}

/// [`Okx`](super::Okx) WebSocket subscription response.
///
/// ### Raw Payload Examples
/// #### Subscription Trades Ok Response
/// ```json
/// {
///   "event": "subscribe",
///   "args": {
///     "channel": "trades",
///     "instId": "BTC-USD-191227"
///   }
/// }
/// ```
///
/// #### Subscription Trades Error Response
/// ```json
/// {
///   "event": "error",
///   "code": "60012",
///   "msg": "Invalid request: {\"op\": \"subscribe\", \"args\":[{ \"channel\" : \"trades\", \"instId\" : \"BTC-USD-191227\"}]}"
/// }
/// ```
///
/// See docs: <https://www.okx.com/docs-v5/en/#websocket-api-subscribe>
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
#[serde(tag = "event", rename_all = "lowercase")]
pub enum OkxSubResponse {
    #[serde(rename = "subscribe")]
    Subscribed,
    Error {
        code: String,
        #[serde(rename = "msg")]
        message: String,
    },
}

impl Validator for OkxSubResponse {
    fn validate(self) -> Result<Self, SocketError>
    where
        Self: Sized,
    {
        match self {
            Self::Subscribed => Ok(self),
            Self::Error { code, message } => Err(SocketError::Subscribe(format!(
                "received failure subscription response code: {code} with message: {message}",
            ))),
        }
    }
}

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

    mod de {
        use super::*;

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

            let cases = vec![
                TestCase {
                    // TC0: input response is subscription success
                    input: r#"
                {
                    "event": "subscribe",
                    "args": {"channel": "trades", "instId": "BTC-USD-191227"}
                }
                "#,
                    expected: Ok(OkxSubResponse::Subscribed),
                },
                TestCase {
                    // TC1: input response is failed subscription
                    input: r#"
                {
                    "event": "error",
                    "code": "60012",
                    "msg": "Invalid request: {\"op\": \"subscribe\", \"args\":[{ \"channel\" : \"trades\", \"instId\" : \"BTC-USD-191227\"}]}"
                }
                "#,
                    expected: Ok(OkxSubResponse::Error {
                        code: "60012".to_string(),
                        message: "Invalid request: {\"op\": \"subscribe\", \"args\":[{ \"channel\" : \"trades\", \"instId\" : \"BTC-USD-191227\"}]}".to_string()
                    }),
                },
            ];

            for (index, test) in cases.into_iter().enumerate() {
                let actual = serde_json::from_str::<OkxSubResponse>(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_okx_sub_response() {
        struct TestCase {
            input_response: OkxSubResponse,
            is_valid: bool,
        }

        let cases = vec![
            TestCase {
                // TC0: input response is subscription success
                input_response: OkxSubResponse::Subscribed,
                is_valid: true,
            },
            TestCase {
                // TC1: input response is failed subscription
                input_response: OkxSubResponse::Error {
                    code: "60012".to_string(),
                    message: "Invalid request: {\"op\": \"subscribe\", \"args\":[{ \"channel\" : \"trades\", \"instId\" : \"BTC-USD-191227\"}]}".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);
        }
    }
}