tosho_mplus/
helper.rs

1//! Provides a collection of helper Structs that can be used.
2//!
3//! ```rust
4//! use tosho_mplus::ImageQuality;
5//!
6//! let hq_img = ImageQuality::High;
7//! ```
8
9use std::str::FromStr;
10
11/// The image quality to be downloaded.
12#[derive(
13    Debug, Clone, Copy, PartialEq, tosho_macros::SerializeEnum, tosho_macros::DeserializeEnum,
14)]
15pub enum ImageQuality {
16    /// Low quality images
17    Low,
18    /// Normal quality images
19    Normal,
20    /// High quality images
21    High,
22}
23
24tosho_macros::enum_error!(ImageQualityFromStrError);
25
26impl FromStr for ImageQuality {
27    type Err = ImageQualityFromStrError;
28
29    fn from_str(s: &str) -> Result<Self, Self::Err> {
30        match s.to_ascii_lowercase().as_str() {
31            "low" => Ok(ImageQuality::Low),
32            "high" | "normal" | "middle" | "standard" => Ok(ImageQuality::Normal),
33            "super_high" | "high_quality" => Ok(ImageQuality::High),
34            _ => Err(ImageQualityFromStrError {
35                original: s.to_string(),
36            }),
37        }
38    }
39}
40
41impl std::fmt::Display for ImageQuality {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match self {
44            ImageQuality::Low => write!(f, "low"),
45            ImageQuality::Normal => write!(f, "high"),
46            ImageQuality::High => write!(f, "super_high"),
47        }
48    }
49}
50
51/// The subscriptions plan tier.
52#[derive(
53    Debug,
54    Clone,
55    Copy,
56    PartialEq,
57    PartialOrd,
58    tosho_macros::SerializeEnum,
59    tosho_macros::DeserializeEnum,
60    tosho_macros::EnumName,
61)]
62pub enum SubscriptionPlan {
63    /// Basic or user has no subscription
64    Basic,
65    /// The standard tier, which includes all the currently releasing chapters
66    Standard,
67    /// Deluxe tier, which is standard tier with extra perks that allows reading finished series
68    Deluxe,
69}
70
71tosho_macros::enum_error!(SubscriptionPlanFromStrError);
72
73impl FromStr for SubscriptionPlan {
74    type Err = SubscriptionPlanFromStrError;
75
76    fn from_str(s: &str) -> Result<Self, Self::Err> {
77        match s.to_ascii_lowercase().as_str() {
78            "basic" => Ok(SubscriptionPlan::Basic),
79            "standard" => Ok(SubscriptionPlan::Standard),
80            "deluxe" => Ok(SubscriptionPlan::Deluxe),
81            _ => Err(SubscriptionPlanFromStrError {
82                original: s.to_string(),
83            }),
84        }
85    }
86}
87
88impl std::fmt::Display for SubscriptionPlan {
89    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90        match self {
91            SubscriptionPlan::Basic => write!(f, "basic"),
92            SubscriptionPlan::Standard => write!(f, "standard"),
93            SubscriptionPlan::Deluxe => write!(f, "deluxe"),
94        }
95    }
96}
97
98/// The title ranking type.
99#[derive(
100    Debug, Clone, Copy, PartialEq, tosho_macros::SerializeEnum, tosho_macros::DeserializeEnum,
101)]
102pub enum RankingType {
103    /// The current hottest title ranking
104    Hottest,
105    /// The currently trending title ranking
106    Trending,
107    /// Completed title ranking
108    Completed,
109}
110
111tosho_macros::enum_error!(RankingTypeFromStrError);
112
113impl FromStr for RankingType {
114    type Err = RankingTypeFromStrError;
115
116    fn from_str(s: &str) -> Result<Self, Self::Err> {
117        match s.to_ascii_lowercase().as_str() {
118            "hottest" | "hot" => Ok(RankingType::Hottest),
119            "trending" => Ok(RankingType::Trending),
120            "completed" | "complete" => Ok(RankingType::Completed),
121            _ => Err(RankingTypeFromStrError {
122                original: s.to_string(),
123            }),
124        }
125    }
126}
127
128impl std::fmt::Display for RankingType {
129    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130        match self {
131            RankingType::Hottest => write!(f, "hottest"),
132            RankingType::Trending => write!(f, "trending"),
133            RankingType::Completed => write!(f, "completed"),
134        }
135    }
136}
137
138#[cfg(test)]
139mod tests {
140    #[test]
141    fn test_plan_type_ord() {
142        use super::SubscriptionPlan;
143
144        assert!(SubscriptionPlan::Basic < SubscriptionPlan::Standard);
145        assert!(SubscriptionPlan::Standard < SubscriptionPlan::Deluxe);
146        assert!(SubscriptionPlan::Basic < SubscriptionPlan::Deluxe);
147        assert!(SubscriptionPlan::Deluxe >= SubscriptionPlan::Standard);
148        assert!(SubscriptionPlan::Standard >= SubscriptionPlan::Standard);
149    }
150}