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
use anyhow::anyhow;
use exc_core::{error::InstrumentError, ExchangeError};
use serde::Deserialize;
pub mod candle;
pub mod trading;
pub use candle::Candle;
pub use trading::OrderDetail;
#[derive(Debug, Deserialize)]
pub struct FullHttpResponse {
pub code: String,
pub msg: String,
#[serde(default)]
pub data: Vec<ResponseData>,
}
#[derive(Debug)]
pub struct HttpResponse {
pub data: Vec<ResponseData>,
}
impl TryFrom<FullHttpResponse> for HttpResponse {
type Error = ExchangeError;
fn try_from(full: FullHttpResponse) -> Result<Self, Self::Error> {
let code = full.code;
let msg = full.msg;
match code.as_str() {
"0" => Ok(Self { data: full.data }),
"51001" => Err(ExchangeError::Instrument(InstrumentError::NotFound)),
"50011" => Err(ExchangeError::RateLimited(anyhow!("{msg}"))),
"50013" => Err(ExchangeError::Unavailable(anyhow!("{msg}"))),
"51603" => Err(ExchangeError::OrderNotFound),
_ => Err(ExchangeError::Api(anyhow!("code={code} msg={msg}",))),
}
}
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum ResponseData {
Candle(Candle),
Order(Box<OrderDetail>),
}