cbr-client 0.1.0

Strictly-typed, unofficial Rust client for downloading CBR data.
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
use std::{marker::PhantomData, num::NonZeroI32};

#[cfg(feature = "chrono")]
use chrono::{Datelike, NaiveDate, NaiveDateTime, Timelike};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use time::{Date, PrimitiveDateTime};

/// Ошибки валидации входных параметров.
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum InputError {
    /// Идентификатор должен быть строго положительным.
    #[error("{kind} must be strictly positive, got {value}")]
    NonPositiveId { kind: &'static str, value: i32 },
    /// Левая граница периода больше правой.
    #[error("year span start {start} must be less than or equal to end {end}")]
    InvalidYearSpan { start: i32, end: i32 },
    /// Родительская ссылка должна быть `-1` (корень) или положительным id.
    #[error("parent reference must be -1 (root) or positive id, got {value}")]
    InvalidParentRef { value: i32 },
    /// Значение chrono выходит за поддерживаемый диапазон.
    #[cfg(feature = "chrono")]
    #[error("chrono value is out of range for {kind}")]
    ChronoOutOfRange { kind: &'static str },
    /// Для ISO-формата поддерживается только точность до секунд.
    #[cfg(feature = "chrono")]
    #[error("sub-second precision is not supported for chrono conversion")]
    ChronoSubsecondPrecision,
}

/// Маркер домена идентификатора.
pub trait IdKind {
    /// Имя поля для текста ошибки.
    const NAME: &'static str;
}

/// Обобщённый строго типизированный идентификатор.
pub struct Id<K>(NonZeroI32, PhantomData<K>);

impl<K> Copy for Id<K> {}

impl<K> Clone for Id<K> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<K> std::fmt::Debug for Id<K> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("Id").field(&self.0.get()).finish()
    }
}

impl<K> PartialEq for Id<K> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<K> Eq for Id<K> {}

impl<K> std::hash::Hash for Id<K> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

impl<K> PartialOrd for Id<K> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<K> Ord for Id<K> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.cmp(&other.0)
    }
}

impl<K: IdKind> Id<K> {
    /// Создаёт идентификатор из целого числа.
    pub fn new(value: i32) -> Result<Self, InputError> {
        if value <= 0 {
            return Err(InputError::NonPositiveId {
                kind: K::NAME,
                value,
            });
        }

        let raw =
            NonZeroI32::new(value).expect("strictly positive value always produces NonZeroI32");
        Ok(Self(raw, PhantomData))
    }

    /// Создаёт идентификатор в `const`-контексте.
    ///
    /// Паникует на этапе компиляции, если `value <= 0`.
    #[must_use]
    #[inline]
    pub const fn new_const(value: i32) -> Self {
        if value <= 0 {
            panic!("identifier must be strictly positive");
        }

        match NonZeroI32::new(value) {
            Some(raw) => Self(raw, PhantomData),
            None => panic!("identifier must be strictly positive"),
        }
    }

    /// Возвращает исходное числовое значение идентификатора.
    #[must_use]
    #[inline]
    pub fn get(self) -> i32 {
        self.0.get()
    }
}

impl<K: IdKind> TryFrom<i32> for Id<K> {
    type Error = InputError;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl<K: IdKind> From<Id<K>> for i32 {
    fn from(value: Id<K>) -> Self {
        value.get()
    }
}

impl<K: IdKind> Serialize for Id<K> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_i32(self.get())
    }
}

impl<'de, K: IdKind> Deserialize<'de> for Id<K> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let raw = i32::deserialize(deserializer)?;
        Self::new(raw).map_err(serde::de::Error::custom)
    }
}

#[doc(hidden)]
pub enum PublicationIdKind {}
impl IdKind for PublicationIdKind {
    const NAME: &'static str = "publication_id";
}
/// Строго типизированный идентификатор публикации.
pub type PublicationId = Id<PublicationIdKind>;

#[doc(hidden)]
pub enum DatasetIdKind {}
impl IdKind for DatasetIdKind {
    const NAME: &'static str = "dataset_id";
}
/// Строго типизированный идентификатор показателя (`dataset`).
pub type DatasetId = Id<DatasetIdKind>;

#[doc(hidden)]
pub enum CategoryIdKind {}
impl IdKind for CategoryIdKind {
    const NAME: &'static str = "category_id";
}
/// Строго типизированный идентификатор категории.
pub type CategoryId = Id<CategoryIdKind>;

#[doc(hidden)]
pub enum IndicatorIdKind {}
impl IdKind for IndicatorIdKind {
    const NAME: &'static str = "indicator_id";
}
/// Строго типизированный идентификатор индикатора.
pub type IndicatorId = Id<IndicatorIdKind>;

#[doc(hidden)]
pub enum MeasureIdKind {}
impl IdKind for MeasureIdKind {
    const NAME: &'static str = "measure_id";
}
/// Строго типизированный идентификатор разреза (`measure`).
pub type MeasureId = Id<MeasureIdKind>;

#[doc(hidden)]
pub enum UnitIdKind {}
impl IdKind for UnitIdKind {
    const NAME: &'static str = "unit_id";
}
/// Строго типизированный идентификатор единицы измерения.
pub type UnitId = Id<UnitIdKind>;

#[doc(hidden)]
pub enum RowIdKind {}
impl IdKind for RowIdKind {
    const NAME: &'static str = "row_id";
}
/// Строго типизированный идентификатор строки данных.
pub type RowId = Id<RowIdKind>;

#[doc(hidden)]
pub enum PeriodIdKind {}
impl IdKind for PeriodIdKind {
    const NAME: &'static str = "period_id";
}
/// Строго типизированный идентификатор периода.
pub type PeriodId = Id<PeriodIdKind>;

#[doc(hidden)]
pub enum ColumnIdKind {}
impl IdKind for ColumnIdKind {
    const NAME: &'static str = "column_id";
}
/// Строго типизированный идентификатор колонки.
pub type ColumnId = Id<ColumnIdKind>;

#[doc(hidden)]
pub enum ElementIdKind {}
impl IdKind for ElementIdKind {
    const NAME: &'static str = "element_id";
}
/// Строго типизированный идентификатор элемента.
pub type ElementId = Id<ElementIdKind>;

/// Родительская ссылка с явным корнем (`-1`) или валидным id.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ParentRef<T> {
    /// Корневой элемент (`-1` в API).
    Root,
    /// Ссылка на родительский элемент.
    Id(T),
}

impl<T> ParentRef<T>
where
    T: TryFrom<i32, Error = InputError> + Copy,
{
    /// Создаёт ссылку из сырого значения API.
    pub fn new(value: i32) -> Result<Self, InputError> {
        match value {
            -1 => Ok(Self::Root),
            value if value <= 0 => Err(InputError::InvalidParentRef { value }),
            _ => T::try_from(value).map(Self::Id),
        }
    }

    /// Возвращает `true`, если это корневая ссылка.
    #[must_use]
    #[inline]
    pub fn is_root(self) -> bool {
        matches!(self, Self::Root)
    }
}

impl<T: Copy> ParentRef<T> {
    /// Возвращает id родителя, если ссылка не корневая.
    #[must_use]
    #[inline]
    pub fn id(self) -> Option<T> {
        match self {
            Self::Root => None,
            Self::Id(value) => Some(value),
        }
    }
}

impl<T> Serialize for ParentRef<T>
where
    T: Copy + Into<i32>,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let raw = match self {
            Self::Root => -1,
            Self::Id(value) => (*value).into(),
        };
        serializer.serialize_i32(raw)
    }
}

impl<'de, T> Deserialize<'de> for ParentRef<T>
where
    T: TryFrom<i32, Error = InputError> + Copy,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let raw = i32::deserialize(deserializer)?;
        Self::new(raw).map_err(serde::de::Error::custom)
    }
}

/// Календарный год.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Year(i32);

impl Year {
    /// Создаёт значение года.
    #[must_use]
    #[inline]
    pub const fn new(value: i32) -> Self {
        Self(value)
    }

    /// Возвращает исходное значение года.
    #[must_use]
    #[inline]
    pub fn get(self) -> i32 {
        self.0
    }
}

impl From<Year> for i32 {
    fn from(value: Year) -> Self {
        value.get()
    }
}

const ISO_DATETIME_FORMAT: &[time::format_description::FormatItem<'static>] =
    time::macros::format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
const DMY_DATE_FORMAT: &[time::format_description::FormatItem<'static>] =
    time::macros::format_description!("[day].[month].[year]");

/// Дата и время в формате `YYYY-MM-DDTHH:MM:SS`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IsoDateTime(PrimitiveDateTime);

impl IsoDateTime {
    /// Создаёт значение из `time::PrimitiveDateTime`.
    #[must_use]
    #[inline]
    pub const fn new(value: PrimitiveDateTime) -> Self {
        Self(value)
    }

    /// Парсит строку формата `YYYY-MM-DDTHH:MM:SS`.
    pub fn parse(value: &str) -> Result<Self, time::error::Parse> {
        PrimitiveDateTime::parse(value, ISO_DATETIME_FORMAT).map(Self)
    }

    /// Возвращает внутреннее представление.
    #[must_use]
    #[inline]
    pub const fn get(self) -> PrimitiveDateTime {
        self.0
    }

    /// Конвертирует значение `chrono::NaiveDateTime` в `IsoDateTime`.
    #[cfg(feature = "chrono")]
    pub fn try_from_chrono(value: NaiveDateTime) -> Result<Self, InputError> {
        Self::try_from(value)
    }

    /// Конвертирует в `chrono::NaiveDateTime`.
    #[cfg(feature = "chrono")]
    pub fn try_to_chrono(self) -> Result<NaiveDateTime, InputError> {
        let date = self.0.date();
        let chrono_date = NaiveDate::from_ymd_opt(
            date.year(),
            u32::from(u8::from(date.month())),
            u32::from(date.day()),
        )
        .ok_or(InputError::ChronoOutOfRange { kind: "date" })?;

        let time = self.0.time();
        chrono_date
            .and_hms_nano_opt(
                u32::from(time.hour()),
                u32::from(time.minute()),
                u32::from(time.second()),
                time.nanosecond(),
            )
            .ok_or(InputError::ChronoOutOfRange { kind: "datetime" })
    }
}

impl Serialize for IsoDateTime {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let value = self
            .0
            .format(ISO_DATETIME_FORMAT)
            .map_err(serde::ser::Error::custom)?;
        serializer.serialize_str(&value)
    }
}

impl<'de> Deserialize<'de> for IsoDateTime {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = <&str>::deserialize(deserializer)?;
        Self::parse(value).map_err(serde::de::Error::custom)
    }
}

impl std::fmt::Display for IsoDateTime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let value = self
            .0
            .format(ISO_DATETIME_FORMAT)
            .map_err(|_| std::fmt::Error)?;
        f.write_str(&value)
    }
}

/// Календарная дата в формате `DD.MM.YYYY`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DmyDate(Date);

impl DmyDate {
    /// Создаёт значение из `time::Date`.
    #[must_use]
    #[inline]
    pub const fn new(value: Date) -> Self {
        Self(value)
    }

    /// Парсит строку формата `DD.MM.YYYY`.
    pub fn parse(value: &str) -> Result<Self, time::error::Parse> {
        Date::parse(value, DMY_DATE_FORMAT).map(Self)
    }

    /// Возвращает внутреннее представление.
    #[must_use]
    #[inline]
    pub const fn get(self) -> Date {
        self.0
    }

    /// Конвертирует значение `chrono::NaiveDate` в `DmyDate`.
    #[cfg(feature = "chrono")]
    pub fn try_from_chrono(value: NaiveDate) -> Result<Self, InputError> {
        Self::try_from(value)
    }

    /// Конвертирует в `chrono::NaiveDate`.
    #[cfg(feature = "chrono")]
    pub fn try_to_chrono(self) -> Result<NaiveDate, InputError> {
        let date = self.0;
        NaiveDate::from_ymd_opt(
            date.year(),
            u32::from(u8::from(date.month())),
            u32::from(date.day()),
        )
        .ok_or(InputError::ChronoOutOfRange { kind: "date" })
    }
}

impl Serialize for DmyDate {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let value = self
            .0
            .format(DMY_DATE_FORMAT)
            .map_err(serde::ser::Error::custom)?;
        serializer.serialize_str(&value)
    }
}

impl<'de> Deserialize<'de> for DmyDate {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = <&str>::deserialize(deserializer)?;
        Self::parse(value).map_err(serde::de::Error::custom)
    }
}

impl std::fmt::Display for DmyDate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let value = self
            .0
            .format(DMY_DATE_FORMAT)
            .map_err(|_| std::fmt::Error)?;
        f.write_str(&value)
    }
}

#[cfg(feature = "chrono")]
impl TryFrom<NaiveDateTime> for IsoDateTime {
    type Error = InputError;

    fn try_from(value: NaiveDateTime) -> Result<Self, Self::Error> {
        if value.nanosecond() != 0 {
            return Err(InputError::ChronoSubsecondPrecision);
        }

        let month = chrono_month_to_time(value.month())?;
        let day =
            u8::try_from(value.day()).map_err(|_| InputError::ChronoOutOfRange { kind: "day" })?;
        let date = Date::from_calendar_date(value.year(), month, day)
            .map_err(|_| InputError::ChronoOutOfRange { kind: "date" })?;
        let hour = u8::try_from(value.hour())
            .map_err(|_| InputError::ChronoOutOfRange { kind: "hour" })?;
        let minute = u8::try_from(value.minute())
            .map_err(|_| InputError::ChronoOutOfRange { kind: "minute" })?;
        let second = u8::try_from(value.second())
            .map_err(|_| InputError::ChronoOutOfRange { kind: "second" })?;
        let time = time::Time::from_hms(hour, minute, second)
            .map_err(|_| InputError::ChronoOutOfRange { kind: "time" })?;

        Ok(Self::new(PrimitiveDateTime::new(date, time)))
    }
}

#[cfg(feature = "chrono")]
impl TryFrom<NaiveDate> for DmyDate {
    type Error = InputError;

    fn try_from(value: NaiveDate) -> Result<Self, Self::Error> {
        let month = chrono_month_to_time(value.month())?;
        let day =
            u8::try_from(value.day()).map_err(|_| InputError::ChronoOutOfRange { kind: "day" })?;
        Date::from_calendar_date(value.year(), month, day)
            .map(Self::new)
            .map_err(|_| InputError::ChronoOutOfRange { kind: "date" })
    }
}

#[cfg(feature = "chrono")]
fn chrono_month_to_time(value: u32) -> Result<time::Month, InputError> {
    let month = u8::try_from(value).map_err(|_| InputError::ChronoOutOfRange { kind: "month" })?;
    time::Month::try_from(month).map_err(|_| InputError::ChronoOutOfRange { kind: "month" })
}

/// Периодичность ряда.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Periodicity {
    /// Месячная периодичность.
    Month,
    /// Квартальная периодичность.
    Quarter,
    /// Годовая периодичность.
    Year,
}

/// Диапазон годов включительно.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct YearSpan {
    start: Year,
    end: Year,
}

impl YearSpan {
    /// Создаёт диапазон годов с проверкой `start <= end`.
    pub fn new(start: Year, end: Year) -> Result<Self, InputError> {
        if start > end {
            return Err(InputError::InvalidYearSpan {
                start: start.get(),
                end: end.get(),
            });
        }

        Ok(Self { start, end })
    }

    /// Левая граница диапазона.
    #[must_use]
    #[inline]
    pub fn start(self) -> Year {
        self.start
    }

    /// Правая граница диапазона.
    #[must_use]
    #[inline]
    pub fn end(self) -> Year {
        self.end
    }
}