brokapi_bcs_protocol/models/types.rs
1//! Small value types used by several models.
2
3use rust_decimal::Decimal;
4use serde::{Deserialize, Serialize};
5
6/// An amount together with its settlement mode.
7///
8/// The settlement mode (`type`) is the term in which the trade is settled, for example `T0`, `T1` or
9/// `T365`. The broker docs describe it and give no closed list.
10///
11/// # Examples
12///
13/// ```
14/// use brokapi_bcs_protocol::models::types::QuantityWithType;
15///
16/// let q: QuantityWithType = serde_json::from_str(r#"{"type":"T0","value":12.50}"#)?;
17/// assert_eq!(q.settlement_type, "T0");
18/// assert_eq!(q.value.to_string(), "12.50");
19/// # Ok::<(), serde_json::Error>(())
20/// ```
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
22pub struct QuantityWithType {
23 /// Settlement mode, such as `T0` (JSON field `type`).
24 #[serde(rename = "type")]
25 pub settlement_type: String,
26 /// The amount, exactly as the broker wrote it.
27 #[serde(serialize_with = "rust_decimal::serde::arbitrary_precision::serialize")]
28 pub value: Decimal,
29}
30
31/// Page number and page size for the paged REST lists.
32///
33/// This is a helper for your own code: the client methods take `size` and `page` as separate
34/// arguments. The broker pages from 0, with `size` from 1 to 100 (default 50), so
35/// [`Pagination::default`] is the first page of 50.
36///
37/// # Examples
38///
39/// ```
40/// use brokapi_bcs_protocol::models::types::Pagination;
41///
42/// let first = Pagination::default();
43/// assert_eq!((first.size, first.page), (50, 0));
44/// let third = Pagination::new(100, 2);
45/// assert_eq!(third.page, 2);
46/// ```
47#[derive(Debug, Clone, Copy)]
48pub struct Pagination {
49 /// Records per page. The broker accepts 1 to 100.
50 pub size: u32,
51 /// Page number, counted from 0.
52 pub page: u32,
53}
54
55impl Default for Pagination {
56 fn default() -> Self {
57 Self { size: 50, page: 0 }
58 }
59}
60
61impl Pagination {
62 /// Builds a page request. Nothing is checked: a `size` outside 1 to 100 is refused by the broker.
63 pub fn new(size: u32, page: u32) -> Self {
64 Self { size, page }
65 }
66}
67
68/// A trading board: a class code on an exchange.
69///
70/// # Examples
71///
72/// ```
73/// use brokapi_bcs_protocol::models::types::Board;
74///
75/// let b: Board = serde_json::from_str(r#"{"classCode":"TQBR","exchange":"MOEX"}"#)?;
76/// assert_eq!(b.class_code, "TQBR");
77/// # Ok::<(), serde_json::Error>(())
78/// ```
79#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
80#[serde(rename_all = "camelCase")]
81pub struct Board {
82 /// Class code of the board, for example `TQBR`.
83 pub class_code: String,
84 /// Exchange code, for example `MOEX`.
85 pub exchange: String,
86}