mangadex_api_schema_rust/v5/statistics/
manga.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
//! Manga statistics from a response body.

use std::collections::HashMap;

use mangadex_api_types::ResultType;
use serde::Deserialize;
use uuid::Uuid;

use crate::FromResponse;

use super::Comments;

#[derive(Clone, Debug, Deserialize)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "non_exhaustive", non_exhaustive)]
#[cfg_attr(feature = "specta", derive(specta::Type))]
pub struct MangaStatisticsObject {
    #[serde(default)]
    pub result: ResultType,
    /// JSON object of `MangaId-StatisticsObject`.
    pub statistics: HashMap<Uuid, MangaStatistics>,
}

#[derive(Clone, Debug, Deserialize, Copy)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "specta", derive(specta::Type))]
pub struct MangaStatistics {
    pub rating: MangaRating,
    /// Number of users following the Manga.
    // The API documentation has placed this within the `rating` object as of MangaDex API 5.4.9 but
    // the actual response has this field at this level.
    pub follows: u32,
    pub comments: Option<Comments>,
}

#[derive(Clone, Debug, Deserialize, Copy)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "specta", derive(specta::Type))]
pub struct MangaRating {
    /// Average rating of distributed votes.
    ///
    /// Ratings values with no votes are not included in the calculation.
    ///
    /// Will be `None` if no ratings calculations have been done.
    #[serde(default)]
    pub average: Option<f32>,
    #[serde(default)]
    pub bayesian: Option<f32>,
    /// Ordered distribution of ratings from 1 to 10.
    ///
    /// Array indices correspond to the rating value.
    ///
    /// Each element corresponds to the number of users that have given that rating.
    #[serde(default)]
    #[cfg_attr(feature = "specta", specta(type = HashMap<String, u32>))]
    pub distribution: RatingsDistribution,
}

/// Distribution of ratings from 1 to 10.
///
/// Because Rust identifies may not begin with a number, the fields are prefixed with an arbitrary
/// "r" to denote "rating".
#[derive(Clone, Debug, Deserialize, Default, Copy)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
pub struct RatingsDistribution {
    #[serde(rename = "1")]
    pub r1: u32,
    #[serde(rename = "2")]
    pub r2: u32,
    #[serde(rename = "3")]
    pub r3: u32,
    #[serde(rename = "4")]
    pub r4: u32,
    #[serde(rename = "5")]
    pub r5: u32,
    #[serde(rename = "6")]
    pub r6: u32,
    #[serde(rename = "7")]
    pub r7: u32,
    #[serde(rename = "8")]
    pub r8: u32,
    #[serde(rename = "9")]
    pub r9: u32,
    #[serde(rename = "10")]
    pub r10: u32,
}

impl FromResponse for MangaStatisticsObject {
    type Response = Self;

    fn from_response(value: Self::Response) -> Self {
        value
    }
}