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