moex-client 0.1.0

Strictly-typed, unofficial Rust client for Moscow Exchange ISS API.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
//! Клиент для HTTP-взаимодействия с ISS API и ошибки транспортного уровня.

#[cfg(any(feature = "async", feature = "blocking"))]
mod client;
mod constants;
mod convert;
pub mod decode;
mod payload;
mod wire;

use std::num::NonZeroU32;
use std::time::{Duration, Instant};

#[cfg(any(feature = "async", feature = "blocking"))]
use reqwest::{StatusCode, header::HeaderMap};
use thiserror::Error;

use crate::models::{
    BoardId, EngineName, IndexId, MarketName, ParseBoardError, ParseCandleBorderError,
    ParseCandleError, ParseEngineError, ParseEventError, ParseHistoryDatesError,
    ParseHistoryRecordError, ParseIndexAnalyticsError, ParseIndexError, ParseMarketError,
    ParseOrderbookError, ParseSecStatError, ParseSecurityBoardError, ParseSecurityError,
    ParseSecuritySnapshotError, ParseSiteNewsError, ParseTradeError, ParseTurnoverError, SecId,
};

/// Асинхронный ленивый paginator по страницам `history`.
#[cfg(all(feature = "async", feature = "history"))]
pub use client::AsyncHistoryPages;
/// Блокирующий ленивый paginator по страницам `history`.
#[cfg(all(feature = "blocking", feature = "history"))]
pub use client::HistoryPages;
/// Асинхронные HTTP-клиенты ISS API.
#[cfg(feature = "async")]
pub use client::{
    AsyncCandlesPages, AsyncGlobalSecuritiesPages, AsyncIndexAnalyticsPages,
    AsyncMarketSecuritiesPages, AsyncMarketTradesPages, AsyncMoexClient, AsyncMoexClientBuilder,
    AsyncOwnedBoardScope, AsyncOwnedEngineScope, AsyncOwnedIndexScope, AsyncOwnedMarketScope,
    AsyncOwnedMarketSecurityScope, AsyncOwnedSecurityResourceScope, AsyncOwnedSecurityScope,
    AsyncRawIssRequestBuilder, AsyncSecStatsPages, AsyncSecuritiesPages, AsyncTradesPages,
};
#[cfg(all(feature = "async", feature = "news"))]
/// Асинхронные paginator-ы новостных endpoint-ов.
pub use client::{AsyncEventsPages, AsyncSiteNewsPages};
/// Блокирующие HTTP-клиенты ISS API.
#[cfg(feature = "blocking")]
pub use client::{
    CandlesPages, GlobalSecuritiesPages, IndexAnalyticsPages, MarketSecuritiesPages,
    MarketTradesPages, OwnedBoardScope, OwnedEngineScope, OwnedIndexScope, OwnedMarketScope,
    OwnedMarketSecurityScope, OwnedSecurityResourceScope, OwnedSecurityScope, RawIssRequestBuilder,
    SecStatsPages, SecuritiesPages, TradesPages,
};
#[cfg(all(feature = "blocking", feature = "news"))]
/// Блокирующие paginator-ы новостных endpoint-ов.
pub use client::{EventsPages, SiteNewsPages};

/// Явное имя блокирующего ISS-клиента.
#[cfg(feature = "blocking")]
pub type BlockingMoexClient = client::BlockingMoexClient;
/// Явное имя builder-а блокирующего ISS-клиента.
#[cfg(feature = "blocking")]
pub type BlockingMoexClientBuilder = client::BlockingMoexClientBuilder;

/// Типизированный идентификатор ISS endpoint-а для raw-запросов.
///
/// Позволяет строить raw-запросы без ручной сборки path-строк.
#[derive(Debug, Clone, Copy)]
pub enum IssEndpoint<'a> {
    /// `/iss/statistics/engines/stock/markets/index/analytics.json` (`indices`).
    Indexes,
    /// `/iss/statistics/engines/stock/markets/index/analytics/{indexid}.json` (`analytics`).
    IndexAnalytics { indexid: &'a IndexId },
    /// `/iss/turnovers.json` (`turnovers`).
    Turnovers,
    /// `/iss/engines/{engine}/turnovers.json` (`turnovers`).
    EngineTurnovers { engine: &'a EngineName },
    /// `/iss/engines.json` (`engines`).
    Engines,
    /// `/iss/engines/{engine}/markets.json` (`markets`).
    Markets { engine: &'a EngineName },
    /// `/iss/engines/{engine}/markets/{market}/boards.json` (`boards`).
    Boards {
        engine: &'a EngineName,
        market: &'a MarketName,
    },
    /// `/iss/securities.json` (`securities`).
    GlobalSecurities,
    /// `/iss/securities/{secid}.json` (`securities`).
    SecurityInfo { security: &'a SecId },
    /// `/iss/securities/{secid}.json` (`boards`).
    SecurityBoards { security: &'a SecId },
    /// `/iss/engines/{engine}/markets/{market}/securities.json` (`securities`).
    MarketSecurities {
        engine: &'a EngineName,
        market: &'a MarketName,
    },
    /// `/iss/engines/{engine}/markets/{market}/securities/{secid}.json` (`securities`).
    MarketSecurityInfo {
        engine: &'a EngineName,
        market: &'a MarketName,
        security: &'a SecId,
    },
    /// `/iss/engines/{engine}/markets/{market}/orderbook.json` (`orderbook`).
    MarketOrderbook {
        engine: &'a EngineName,
        market: &'a MarketName,
    },
    /// `/iss/engines/{engine}/markets/{market}/trades.json` (`trades`).
    MarketTrades {
        engine: &'a EngineName,
        market: &'a MarketName,
    },
    /// `/iss/engines/{engine}/markets/{market}/secstats.json` (`secstats`).
    SecStats {
        engine: &'a EngineName,
        market: &'a MarketName,
    },
    /// `/iss/engines/{engine}/markets/{market}/boards/{board}/securities.json` (`securities`).
    Securities {
        engine: &'a EngineName,
        market: &'a MarketName,
        board: &'a BoardId,
    },
    /// `/iss/engines/{engine}/markets/{market}/boards/{board}/securities.json` (`securities,marketdata`).
    BoardSecuritySnapshots {
        engine: &'a EngineName,
        market: &'a MarketName,
        board: &'a BoardId,
    },
    /// `/iss/engines/{engine}/markets/{market}/boards/{board}/securities/{secid}/orderbook.json` (`orderbook`).
    Orderbook {
        engine: &'a EngineName,
        market: &'a MarketName,
        board: &'a BoardId,
        security: &'a SecId,
    },
    /// `/iss/engines/{engine}/markets/{market}/boards/{board}/securities/{secid}/trades.json` (`trades`).
    Trades {
        engine: &'a EngineName,
        market: &'a MarketName,
        board: &'a BoardId,
        security: &'a SecId,
    },
    /// `/iss/engines/{engine}/markets/{market}/boards/{board}/securities/{secid}/candles.json` (`candles`).
    Candles {
        engine: &'a EngineName,
        market: &'a MarketName,
        board: &'a BoardId,
        security: &'a SecId,
    },
    /// `/iss/engines/{engine}/markets/{market}/securities/{secid}/candleborders.json` (`borders`).
    CandleBorders {
        engine: &'a EngineName,
        market: &'a MarketName,
        security: &'a SecId,
    },
    /// `/iss/sitenews.json` (`sitenews`).
    #[cfg(feature = "news")]
    SiteNews,
    /// `/iss/events.json` (`events`).
    #[cfg(feature = "news")]
    Events,
    /// `/iss/history/engines/{engine}/markets/{market}/boards/{board}/securities/{secid}/dates.json` (`dates`).
    #[cfg(feature = "history")]
    HistoryDates {
        engine: &'a EngineName,
        market: &'a MarketName,
        board: &'a BoardId,
        security: &'a SecId,
    },
    /// `/iss/history/engines/{engine}/markets/{market}/boards/{board}/securities/{secid}.json` (`history`).
    #[cfg(feature = "history")]
    History {
        engine: &'a EngineName,
        market: &'a MarketName,
        board: &'a BoardId,
        security: &'a SecId,
    },
}

impl IssEndpoint<'_> {
    /// Построить относительный endpoint-path (`*.json`) для raw-запроса.
    pub fn path(self) -> String {
        match self {
            Self::Indexes => constants::INDEXES_ENDPOINT.to_owned(),
            Self::IndexAnalytics { indexid } => constants::index_analytics_endpoint(indexid),
            Self::Turnovers => constants::TURNOVERS_ENDPOINT.to_owned(),
            Self::EngineTurnovers { engine } => constants::engine_turnovers_endpoint(engine),
            Self::Engines => constants::ENGINES_ENDPOINT.to_owned(),
            Self::Markets { engine } => constants::markets_endpoint(engine),
            Self::Boards { engine, market } => constants::boards_endpoint(engine, market),
            Self::GlobalSecurities => constants::GLOBAL_SECURITIES_ENDPOINT.to_owned(),
            Self::SecurityInfo { security } | Self::SecurityBoards { security } => {
                constants::security_endpoint(security)
            }
            Self::MarketSecurities { engine, market } => {
                constants::market_securities_endpoint(engine, market)
            }
            Self::MarketSecurityInfo {
                engine,
                market,
                security,
            } => constants::market_security_endpoint(engine, market, security),
            Self::MarketOrderbook { engine, market } => {
                constants::market_orderbook_endpoint(engine, market)
            }
            Self::MarketTrades { engine, market } => {
                constants::market_trades_endpoint(engine, market)
            }
            Self::SecStats { engine, market } => constants::secstats_endpoint(engine, market),
            Self::Securities {
                engine,
                market,
                board,
            }
            | Self::BoardSecuritySnapshots {
                engine,
                market,
                board,
            } => constants::securities_endpoint(engine, market, board),
            Self::Orderbook {
                engine,
                market,
                board,
                security,
            } => constants::orderbook_endpoint(engine, market, board, security),
            Self::Trades {
                engine,
                market,
                board,
                security,
            } => constants::trades_endpoint(engine, market, board, security),
            Self::Candles {
                engine,
                market,
                board,
                security,
            } => constants::candles_endpoint(engine, market, board, security),
            Self::CandleBorders {
                engine,
                market,
                security,
            } => constants::candleborders_endpoint(engine, market, security),
            #[cfg(feature = "news")]
            Self::SiteNews => constants::SITENEWS_ENDPOINT.to_owned(),
            #[cfg(feature = "news")]
            Self::Events => constants::EVENTS_ENDPOINT.to_owned(),
            #[cfg(feature = "history")]
            Self::HistoryDates {
                engine,
                market,
                board,
                security,
            } => constants::history_dates_endpoint(engine, market, board, security),
            #[cfg(feature = "history")]
            Self::History {
                engine,
                market,
                board,
                security,
            } => constants::history_endpoint(engine, market, board, security),
        }
    }

    /// Таблица по умолчанию для `iss.only`, если endpoint описывает единственную цель выборки.
    pub fn default_table(self) -> Option<&'static str> {
        match self {
            Self::Indexes => Some("indices"),
            Self::IndexAnalytics { .. } => Some("analytics"),
            Self::Turnovers | Self::EngineTurnovers { .. } => Some("turnovers"),
            Self::Engines => Some("engines"),
            Self::Markets { .. } => Some("markets"),
            Self::Boards { .. } | Self::SecurityBoards { .. } => Some("boards"),
            Self::GlobalSecurities
            | Self::SecurityInfo { .. }
            | Self::MarketSecurities { .. }
            | Self::MarketSecurityInfo { .. }
            | Self::Securities { .. } => Some("securities"),
            Self::BoardSecuritySnapshots { .. } => Some("securities,marketdata"),
            Self::Orderbook { .. } | Self::MarketOrderbook { .. } => Some("orderbook"),
            Self::Trades { .. } | Self::MarketTrades { .. } => Some("trades"),
            Self::Candles { .. } => Some("candles"),
            Self::CandleBorders { .. } => Some("borders"),
            Self::SecStats { .. } => Some("secstats"),
            #[cfg(feature = "news")]
            Self::SiteNews => Some("sitenews"),
            #[cfg(feature = "news")]
            Self::Events => Some("events"),
            #[cfg(feature = "history")]
            Self::HistoryDates { .. } => Some("dates"),
            #[cfg(feature = "history")]
            Self::History { .. } => Some("history"),
        }
    }
}

/// Политика повторных попыток для операций с [`MoexError`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RetryPolicy {
    max_attempts: NonZeroU32,
    delay: Duration,
}

impl RetryPolicy {
    /// Создать политику повторов с числом попыток и delay по умолчанию.
    ///
    /// Значение delay по умолчанию — `400ms`.
    pub fn new(max_attempts: NonZeroU32) -> Self {
        Self {
            max_attempts,
            delay: Duration::from_millis(400),
        }
    }

    /// Установить фиксированную паузу между попытками.
    pub fn with_delay(mut self, delay: Duration) -> Self {
        self.delay = delay;
        self
    }

    /// Максимальное число попыток (включая первую).
    pub fn max_attempts(self) -> NonZeroU32 {
        self.max_attempts
    }

    /// Пауза между попытками.
    pub fn delay(self) -> Duration {
        self.delay
    }
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self::new(NonZeroU32::new(3).expect("retry policy default attempts must be non-zero"))
    }
}

/// Выполнить blocking-операцию с повтором retryable-ошибок.
///
/// Повтор выполняется только для [`MoexError::is_retryable`].
pub fn with_retry<T, F>(policy: RetryPolicy, mut action: F) -> Result<T, MoexError>
where
    F: FnMut() -> Result<T, MoexError>,
{
    let mut attempts_left = policy.max_attempts().get();
    loop {
        match action() {
            Ok(value) => return Ok(value),
            Err(error) if attempts_left > 1 && error.is_retryable() => {
                attempts_left -= 1;
                std::thread::sleep(policy.delay());
            }
            Err(error) => return Err(error),
        }
    }
}

/// Выполнить async-операцию с повтором retryable-ошибок.
///
/// `sleep` задаётся вызывающим кодом, чтобы библиотека не навязывала runtime.
#[cfg(feature = "async")]
pub async fn with_retry_async<T, F, Fut, S, SleepFut>(
    policy: RetryPolicy,
    mut action: F,
    mut sleep: S,
) -> Result<T, MoexError>
where
    F: FnMut() -> Fut,
    Fut: std::future::Future<Output = Result<T, MoexError>>,
    S: FnMut(Duration) -> SleepFut,
    SleepFut: std::future::Future<Output = ()>,
{
    let mut attempts_left = policy.max_attempts().get();
    loop {
        match action().await {
            Ok(value) => return Ok(value),
            Err(error) if attempts_left > 1 && error.is_retryable() => {
                attempts_left -= 1;
                sleep(policy.delay()).await;
            }
            Err(error) => return Err(error),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Ограничение частоты запросов.
///
/// Хранит минимальный интервал между последовательными запросами.
pub struct RateLimit {
    min_interval: Duration,
}

impl RateLimit {
    /// Создать limit из минимального интервала между запросами.
    pub fn every(min_interval: Duration) -> Self {
        Self { min_interval }
    }

    /// Создать limit из числа запросов в секунду.
    ///
    /// Интервал округляется вверх до целого числа наносекунд.
    pub fn per_second(requests_per_second: NonZeroU32) -> Self {
        let per_second_nanos: u128 = 1_000_000_000;
        let requests = u128::from(requests_per_second.get());
        let nanos = per_second_nanos.div_ceil(requests);
        let nanos = u64::try_from(nanos).unwrap_or(u64::MAX);
        Self::every(Duration::from_nanos(nanos))
    }

    /// Минимальный интервал между запросами.
    pub fn min_interval(self) -> Duration {
        self.min_interval
    }
}

#[derive(Debug, Clone)]
/// Состояние rate-limit для последовательности запросов.
pub struct RateLimiter {
    limit: RateLimit,
    next_allowed_at: Option<Instant>,
}

impl RateLimiter {
    /// Создать новый limiter с заданным ограничением.
    pub fn new(limit: RateLimit) -> Self {
        Self {
            limit,
            next_allowed_at: None,
        }
    }

    /// Текущая конфигурация ограничения.
    pub fn limit(&self) -> RateLimit {
        self.limit
    }

    /// Рассчитать задержку до следующего запроса и зарезервировать слот.
    pub fn reserve_delay(&mut self) -> Duration {
        self.reserve_delay_at(Instant::now())
    }

    fn reserve_delay_at(&mut self, now: Instant) -> Duration {
        let scheduled_at = match self.next_allowed_at {
            Some(next_allowed_at) if next_allowed_at > now => next_allowed_at,
            _ => now,
        };
        let delay = scheduled_at.saturating_duration_since(now);
        self.next_allowed_at = Some(scheduled_at + self.limit.min_interval);
        delay
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
/// Универсальный переключатель ISS-параметров со значениями `on/off`.
pub enum IssToggle {
    /// Значение `off`.
    #[default]
    Off,
    /// Значение `on`.
    On,
}

impl IssToggle {
    /// Вернуть wire-значение параметра (`on`/`off`).
    pub const fn as_query_value(self) -> &'static str {
        match self {
            Self::Off => "off",
            Self::On => "on",
        }
    }
}

impl From<bool> for IssToggle {
    fn from(value: bool) -> Self {
        if value { Self::On } else { Self::Off }
    }
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
/// Системные опции ISS-запроса (`iss.*`) для raw endpoint-ов.
pub struct IssRequestOptions {
    metadata: Option<IssToggle>,
    data: Option<IssToggle>,
    version: Option<IssToggle>,
    json: Option<Box<str>>,
}

impl IssRequestOptions {
    /// Создать пустой набор опций.
    pub fn new() -> Self {
        Self::default()
    }

    /// Установить `iss.meta`.
    pub fn metadata(mut self, metadata: IssToggle) -> Self {
        self.metadata = Some(metadata);
        self
    }

    /// Установить `iss.data`.
    pub fn data(mut self, data: IssToggle) -> Self {
        self.data = Some(data);
        self
    }

    /// Установить `iss.version`.
    pub fn version(mut self, version: IssToggle) -> Self {
        self.version = Some(version);
        self
    }

    /// Установить `iss.json`.
    pub fn json(mut self, json: impl Into<String>) -> Self {
        self.json = Some(json.into().into_boxed_str());
        self
    }

    /// Текущее значение `iss.meta`, если задано.
    pub fn metadata_value(&self) -> Option<IssToggle> {
        self.metadata
    }

    /// Текущее значение `iss.data`, если задано.
    pub fn data_value(&self) -> Option<IssToggle> {
        self.data
    }

    /// Текущее значение `iss.version`, если задано.
    pub fn version_value(&self) -> Option<IssToggle> {
        self.version
    }

    /// Текущее значение `iss.json`, если задано.
    pub fn json_value(&self) -> Option<&str> {
        self.json.as_deref()
    }
}

#[derive(Debug, Clone)]
/// HTTP-ответ raw ISS-запроса без дополнительной валидации статуса/формата.
#[cfg(any(feature = "async", feature = "blocking"))]
pub struct RawIssResponse {
    status: StatusCode,
    headers: HeaderMap,
    body: String,
}

#[cfg(any(feature = "async", feature = "blocking"))]
impl RawIssResponse {
    pub(crate) fn new(status: StatusCode, headers: HeaderMap, body: String) -> Self {
        Self {
            status,
            headers,
            body,
        }
    }

    /// HTTP-статус ответа.
    pub fn status(&self) -> StatusCode {
        self.status
    }

    /// HTTP-заголовки ответа.
    pub fn headers(&self) -> &HeaderMap {
        &self.headers
    }

    /// Полное тело ответа как строка.
    pub fn body(&self) -> &str {
        &self.body
    }

    /// Разобрать ответ на части (`status`, `headers`, `body`).
    pub fn into_parts(self) -> (StatusCode, HeaderMap, String) {
        (self.status, self.headers, self.body)
    }
}

/// Выполнить blocking-операцию c соблюдением [`RateLimiter`].
pub fn with_rate_limit<T, F>(limiter: &mut RateLimiter, action: F) -> T
where
    F: FnOnce() -> T,
{
    let delay = limiter.reserve_delay();
    if !delay.is_zero() {
        std::thread::sleep(delay);
    }
    action()
}

/// Выполнить async-операцию c соблюдением [`RateLimiter`].
///
/// `sleep` задаётся приложением, чтобы библиотека не требовала конкретный runtime.
#[cfg(feature = "async")]
pub async fn with_rate_limit_async<T, F, Fut, S, SleepFut>(
    limiter: &mut RateLimiter,
    action: F,
    mut sleep: S,
) -> T
where
    F: FnOnce() -> Fut,
    Fut: std::future::Future<Output = T>,
    S: FnMut(Duration) -> SleepFut,
    SleepFut: std::future::Future<Output = ()>,
{
    let delay = limiter.reserve_delay();
    if !delay.is_zero() {
        sleep(delay).await;
    }
    action().await
}

#[derive(Debug, Error)]
/// Ошибки выполнения запросов к ISS и конвертации wire-ответов в доменные типы.
pub enum MoexError {
    /// Некорректно задан базовый URL ISS.
    #[error("invalid base URL '{base_url}': {reason}")]
    InvalidBaseUrl {
        /// Строка базового URL, которую не удалось разобрать.
        base_url: &'static str,
        /// Подробность ошибки парсинга URL.
        reason: String,
    },
    /// Ошибка сборки `reqwest::blocking::Client`.
    #[cfg(any(feature = "async", feature = "blocking"))]
    #[error("failed to build HTTP client: {source}")]
    BuildHttpClient {
        /// Исходная ошибка HTTP-клиента.
        #[source]
        source: reqwest::Error,
    },
    /// Для async rate-limit не задана функция `sleep`.
    #[error(
        "async rate limit requires sleep function; set AsyncMoexClientBuilder::rate_limit_sleep(...)"
    )]
    MissingAsyncRateLimitSleep,
    /// Не удалось построить URL конкретного endpoint.
    #[error("failed to build URL for endpoint '{endpoint}': {reason}")]
    EndpointUrl {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Подробность ошибки построения URL.
        reason: String,
    },
    /// Для raw-запроса не задан endpoint-path.
    #[error("raw request path is not set")]
    MissingRawPath,
    /// Некорректно задан endpoint-path в raw-запросе.
    #[error("invalid raw request path '{path}': {reason}")]
    InvalidRawPath {
        /// Исходный endpoint-path.
        path: Box<str>,
        /// Деталь ошибки валидации path.
        reason: Box<str>,
    },
    /// В raw JSON-ответе отсутствует запрошенная таблица ISS.
    #[error("raw endpoint '{endpoint}' does not contain table '{table}'")]
    MissingRawTable {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Имя таблицы ISS (`history`, `trades`, `securities` и т.д.).
        table: Box<str>,
    },
    /// Строка raw-таблицы содержит число значений, отличное от числа колонок.
    #[error(
        "raw table '{table}' from endpoint '{endpoint}' has invalid row width at row {row}: expected {expected}, got {actual}"
    )]
    InvalidRawTableRowWidth {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Имя таблицы ISS.
        table: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Число колонок таблицы.
        expected: usize,
        /// Число значений в конкретной строке.
        actual: usize,
    },
    /// Не удалось декодировать строку raw-таблицы в пользовательский тип.
    #[error("failed to decode raw table '{table}' row {row} from endpoint '{endpoint}': {source}")]
    InvalidRawTableRow {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Имя таблицы ISS.
        table: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Исходная ошибка JSON-декодера.
        #[source]
        source: serde_json::Error,
    },
    /// Ошибка отправки HTTP-запроса до получения ответа.
    #[cfg(any(feature = "async", feature = "blocking"))]
    #[error("request to endpoint '{endpoint}' failed: {source}")]
    Request {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Исходная ошибка HTTP-клиента.
        #[source]
        source: reqwest::Error,
    },
    /// Endpoint вернул HTTP-статус вне диапазона `2xx`.
    #[cfg(any(feature = "async", feature = "blocking"))]
    #[error(
        "endpoint '{endpoint}' returned HTTP {status} (content-type={content_type:?}, prefix={body_prefix:?})"
    )]
    HttpStatus {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// HTTP-статус ответа.
        status: StatusCode,
        /// Значение HTTP `content-type`, если присутствует.
        content_type: Option<Box<str>>,
        /// Начало тела ответа для диагностики.
        body_prefix: Box<str>,
    },
    /// Ошибка чтения тела HTTP-ответа.
    #[cfg(any(feature = "async", feature = "blocking"))]
    #[error("failed to read endpoint '{endpoint}' response body: {source}")]
    ReadBody {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Исходная ошибка HTTP-клиента.
        #[source]
        source: reqwest::Error,
    },
    /// Ошибка десериализации JSON-пейлоада ISS.
    #[error("failed to decode endpoint '{endpoint}' JSON payload: {source}")]
    Decode {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Исходная ошибка JSON-декодера.
        #[source]
        source: serde_json::Error,
    },
    /// Endpoint вернул payload, не похожий на JSON.
    #[error(
        "endpoint '{endpoint}' returned non-JSON payload (content-type={content_type:?}, prefix={body_prefix:?})"
    )]
    NonJsonPayload {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Значение HTTP `content-type`, если присутствует.
        content_type: Option<Box<str>>,
        /// Начало тела ответа для диагностики.
        body_prefix: Box<str>,
    },
    /// В endpoint `securities/{secid}` пришло больше одной строки `securities`.
    #[error("endpoint '{endpoint}' returned unexpected security rows count: {row_count}")]
    UnexpectedSecurityRows {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Фактическое число строк в таблице `securities`.
        row_count: usize,
    },
    /// В history endpoint `.../dates` пришло больше одной строки `dates`.
    #[error("endpoint '{endpoint}' returned unexpected history dates rows count: {row_count}")]
    UnexpectedHistoryDatesRows {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Фактическое число строк в таблице `dates`.
        row_count: usize,
    },
    /// Ошибка преобразования строки таблицы `indices`.
    #[error("invalid index row {row} from endpoint '{endpoint}': {source}")]
    InvalidIndex {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseIndexError,
    },
    /// Ошибка преобразования строки таблицы `dates` из history endpoint.
    #[error("invalid history dates row {row} from endpoint '{endpoint}': {source}")]
    InvalidHistoryDates {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseHistoryDatesError,
    },
    /// Ошибка преобразования строки таблицы `history`.
    #[error("invalid history row {row} from endpoint '{endpoint}': {source}")]
    InvalidHistory {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseHistoryRecordError,
    },
    /// Ошибка преобразования строки таблицы `turnovers`.
    #[error("invalid turnover row {row} from endpoint '{endpoint}': {source}")]
    InvalidTurnover {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseTurnoverError,
    },
    /// Ошибка преобразования строки таблицы `sitenews`.
    #[error("invalid sitenews row {row} from endpoint '{endpoint}': {source}")]
    InvalidSiteNews {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseSiteNewsError,
    },
    /// Ошибка преобразования строки таблицы `events`.
    #[error("invalid events row {row} from endpoint '{endpoint}': {source}")]
    InvalidEvent {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseEventError,
    },
    /// Ошибка преобразования строки таблицы `secstats`.
    #[error("invalid secstats row {row} from endpoint '{endpoint}': {source}")]
    InvalidSecStat {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseSecStatError,
    },
    /// Ошибка преобразования строки таблицы `analytics`.
    #[error("invalid index analytics row {row} from endpoint '{endpoint}': {source}")]
    InvalidIndexAnalytics {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseIndexAnalyticsError,
    },
    /// Ошибка преобразования строки таблицы `engines`.
    #[error("invalid engine row {row} from endpoint '{endpoint}': {source}")]
    InvalidEngine {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseEngineError,
    },
    /// Ошибка преобразования строки таблицы `markets`.
    #[error("invalid market row {row} from endpoint '{endpoint}': {source}")]
    InvalidMarket {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseMarketError,
    },
    /// Ошибка преобразования строки таблицы `boards`.
    #[error("invalid board row {row} from endpoint '{endpoint}': {source}")]
    InvalidBoard {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseBoardError,
    },
    /// Ошибка преобразования строки таблицы `boards` в endpoint `securities/{secid}`.
    #[error("invalid security board row {row} from endpoint '{endpoint}': {source}")]
    InvalidSecurityBoard {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseSecurityBoardError,
    },
    /// Ошибка преобразования строки таблицы `securities`.
    #[error("invalid security row {row} from endpoint '{endpoint}': {source}")]
    InvalidSecurity {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseSecurityError,
    },
    /// Ошибка преобразования строки таблиц `securities`/`marketdata` в снимок инструмента.
    #[error("invalid security snapshot {table} row {row} from endpoint '{endpoint}': {source}")]
    InvalidSecuritySnapshot {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Имя таблицы ISS (`securities` или `marketdata`).
        table: &'static str,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseSecuritySnapshotError,
    },
    /// Ошибка преобразования строки таблицы `orderbook`.
    #[error("invalid orderbook row {row} from endpoint '{endpoint}': {source}")]
    InvalidOrderbook {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseOrderbookError,
    },
    /// Ошибка преобразования строки таблицы `borders`.
    #[error("invalid candle border row {row} from endpoint '{endpoint}': {source}")]
    InvalidCandleBorder {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseCandleBorderError,
    },
    /// Ошибка преобразования строки таблицы `candles`.
    #[error("invalid candle row {row} from endpoint '{endpoint}': {source}")]
    InvalidCandle {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseCandleError,
    },
    /// Ошибка преобразования строки таблицы `trades`.
    #[error("invalid trade row {row} from endpoint '{endpoint}': {source}")]
    InvalidTrade {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Индекс строки в таблице ISS.
        row: usize,
        /// Деталь ошибки парсинга доменной сущности.
        #[source]
        source: ParseTradeError,
    },
    /// Переполнение счётчика `start` при авто-пагинации ISS.
    #[error(
        "pagination overflow for endpoint '{endpoint}': start={start}, limit={limit} exceeds u32"
    )]
    PaginationOverflow {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Текущее значение `start`.
        start: u32,
        /// Размер страницы `limit`.
        limit: u32,
    },
    /// Обнаружен зацикленный ответ при авто-пагинации ISS.
    #[error(
        "pagination is stuck for endpoint '{endpoint}': repeated page at start={start}, limit={limit}"
    )]
    PaginationStuck {
        /// Относительный путь endpoint.
        endpoint: Box<str>,
        /// Текущее значение `start`.
        start: u32,
        /// Размер страницы `limit`.
        limit: u32,
    },
}

impl MoexError {
    /// Признак, что операцию обычно имеет смысл повторить с backoff.
    pub fn is_retryable(&self) -> bool {
        match self {
            #[cfg(any(feature = "async", feature = "blocking"))]
            Self::BuildHttpClient { .. } => false,
            #[cfg(any(feature = "async", feature = "blocking"))]
            Self::Request { source, .. } => {
                source.is_timeout()
                    || source.is_connect()
                    || source.status().is_some_and(is_retryable_status)
            }
            #[cfg(any(feature = "async", feature = "blocking"))]
            Self::ReadBody { .. } => true,
            #[cfg(any(feature = "async", feature = "blocking"))]
            Self::HttpStatus { status, .. } => is_retryable_status(*status),
            _ => false,
        }
    }

    /// HTTP-статус, если ошибка была получена после ответа сервера.
    #[cfg(any(feature = "async", feature = "blocking"))]
    pub fn status_code(&self) -> Option<StatusCode> {
        match self {
            Self::Request { source, .. } => source.status(),
            Self::HttpStatus { status, .. } => Some(*status),
            _ => None,
        }
    }

    /// Диагностический префикс тела ответа, если он сохранён в ошибке.
    pub fn response_body_prefix(&self) -> Option<&str> {
        match self {
            #[cfg(any(feature = "async", feature = "blocking"))]
            Self::HttpStatus { body_prefix, .. } | Self::NonJsonPayload { body_prefix, .. } => {
                Some(body_prefix)
            }
            #[cfg(not(any(feature = "async", feature = "blocking")))]
            Self::NonJsonPayload { body_prefix, .. } => Some(body_prefix),
            _ => None,
        }
    }
}

#[cfg(any(feature = "async", feature = "blocking"))]
fn is_retryable_status(status: StatusCode) -> bool {
    status == StatusCode::TOO_MANY_REQUESTS || status.is_server_error()
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RepeatPagePolicy {
    Error,
}

#[cfg(test)]
mod tests;