longport/quote/
sub_flags.rs

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
use longport_proto::quote::SubType;

bitflags::bitflags! {
    /// Subscription flags
    #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
    pub struct SubFlags: u8 {
        /// Quote
        const QUOTE = 0x1;

        /// Depth
        const DEPTH = 0x2;

        /// Broker
        const BROKER = 0x4;

        /// Trade
        const TRADE = 0x8;
    }
}

impl From<SubFlags> for Vec<i32> {
    fn from(flags: SubFlags) -> Self {
        let mut sub_types = Vec::new();

        if flags.contains(SubFlags::QUOTE) {
            sub_types.push(SubType::Quote.into());
        }

        if flags.contains(SubFlags::DEPTH) {
            sub_types.push(SubType::Depth.into());
        }

        if flags.contains(SubFlags::BROKER) {
            sub_types.push(SubType::Brokers.into());
        }

        if flags.contains(SubFlags::TRADE) {
            sub_types.push(SubType::Trade.into());
        }

        sub_types
    }
}

impl From<Vec<i32>> for SubFlags {
    fn from(sub_types: Vec<i32>) -> Self {
        let mut flags = SubFlags::empty();

        for sub_type in sub_types {
            if let Ok(sub_type) = SubType::try_from(sub_type) {
                match sub_type {
                    SubType::Quote => flags |= SubFlags::QUOTE,
                    SubType::Depth => flags |= SubFlags::DEPTH,
                    SubType::Brokers => flags |= SubFlags::BROKER,
                    SubType::Trade => flags |= SubFlags::TRADE,
                    _ => {}
                }
            }
        }

        flags
    }
}