Skip to main content

brokapi_bcs_protocol/models/
instrument.rs

1//! The key that names an instrument in WebSocket subscriptions.
2
3use serde::{Deserialize, Serialize};
4
5/// Names one instrument for the broker: a ticker and a class code (board).
6///
7/// The same ticker can trade on several boards (for example `SBER` on `TQBR`), so the broker always
8/// wants both. The class code is the code of the board on the exchange, `TQBR` for most Russian
9/// shares.
10///
11/// # Examples
12///
13/// ```
14/// use brokapi_bcs_protocol::models::instrument::InstrumentKey;
15///
16/// let key = InstrumentKey::new("SBER", "TQBR");
17/// assert_eq!(serde_json::to_string(&key)?, r#"{"ticker":"SBER","classCode":"TQBR"}"#);
18/// # Ok::<(), serde_json::Error>(())
19/// ```
20#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
21#[serde(rename_all = "camelCase")]
22pub struct InstrumentKey {
23    /// Exchange ticker, for example `SBER`.
24    pub ticker: String,
25    /// Class code (board) of the ticker, for example `TQBR`.
26    pub class_code: String,
27}
28
29impl InstrumentKey {
30    /// Builds a key from a ticker and a class code.
31    ///
32    /// # Examples
33    ///
34    /// ```
35    /// use brokapi_bcs_protocol::models::instrument::InstrumentKey;
36    ///
37    /// let key = InstrumentKey::new(String::from("GAZP"), "TQBR");
38    /// assert_eq!((key.ticker.as_str(), key.class_code.as_str()), ("GAZP", "TQBR"));
39    /// ```
40    pub fn new(ticker: impl Into<String>, class_code: impl Into<String>) -> Self {
41        Self {
42            ticker: ticker.into(),
43            class_code: class_code.into(),
44        }
45    }
46}