kodik-api-client 0.1.0

Typed asynchronous Rust client for the public Kodik 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
//! Типизированные параметры всех ресурсов Kodik API.
//!
//! Все строки с несколькими значениями сериализуются через запятую, как требует API.

use std::fmt;

use crate::error::{Error, Result};

/// HTTP-способ передачи параметров к Kodik API.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum RequestMethod {
    /// Параметры передаются в URL как query string. Это значение по умолчанию.
    #[default]
    Get,
    /// Параметры передаются в теле `application/x-www-form-urlencoded`.
    Post,
}

/// Тип материала, который Kodik принимает в параметре `types`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ContentType {
    ForeignMovie,
    SovietCartoon,
    ForeignCartoon,
    RussianCartoon,
    Anime,
    RussianMovie,
    CartoonSerial,
    DocumentarySerial,
    RussianSerial,
    ForeignSerial,
    AnimeSerial,
    MultiPartFilm,
}

impl ContentType {
    /// Строковое значение Kodik API.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::ForeignMovie => "foreign-movie",
            Self::SovietCartoon => "soviet-cartoon",
            Self::ForeignCartoon => "foreign-cartoon",
            Self::RussianCartoon => "russian-cartoon",
            Self::Anime => "anime",
            Self::RussianMovie => "russian-movie",
            Self::CartoonSerial => "cartoon-serial",
            Self::DocumentarySerial => "documentary-serial",
            Self::RussianSerial => "russian-serial",
            Self::ForeignSerial => "foreign-serial",
            Self::AnimeSerial => "anime-serial",
            Self::MultiPartFilm => "multi-part-film",
        }
    }
}

impl fmt::Display for ContentType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Тип перевода в Kodik.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TranslationType {
    Voice,
    Subtitles,
}

impl TranslationType {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Voice => "voice",
            Self::Subtitles => "subtitles",
        }
    }
}

impl fmt::Display for TranslationType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Поле внешнего идентификатора, требуемое параметрами `has_field` и `has_field_and`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RequiredField {
    KinopoiskId,
    ImdbId,
    MdlId,
    WorldartLink,
    ShikimoriId,
}

impl RequiredField {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::KinopoiskId => "kinopoisk_id",
            Self::ImdbId => "imdb_id",
            Self::MdlId => "mdl_id",
            Self::WorldartLink => "worldart_link",
            Self::ShikimoriId => "shikimori_id",
        }
    }
}

impl fmt::Display for RequiredField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Типы списка жанров, который должен вернуть `/genres`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum GenreSource {
    /// Жанры КиноПоиска — значение API по умолчанию.
    #[default]
    Kinopoisk,
    Shikimori,
    MyDramaList,
    All,
}

impl GenreSource {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Kinopoisk => "kinopoisk",
            Self::Shikimori => "shikimori",
            Self::MyDramaList => "mydramalist",
            Self::All => "all",
        }
    }
}

impl fmt::Display for GenreSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Направление сортировки `/list`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SortOrder {
    Asc,
    /// Убывание — значение API по умолчанию.
    #[default]
    Desc,
}

impl SortOrder {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Asc => "asc",
            Self::Desc => "desc",
        }
    }
}

impl fmt::Display for SortOrder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Поле сортировки `/list`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ListSort {
    Year,
    CreatedAt,
    /// Время обновления — значение API по умолчанию.
    #[default]
    UpdatedAt,
    KinopoiskRating,
    ImdbRating,
    ShikimoriRating,
}

impl ListSort {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Year => "year",
            Self::CreatedAt => "created_at",
            Self::UpdatedAt => "updated_at",
            Self::KinopoiskRating => "kinopoisk_rating",
            Self::ImdbRating => "imdb_rating",
            Self::ShikimoriRating => "shikimori_rating",
        }
    }
}

impl fmt::Display for ListSort {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Сортировка строкового справочника: стран, жанров, качеств и озвучек.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LabelSort {
    /// По названию — значение API по умолчанию.
    #[default]
    Title,
    Count,
}

impl LabelSort {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Title => "title",
            Self::Count => "count",
        }
    }
}

impl fmt::Display for LabelSort {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Сортировка списка лет.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum YearSort {
    /// По году — значение API по умолчанию.
    #[default]
    Year,
    Count,
}

impl YearSort {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Year => "year",
            Self::Count => "count",
        }
    }
}

impl fmt::Display for YearSort {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Общие фильтры материалов, доступные в `/years`, `/genres`, `/countries`,
/// `/qualities/v2`, `/translations/v2`, `/list` и `/search`.
///
/// Поля с суффиксом `_and` требуют наличия **всех** перечисленных значений.
/// Их аналоги без суффикса используют семантику «хотя бы одно». Диапазоны
/// (`duration`, рейтинги и `minimal_age`) нужно задавать строкой: `"40-80"`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Filters {
    pub types: Vec<ContentType>,
    pub year: Vec<i32>,
    pub translation_ids: Vec<u64>,
    pub block_translations: Vec<u64>,
    pub translation_type: Option<TranslationType>,
    pub has_field: Vec<RequiredField>,
    pub has_field_and: Vec<RequiredField>,
    pub camrip: Option<bool>,
    pub lgbt: Option<bool>,
    pub countries: Vec<String>,
    pub countries_and: Vec<String>,
    pub genres: Vec<String>,
    pub genres_and: Vec<String>,
    pub anime_genres: Vec<String>,
    pub anime_genres_and: Vec<String>,
    pub drama_genres: Vec<String>,
    pub drama_genres_and: Vec<String>,
    pub all_genres: Vec<String>,
    pub all_genres_and: Vec<String>,
    pub duration: Option<String>,
    pub kinopoisk_rating: Option<String>,
    pub imdb_rating: Option<String>,
    pub shikimori_rating: Option<String>,
    pub mydramalist_rating: Option<String>,
    pub actors: Vec<String>,
    pub actors_and: Vec<String>,
    pub directors: Vec<String>,
    pub directors_and: Vec<String>,
    pub producers: Vec<String>,
    pub producers_and: Vec<String>,
    pub writers: Vec<String>,
    pub writers_and: Vec<String>,
    pub composers: Vec<String>,
    pub composers_and: Vec<String>,
    pub editors: Vec<String>,
    pub editors_and: Vec<String>,
    pub designers: Vec<String>,
    pub designers_and: Vec<String>,
    pub operators: Vec<String>,
    pub operators_and: Vec<String>,
    pub rating_mpaa: Vec<String>,
    pub minimal_age: Option<String>,
    pub anime_kind: Vec<String>,
    pub mydramalist_tags: Vec<String>,
    pub mydramalist_tags_and: Vec<String>,
    pub anime_status: Vec<String>,
    pub drama_status: Vec<String>,
    pub all_status: Vec<String>,
    pub anime_studios: Vec<String>,
    pub anime_studios_and: Vec<String>,
    pub anime_licensed_by: Vec<String>,
    pub anime_licensed_by_and: Vec<String>,
}

impl Filters {
    /// Добавляет фильтр по типам материалов.
    pub fn with_types(mut self, values: impl IntoIterator<Item = ContentType>) -> Self {
        self.types.extend(values);
        self
    }

    /// Добавляет фильтр по странам с семантикой «хотя бы одна».
    pub fn with_countries(mut self, values: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.countries.extend(values.into_iter().map(Into::into));
        self
    }

    /// Добавляет фильтр по жанрам КиноПоиска с семантикой «хотя бы один».
    pub fn with_genres(mut self, values: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.genres.extend(values.into_iter().map(Into::into));
        self
    }

    fn encode_into(&self, output: &mut Parameters) {
        output.push_joined("types", &self.types);
        output.push_joined("year", &self.year);
        output.push_joined("translation_id", &self.translation_ids);
        output.push_joined("block_translations", &self.block_translations);
        output.push_option("translation_type", self.translation_type);
        output.push_joined("has_field", &self.has_field);
        output.push_joined("has_field_and", &self.has_field_and);
        output.push_bool("camrip", self.camrip);
        output.push_bool("lgbt", self.lgbt);
        output.push_joined("countries", &self.countries);
        output.push_joined("countries_and", &self.countries_and);
        output.push_joined("genres", &self.genres);
        output.push_joined("genres_and", &self.genres_and);
        output.push_joined("anime_genres", &self.anime_genres);
        output.push_joined("anime_genres_and", &self.anime_genres_and);
        output.push_joined("drama_genres", &self.drama_genres);
        output.push_joined("drama_genres_and", &self.drama_genres_and);
        output.push_joined("all_genres", &self.all_genres);
        output.push_joined("all_genres_and", &self.all_genres_and);
        output.push_string("duration", self.duration.as_deref());
        output.push_string("kinopoisk_rating", self.kinopoisk_rating.as_deref());
        output.push_string("imdb_rating", self.imdb_rating.as_deref());
        output.push_string("shikimori_rating", self.shikimori_rating.as_deref());
        output.push_string("mydramalist_rating", self.mydramalist_rating.as_deref());
        output.push_joined("actors", &self.actors);
        output.push_joined("actors_and", &self.actors_and);
        output.push_joined("directors", &self.directors);
        output.push_joined("directors_and", &self.directors_and);
        output.push_joined("producers", &self.producers);
        output.push_joined("producers_and", &self.producers_and);
        output.push_joined("writers", &self.writers);
        output.push_joined("writers_and", &self.writers_and);
        output.push_joined("composers", &self.composers);
        output.push_joined("composers_and", &self.composers_and);
        output.push_joined("editors", &self.editors);
        output.push_joined("editors_and", &self.editors_and);
        output.push_joined("designers", &self.designers);
        output.push_joined("designers_and", &self.designers_and);
        output.push_joined("operators", &self.operators);
        output.push_joined("operators_and", &self.operators_and);
        output.push_joined("rating_mpaa", &self.rating_mpaa);
        output.push_string("minimal_age", self.minimal_age.as_deref());
        output.push_joined("anime_kind", &self.anime_kind);
        output.push_joined("mydramalist_tags", &self.mydramalist_tags);
        output.push_joined("mydramalist_tags_and", &self.mydramalist_tags_and);
        output.push_joined("anime_status", &self.anime_status);
        output.push_joined("drama_status", &self.drama_status);
        output.push_joined("all_status", &self.all_status);
        output.push_joined("anime_studios", &self.anime_studios);
        output.push_joined("anime_studios_and", &self.anime_studios_and);
        output.push_joined("anime_licensed_by", &self.anime_licensed_by);
        output.push_joined("anime_licensed_by_and", &self.anime_licensed_by_and);
    }
}

/// Запрос к `/years`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct YearsQuery {
    pub filters: Filters,
    pub sort: YearSort,
}

impl YearsQuery {
    pub fn query_string(&self) -> String {
        self.parameters().encoded()
    }

    fn parameters(&self) -> Parameters {
        let mut parameters = Parameters::default();
        self.filters.encode_into(&mut parameters);
        parameters.push("sort", self.sort);
        parameters
    }
}

/// Запрос к `/genres`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct GenresQuery {
    pub filters: Filters,
    pub genres_type: GenreSource,
    pub sort: LabelSort,
}

impl GenresQuery {
    pub fn query_string(&self) -> String {
        self.parameters().encoded()
    }

    fn parameters(&self) -> Parameters {
        let mut parameters = Parameters::default();
        self.filters.encode_into(&mut parameters);
        parameters.push("genres_type", self.genres_type);
        parameters.push("sort", self.sort);
        parameters
    }
}

/// Запрос к `/countries`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CountriesQuery {
    pub filters: Filters,
    pub sort: LabelSort,
}

impl CountriesQuery {
    pub fn query_string(&self) -> String {
        self.parameters().encoded()
    }

    fn parameters(&self) -> Parameters {
        let mut parameters = Parameters::default();
        self.filters.encode_into(&mut parameters);
        parameters.push("sort", self.sort);
        parameters
    }
}

/// Запрос к `/qualities/v2`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct QualitiesQuery {
    pub filters: Filters,
    pub sort: LabelSort,
}

impl QualitiesQuery {
    pub fn query_string(&self) -> String {
        self.parameters().encoded()
    }

    fn parameters(&self) -> Parameters {
        let mut parameters = Parameters::default();
        self.filters.encode_into(&mut parameters);
        parameters.push("sort", self.sort);
        parameters
    }
}

/// Запрос к `/translations/v2`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TranslationsQuery {
    pub filters: Filters,
    pub sort: LabelSort,
}

impl TranslationsQuery {
    pub fn query_string(&self) -> String {
        self.parameters().encoded()
    }

    fn parameters(&self) -> Parameters {
        let mut parameters = Parameters::default();
        self.filters.encode_into(&mut parameters);
        parameters.push("sort", self.sort);
        parameters
    }
}

/// Запрос к `/list`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ListQuery {
    pub filters: Filters,
    /// Количество результатов на странице, от 1 до 100. `None` передаёт выбор серверу.
    pub limit: Option<u8>,
    pub sort: ListSort,
    pub order: SortOrder,
    pub with_seasons: bool,
    pub with_episodes: bool,
    pub with_episodes_data: bool,
    pub with_page_links: bool,
    pub not_blocked_in: Vec<String>,
    pub not_blocked_for_me: bool,
    pub with_material_data: bool,
    /// Курсор API из поля `next_page`; передавайте уже percent-decoded значение параметра `next`.
    pub next: Option<String>,
}

impl ListQuery {
    /// Проверяет границы `limit` до сетевого вызова.
    pub fn validate(&self) -> Result<()> {
        if self.limit.is_some_and(|limit| !(1..=100).contains(&limit)) {
            return Err(Error::Validation("list limit must be in 1..=100".into()));
        }
        Ok(())
    }

    pub fn query_string(&self) -> Result<String> {
        Ok(self.parameters()?.encoded())
    }

    fn parameters(&self) -> Result<Parameters> {
        self.validate()?;
        let mut parameters = Parameters::default();
        self.filters.encode_into(&mut parameters);
        if let Some(limit) = self.limit {
            parameters.push("limit", limit);
        }
        parameters.push("sort", self.sort);
        parameters.push("order", self.order);
        parameters.push_bool_if_true("with_seasons", self.with_seasons);
        parameters.push_bool_if_true("with_episodes", self.with_episodes);
        parameters.push_bool_if_true("with_episodes_data", self.with_episodes_data);
        parameters.push_bool_if_true("with_page_links", self.with_page_links);
        parameters.push_joined("not_blocked_in", &self.not_blocked_in);
        parameters.push_bool_if_true("not_blocked_for_me", self.not_blocked_for_me);
        parameters.push_bool_if_true("with_material_data", self.with_material_data);
        parameters.push_string("next", self.next.as_deref());
        Ok(parameters)
    }
}

/// Параметры точного и нечёткого поиска `/search`.
///
/// Хотя бы одно поле-критерий должно быть заполнено: название, Kodik ID, ссылка
/// плеера или один из внешних идентификаторов.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SearchQuery {
    pub title: Option<String>,
    pub title_orig: Option<String>,
    pub strict: bool,
    pub full_match: bool,
    pub id: Option<String>,
    pub player_link: Option<String>,
    pub kinopoisk_ids: Vec<String>,
    pub imdb_ids: Vec<String>,
    pub mdl_ids: Vec<String>,
    pub worldart_animation_ids: Vec<String>,
    pub worldart_cinema_ids: Vec<String>,
    pub worldart_link: Option<String>,
    pub shikimori_ids: Vec<String>,
    /// Максимум результатов, от 1 до 100. `None` оставляет ограничение API.
    pub limit: Option<u8>,
    pub filters: Filters,
    /// Последовательность ID/типов перевода с более высоким приоритетом. Значение `"0"`
    /// в списке отключает серверную приоритизацию по умолчанию.
    pub prioritize_translations: Vec<String>,
    /// Последовательность ID/типов перевода с более низким приоритетом.
    pub unprioritize_translations: Vec<String>,
    pub prioritize_translation_type: Option<TranslationType>,
    pub with_seasons: bool,
    pub season: Option<u32>,
    pub with_episodes: bool,
    pub with_episodes_data: bool,
    pub episode: Option<u32>,
    pub with_page_links: bool,
    pub not_blocked_in: Vec<String>,
    pub not_blocked_for_me: bool,
    pub with_material_data: bool,
}

impl SearchQuery {
    /// Валидирует обязательный критерий поиска и зависимость `episode -> season`.
    pub fn validate(&self) -> Result<()> {
        if self.limit.is_some_and(|limit| !(1..=100).contains(&limit)) {
            return Err(Error::Validation("search limit must be in 1..=100".into()));
        }
        if self.episode.is_some() && self.season.is_none() {
            return Err(Error::Validation(
                "search episode requires a corresponding season".into(),
            ));
        }
        if !has_text(self.title.as_deref())
            && !has_text(self.title_orig.as_deref())
            && !has_text(self.id.as_deref())
            && !has_text(self.player_link.as_deref())
            && !has_non_blank_item(&self.kinopoisk_ids)
            && !has_non_blank_item(&self.imdb_ids)
            && !has_non_blank_item(&self.mdl_ids)
            && !has_non_blank_item(&self.worldart_animation_ids)
            && !has_non_blank_item(&self.worldart_cinema_ids)
            && !has_text(self.worldart_link.as_deref())
            && !has_non_blank_item(&self.shikimori_ids)
        {
            return Err(Error::Validation(
                "search requires title, title_orig, Kodik ID, player link, or an external ID"
                    .into(),
            ));
        }
        Ok(())
    }

    pub fn query_string(&self) -> Result<String> {
        Ok(self.parameters()?.encoded())
    }

    fn parameters(&self) -> Result<Parameters> {
        self.validate()?;
        let mut parameters = Parameters::default();
        parameters.push_string("title", self.title.as_deref());
        parameters.push_string("title_orig", self.title_orig.as_deref());
        parameters.push_bool_if_true("strict", self.strict);
        parameters.push_bool_if_true("full_match", self.full_match);
        parameters.push_string("id", self.id.as_deref());
        parameters.push_string("player_link", self.player_link.as_deref());
        parameters.push_joined("kinopoisk_id", &self.kinopoisk_ids);
        parameters.push_joined("imdb_id", &self.imdb_ids);
        parameters.push_joined("mdl_id", &self.mdl_ids);
        parameters.push_joined("worldart_animation_id", &self.worldart_animation_ids);
        parameters.push_joined("worldart_cinema_id", &self.worldart_cinema_ids);
        parameters.push_string("worldart_link", self.worldart_link.as_deref());
        parameters.push_joined("shikimori_id", &self.shikimori_ids);
        if let Some(limit) = self.limit {
            parameters.push("limit", limit);
        }
        self.filters.encode_into(&mut parameters);
        parameters.push_joined("prioritize_translations", &self.prioritize_translations);
        parameters.push_joined("unprioritize_translations", &self.unprioritize_translations);
        parameters.push_option(
            "prioritize_translation_type",
            self.prioritize_translation_type,
        );
        parameters.push_bool_if_true("with_seasons", self.with_seasons);
        if let Some(season) = self.season {
            parameters.push("season", season);
        }
        parameters.push_bool_if_true("with_episodes", self.with_episodes);
        parameters.push_bool_if_true("with_episodes_data", self.with_episodes_data);
        if let Some(episode) = self.episode {
            parameters.push("episode", episode);
        }
        parameters.push_bool_if_true("with_page_links", self.with_page_links);
        parameters.push_joined("not_blocked_in", &self.not_blocked_in);
        parameters.push_bool_if_true("not_blocked_for_me", self.not_blocked_for_me);
        parameters.push_bool_if_true("with_material_data", self.with_material_data);
        Ok(parameters)
    }
}

#[derive(Clone, Debug, Default)]
pub(crate) struct Parameters(Vec<(String, String)>);

impl Parameters {
    pub(crate) fn push(&mut self, key: impl Into<String>, value: impl fmt::Display) {
        self.0.push((key.into(), value.to_string()));
    }

    fn push_string(&mut self, key: impl Into<String>, value: Option<&str>) {
        if let Some(value) = value.filter(|value| !value.trim().is_empty()) {
            self.0.push((key.into(), value.to_owned()));
        }
    }

    fn push_option<T: fmt::Display>(&mut self, key: impl Into<String>, value: Option<T>) {
        if let Some(value) = value {
            self.push(key, value);
        }
    }

    fn push_bool(&mut self, key: impl Into<String>, value: Option<bool>) {
        if let Some(value) = value {
            self.push(key, value);
        }
    }

    fn push_bool_if_true(&mut self, key: impl Into<String>, value: bool) {
        if value {
            self.push(key, "true");
        }
    }

    fn push_joined<T: fmt::Display>(&mut self, key: impl Into<String>, values: &[T]) {
        if !values.is_empty() {
            let value = values
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join(",");
            self.0.push((key.into(), value));
        }
    }

    pub(crate) fn extend(&mut self, other: Parameters) {
        self.0.extend(other.0);
    }

    pub(crate) fn encoded(&self) -> String {
        self.0
            .iter()
            .map(|(key, value)| format!("{}={}", percent_encode(key), percent_encode(value)))
            .collect::<Vec<_>>()
            .join("&")
    }
}

pub(crate) trait IntoParameters {
    fn into_parameters(self) -> Result<Parameters>;
}

impl IntoParameters for YearsQuery {
    fn into_parameters(self) -> Result<Parameters> {
        Ok(self.parameters())
    }
}

impl IntoParameters for GenresQuery {
    fn into_parameters(self) -> Result<Parameters> {
        Ok(self.parameters())
    }
}

impl IntoParameters for CountriesQuery {
    fn into_parameters(self) -> Result<Parameters> {
        Ok(self.parameters())
    }
}

impl IntoParameters for QualitiesQuery {
    fn into_parameters(self) -> Result<Parameters> {
        Ok(self.parameters())
    }
}

impl IntoParameters for TranslationsQuery {
    fn into_parameters(self) -> Result<Parameters> {
        Ok(self.parameters())
    }
}

impl IntoParameters for ListQuery {
    fn into_parameters(self) -> Result<Parameters> {
        self.parameters()
    }
}

impl IntoParameters for SearchQuery {
    fn into_parameters(self) -> Result<Parameters> {
        self.parameters()
    }
}

fn has_text(value: Option<&str>) -> bool {
    value.is_some_and(|value| !value.trim().is_empty())
}

fn has_non_blank_item(values: &[String]) -> bool {
    values.iter().any(|value| !value.trim().is_empty())
}

fn percent_encode(value: &str) -> String {
    let mut encoded = String::with_capacity(value.len());
    for byte in value.bytes() {
        if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'~') {
            encoded.push(byte as char);
        } else {
            encoded.push('%');
            encoded.push(hex(byte >> 4));
            encoded.push(hex(byte & 0x0f));
        }
    }
    encoded
}

const fn hex(nibble: u8) -> char {
    match nibble {
        0..=9 => (b'0' + nibble) as char,
        _ => (b'A' + nibble - 10) as char,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn encodes_unicode_and_comma_separated_values() {
        let query = YearsQuery {
            filters: Filters::default()
                .with_types([ContentType::AnimeSerial])
                .with_countries(["США", "Россия"]),
            sort: YearSort::Count,
        };
        assert_eq!(
            query.query_string(),
            "types=anime-serial&countries=%D0%A1%D0%A8%D0%90%2C%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D1%8F&sort=count"
        );
    }

    #[test]
    fn search_requires_a_criterion() {
        let error = SearchQuery::default().validate().unwrap_err();
        assert!(error.to_string().contains("search requires"));
    }

    #[test]
    fn episode_requires_season() {
        let query = SearchQuery {
            title: Some("Test".into()),
            episode: Some(4),
            ..Default::default()
        };
        assert!(query.validate().is_err());
    }

    #[test]
    fn search_rejects_empty_or_whitespace_only_criteria() {
        for value in ["", "   ", "\n\t"] {
            let query = SearchQuery {
                title: Some(value.into()),
                ..Default::default()
            };
            assert!(
                query.validate().is_err(),
                "criterion {value:?} must be rejected"
            );
        }

        let query = SearchQuery {
            kinopoisk_ids: vec![" ".into(), "\t".into()],
            ..Default::default()
        };
        assert!(query.validate().is_err());
    }

    #[test]
    fn list_and_search_reject_limits_outside_one_through_one_hundred() {
        for limit in [0, 101] {
            assert!(ListQuery {
                limit: Some(limit),
                ..Default::default()
            }
            .validate()
            .is_err());

            assert!(SearchQuery {
                title: Some("Avatar".into()),
                limit: Some(limit),
                ..Default::default()
            }
            .validate()
            .is_err());
        }
    }
}