mangadex_api_schema_rust/v5/
manga_aggregate.rs

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
#[cfg(feature = "serialize")]
use std::collections::HashMap;

use mangadex_api_types::ResultType;
#[cfg(feature = "serialize")]
use serde::Serialize;

use serde::Deserialize;
use uuid::Uuid;

use crate::v5::{chapter_aggregate_array_or_map, volume_aggregate_array_or_map};

/// Response struct for the manga aggregate endpoint (GET `/manga/:id/aggregate`).
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(feature = "non_exhaustive", non_exhaustive)]
pub struct MangaAggregate {
    #[serde(default)]
    pub result: ResultType,
    /// Object with (volume_number, volume) key-value pairs.
    #[serde(with = "volume_aggregate_array_or_map")]
    pub volumes: Vec<VolumeAggregate>,
}

#[cfg(feature = "specta")]
impl specta::Type for MangaAggregate {
    fn inline(
        opts: specta::DefOpts,
        generics: &[specta::DataType],
    ) -> Result<specta::DataType, specta::ExportError> {
        MangaAggregatSer::inline(opts, generics)
    }
}

#[cfg(feature = "serialize")]
impl From<MangaAggregate> for MangaAggregatSer {
    fn from(value: MangaAggregate) -> Self {
        let mut volumes: HashMap<String, VolumeAggregateSer> = HashMap::new();
        for volume in value.volumes {
            volumes.insert(volume.volume.clone(), Into::into(volume.clone()));
        }
        MangaAggregatSer {
            result: value.result,
            volumes,
        }
    }
}

#[cfg(feature = "serialize")]
#[derive(serde::Serialize, Clone)]
#[cfg_attr(feature = "specta", derive(specta::Type))]
pub struct MangaAggregatSer {
    result: ResultType,
    volumes: HashMap<String, VolumeAggregateSer>,
}

#[cfg(feature = "serialize")]
#[derive(serde::Serialize, Clone)]
#[cfg_attr(feature = "specta", derive(specta::Type))]
pub struct VolumeAggregateSer {
    /// Volume number.
    pub volume: String,
    /// Number of chapter translations for the volume.
    pub count: u32,
    /// Object with (chapter_number, chapter) key-value pairs.
    pub chapters: HashMap<String, ChapterAggregate>,
}

#[cfg(feature = "serialize")]
impl Serialize for MangaAggregate {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let ser: MangaAggregatSer = Into::into(self.clone());
        ser.serialize(serializer)
    }
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "non_exhaustive", non_exhaustive)]
#[cfg_attr(feature = "specta", derive(specta::Type))]
pub struct VolumeAggregate {
    /// Volume number.
    pub volume: String,
    /// Number of chapter translations for the volume.
    pub count: u32,
    /// Object with (chapter_number, chapter) key-value pairs.
    #[serde(with = "chapter_aggregate_array_or_map")]
    pub chapters: Vec<ChapterAggregate>,
}

#[cfg(feature = "serialize")]
#[allow(clippy::from_over_into)]
impl From<VolumeAggregate> for VolumeAggregateSer {
    fn from(value: VolumeAggregate) -> Self {
        let mut chapters: HashMap<String, ChapterAggregate> = HashMap::new();
        for chapter in value.chapters {
            chapters.insert(chapter.chapter.clone(), chapter);
        }
        VolumeAggregateSer {
            volume: value.volume,
            count: value.count,
            chapters,
        }
    }
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "non_exhaustive", non_exhaustive)]
#[cfg_attr(feature = "specta", derive(specta::Type))]
pub struct ChapterAggregate {
    /// Chapter number.
    pub chapter: String,
    pub id: Uuid,
    // TODO: Add docblock explaining what this field represents.
    pub others: Vec<Uuid>,
    /// Number of translations for the chapter.
    pub count: u32,
}