Skip to main content

longport/
types.rs

1use strum_macros::{Display, EnumString};
2
3/// Market
4#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, EnumString, Display)]
5pub enum Market {
6    /// Unknown
7    Unknown,
8    /// US market
9    US,
10    /// HK market
11    HK,
12    /// CN market
13    CN,
14    /// SG market
15    SG,
16    /// Crypto market
17    Crypto,
18}
19
20impl_default_for_enum_string!(Market);
21impl_serde_for_enum_string!(Market);
22
23/// Canonicalize a security symbol for the LongPort gateway.
24///
25/// HK symbols are carried without the leading zeros the LongPort apps
26/// display, so `00700.HK` becomes `700.HK`. Symbols of every other market are
27/// returned unchanged.
28///
29/// The padded form is not handled consistently. Measured against the gateway:
30/// `/v1/asset/stock?symbol=00700.HK` answers `200` with **no** positions
31/// instead of an error, while `700.HK` returns the position. The quote
32/// subscription path has always trimmed the symbol itself, and
33/// longport/developers#789 reports that the quote HTTP endpoints accept the
34/// padded form — that last part is the reporter's observation, not something
35/// measured here. Normalizing on the way out makes both spellings behave the
36/// same in every SDK call that goes through these builders.
37pub(crate) fn normalize_symbol(symbol: &str) -> &str {
38    match symbol.split_once('.') {
39        Some((code, market)) if market.eq_ignore_ascii_case("HK") => {
40            let trimmed = code.trim_start_matches('0');
41            // An all-zero code would trim away to nothing; leave such a symbol
42            // alone rather than sending a bare `.HK`.
43            if trimmed.is_empty() {
44                symbol
45            } else {
46                &symbol[code.len() - trimmed.len()..]
47            }
48        }
49        _ => symbol,
50    }
51}
52
53/// [`normalize_symbol`] for an owned symbol, reusing the allocation when
54/// nothing had to be trimmed.
55pub(crate) fn normalize_symbol_owned(symbol: impl Into<String>) -> String {
56    let symbol = symbol.into();
57    let normalized = normalize_symbol(&symbol);
58    if normalized.len() == symbol.len() {
59        symbol
60    } else {
61        normalized.to_string()
62    }
63}
64
65#[cfg(test)]
66mod normalize_symbol_tests {
67    use super::{normalize_symbol, normalize_symbol_owned};
68
69    #[test]
70    fn strips_leading_zeros_for_hk_only() {
71        assert_eq!(normalize_symbol("00700.HK"), "700.HK");
72        assert_eq!(normalize_symbol("700.HK"), "700.HK");
73        assert_eq!(normalize_symbol("00700.hk"), "700.hk");
74        // Other markets keep their symbol verbatim, zeros and all.
75        assert_eq!(normalize_symbol("AAPL.US"), "AAPL.US");
76        assert_eq!(normalize_symbol("000001.SZ"), "000001.SZ");
77        assert_eq!(normalize_symbol("0001.SG"), "0001.SG");
78        // No market suffix at all.
79        assert_eq!(normalize_symbol("700"), "700");
80        assert_eq!(normalize_symbol(""), "");
81    }
82
83    #[test]
84    fn owned_variant_matches_the_borrowed_one() {
85        for s in ["00700.HK", "700.HK", "AAPL.US", "000001.SZ", "0.HK", ""] {
86            assert_eq!(normalize_symbol_owned(s), normalize_symbol(s));
87        }
88    }
89
90    #[test]
91    fn leaves_an_all_zero_code_alone() {
92        assert_eq!(normalize_symbol("0.HK"), "0.HK");
93        assert_eq!(normalize_symbol("000.HK"), "000.HK");
94        assert_eq!(normalize_symbol(".HK"), ".HK");
95    }
96}