Skip to main content

moex_client/moex/
decode.rs

1use crate::models::{
2    Board, Candle, CandleBorder, Engine, Index, IndexAnalytics, Market, OrderbookLevel, SecStat,
3    Security, SecurityBoard, SecuritySnapshot, Trade, Turnover,
4};
5#[cfg(feature = "news")]
6use crate::models::{Event, SiteNews};
7#[cfg(feature = "history")]
8use crate::models::{HistoryDates, HistoryRecord};
9
10use super::MoexError;
11use super::constants::{
12    BOARDS_ENDPOINT_TEMPLATE, CANDLEBORDERS_ENDPOINT_TEMPLATE, CANDLES_ENDPOINT_TEMPLATE,
13    INDEX_ANALYTICS_ENDPOINT_TEMPLATE, MARKETS_ENDPOINT_TEMPLATE, ORDERBOOK_ENDPOINT_TEMPLATE,
14    SECSTATS_ENDPOINT_TEMPLATE, SECURITIES_ENDPOINT_TEMPLATE, SECURITY_BOARDS_ENDPOINT_TEMPLATE,
15    TRADES_ENDPOINT_TEMPLATE, TURNOVERS_ENDPOINT,
16};
17#[cfg(feature = "news")]
18use super::constants::{EVENTS_ENDPOINT, SITENEWS_ENDPOINT};
19#[cfg(feature = "history")]
20use super::constants::{HISTORY_DATES_ENDPOINT_TEMPLATE, HISTORY_ENDPOINT_TEMPLATE};
21use super::payload::{
22    decode_board_security_snapshots_json_with_endpoint, decode_boards_json_with_endpoint,
23    decode_candle_borders_json_with_endpoint, decode_candles_json_with_endpoint,
24    decode_engines_json_payload, decode_index_analytics_json_with_endpoint,
25    decode_indexes_json_payload, decode_markets_json_with_endpoint,
26    decode_orderbook_json_with_endpoint, decode_raw_table_rows_json_with_endpoint,
27    decode_raw_table_view_json_with_endpoint, decode_raw_tables_json_with_endpoint,
28    decode_secstats_json_with_endpoint, decode_securities_json_with_endpoint,
29    decode_security_boards_json_with_endpoint, decode_trades_json_with_endpoint,
30    decode_turnovers_json_with_endpoint,
31};
32#[cfg(feature = "news")]
33use super::payload::{decode_events_json_with_endpoint, decode_sitenews_json_with_endpoint};
34#[cfg(feature = "history")]
35use super::payload::{decode_history_dates_json_with_endpoint, decode_history_json_with_endpoint};
36
37pub use super::payload::{RawTableView, RawTables};
38
39/// Разобрать JSON-представление `indices` ISS в доменные типы.
40pub fn indexes_json(payload: &str) -> Result<Vec<Index>, MoexError> {
41    decode_indexes_json_payload(payload)
42}
43
44/// Разобрать JSON-представление `engines` ISS в доменные типы.
45pub fn engines_json(payload: &str) -> Result<Vec<Engine>, MoexError> {
46    decode_engines_json_payload(payload)
47}
48
49/// Разобрать JSON-представление `markets` ISS в доменные типы.
50pub fn markets_json(payload: &str) -> Result<Vec<Market>, MoexError> {
51    decode_markets_json_with_endpoint(payload, MARKETS_ENDPOINT_TEMPLATE)
52}
53
54/// Разобрать JSON-представление `boards` ISS в доменные типы.
55pub fn boards_json(payload: &str) -> Result<Vec<Board>, MoexError> {
56    decode_boards_json_with_endpoint(payload, BOARDS_ENDPOINT_TEMPLATE)
57}
58
59/// Разобрать JSON `boards` из `securities/{secid}` в доменные типы.
60pub fn security_boards_json(payload: &str) -> Result<Vec<SecurityBoard>, MoexError> {
61    decode_security_boards_json_with_endpoint(payload, SECURITY_BOARDS_ENDPOINT_TEMPLATE)
62}
63
64/// Разобрать JSON-представление `securities` ISS в доменные типы.
65pub fn securities_json(payload: &str) -> Result<Vec<Security>, MoexError> {
66    decode_securities_json_with_endpoint(payload, SECURITIES_ENDPOINT_TEMPLATE)
67}
68
69/// Разобрать JSON `securities+marketdata` в снимки инструментов.
70pub fn board_security_snapshots_json(payload: &str) -> Result<Vec<SecuritySnapshot>, MoexError> {
71    decode_board_security_snapshots_json_with_endpoint(payload, SECURITIES_ENDPOINT_TEMPLATE)
72}
73
74/// Разобрать JSON-представление `orderbook` ISS в доменные типы.
75pub fn orderbook_json(payload: &str) -> Result<Vec<OrderbookLevel>, MoexError> {
76    decode_orderbook_json_with_endpoint(payload, ORDERBOOK_ENDPOINT_TEMPLATE)
77}
78
79/// Разобрать JSON-представление `candleborders` ISS в доменные типы.
80pub fn candle_borders_json(payload: &str) -> Result<Vec<CandleBorder>, MoexError> {
81    decode_candle_borders_json_with_endpoint(payload, CANDLEBORDERS_ENDPOINT_TEMPLATE)
82}
83
84/// Разобрать JSON-представление `candles` ISS в доменные типы.
85pub fn candles_json(payload: &str) -> Result<Vec<Candle>, MoexError> {
86    decode_candles_json_with_endpoint(payload, CANDLES_ENDPOINT_TEMPLATE)
87}
88
89/// Разобрать JSON-представление `trades` ISS в доменные типы.
90pub fn trades_json(payload: &str) -> Result<Vec<Trade>, MoexError> {
91    decode_trades_json_with_endpoint(payload, TRADES_ENDPOINT_TEMPLATE)
92}
93
94/// Разобрать JSON-представление `analytics` ISS в доменные типы.
95pub fn index_analytics_json(payload: &str) -> Result<Vec<IndexAnalytics>, MoexError> {
96    decode_index_analytics_json_with_endpoint(payload, INDEX_ANALYTICS_ENDPOINT_TEMPLATE)
97}
98
99/// Разобрать JSON-представление `turnovers` ISS в доменные типы.
100pub fn turnovers_json(payload: &str) -> Result<Vec<Turnover>, MoexError> {
101    decode_turnovers_json_with_endpoint(payload, TURNOVERS_ENDPOINT)
102}
103
104/// Разобрать JSON-представление `secstats` ISS в доменные типы.
105pub fn secstats_json(payload: &str) -> Result<Vec<SecStat>, MoexError> {
106    decode_secstats_json_with_endpoint(payload, SECSTATS_ENDPOINT_TEMPLATE)
107}
108
109#[cfg(feature = "history")]
110/// Разобрать JSON-представление `history/.../dates` ISS в доменные типы.
111pub fn history_dates_json(payload: &str) -> Result<Vec<HistoryDates>, MoexError> {
112    decode_history_dates_json_with_endpoint(payload, HISTORY_DATES_ENDPOINT_TEMPLATE)
113}
114
115#[cfg(feature = "history")]
116/// Разобрать JSON-представление `history` ISS в доменные типы.
117pub fn history_json(payload: &str) -> Result<Vec<HistoryRecord>, MoexError> {
118    decode_history_json_with_endpoint(payload, HISTORY_ENDPOINT_TEMPLATE)
119}
120
121#[cfg(feature = "news")]
122/// Разобрать JSON-представление `sitenews` ISS в доменные типы.
123pub fn sitenews_json(payload: &str) -> Result<Vec<SiteNews>, MoexError> {
124    decode_sitenews_json_with_endpoint(payload, SITENEWS_ENDPOINT)
125}
126
127#[cfg(feature = "news")]
128/// Разобрать JSON-представление `events` ISS в доменные типы.
129pub fn events_json(payload: &str) -> Result<Vec<Event>, MoexError> {
130    decode_events_json_with_endpoint(payload, EVENTS_ENDPOINT)
131}
132
133/// Декодировать строки выбранной ISS-таблицы в пользовательский тип.
134///
135/// Аргумент `endpoint` используется для контекста ошибок.
136/// Если нужен только один блок, это самый прямой API.
137pub fn raw_table_rows_json<T>(
138    payload: &str,
139    endpoint: &str,
140    table: &str,
141) -> Result<Vec<T>, MoexError>
142where
143    T: serde::de::DeserializeOwned,
144{
145    decode_raw_table_rows_json_with_endpoint(payload, endpoint, table)
146}
147
148/// Декодировать таблицу ISS в borrowed-представление без `DeserializeOwned`.
149///
150/// Подходит для zero-copy чтения отдельных ячеек и ленивой десериализации.
151pub fn raw_table_view_json<'a>(
152    payload: &'a str,
153    endpoint: &str,
154    table: &str,
155) -> Result<RawTableView<'a>, MoexError> {
156    decode_raw_table_view_json_with_endpoint(payload, endpoint, table)
157}
158
159/// Подготовить верхнеуровневые блоки payload-а для декодирования нескольких таблиц.
160///
161/// Полезно, когда из одного payload-а нужно извлечь несколько таблиц
162/// без повторного разбора всего JSON.
163/// Дальше используйте [`RawTables::take_rows`] для поэтапного извлечения.
164pub fn raw_tables_json(payload: &str, endpoint: &str) -> Result<RawTables, MoexError> {
165    decode_raw_tables_json_with_endpoint(payload, endpoint)
166}