1use futu_core::error::{FutuError, Result};
2use futu_core::proto_id;
3use futu_net::client::FutuClient;
4
5use crate::types::Security;
6
7#[derive(Debug, Clone)]
9pub struct BrokerEntry {
10 pub id: i64,
11 pub name: String,
12 pub pos: i32,
13}
14
15impl BrokerEntry {
16 pub fn from_proto(b: &futu_proto::qot_common::Broker) -> Self {
17 Self {
18 id: b.id,
19 name: b.name.clone(),
20 pos: b.pos,
21 }
22 }
23}
24
25#[derive(Debug, Clone)]
27pub struct BrokerData {
28 pub security: Security,
29 pub ask_list: Vec<BrokerEntry>,
30 pub bid_list: Vec<BrokerEntry>,
31}
32
33pub async fn get_broker(client: &FutuClient, security: &Security) -> Result<BrokerData> {
37 let req = futu_proto::qot_get_broker::Request {
38 c2s: futu_proto::qot_get_broker::C2s {
39 security: security.to_proto(),
40 header: None,
41 },
42 };
43
44 let body = prost::Message::encode_to_vec(&req);
45 let resp_frame = client.request(proto_id::QOT_GET_BROKER, body).await?;
46
47 let resp: futu_proto::qot_get_broker::Response =
48 prost::Message::decode(resp_frame.body.as_ref()).map_err(FutuError::Proto)?;
49
50 if resp.ret_type != 0 {
51 return Err(crate::server_err(resp.ret_type, resp.ret_msg));
52 }
53
54 let s2c = resp
55 .s2c
56 .ok_or(FutuError::Codec("missing s2c in GetBroker".into()))?;
57
58 Ok(BrokerData {
59 security: Security::from_proto(&s2c.security),
60 ask_list: s2c
61 .broker_ask_list
62 .iter()
63 .map(BrokerEntry::from_proto)
64 .collect(),
65 bid_list: s2c
66 .broker_bid_list
67 .iter()
68 .map(BrokerEntry::from_proto)
69 .collect(),
70 })
71}