rust_anilist/models/
season.rs1use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Default, Clone, Eq, Hash, PartialEq, Deserialize, Serialize)]
21#[serde(rename_all(deserialize = "UPPERCASE"))]
22pub enum Season {
23 #[default]
25 Winter,
26 Spring,
28 Summer,
30 Fall,
32}
33
34impl Season {
35 pub fn name(&self) -> &str {
45 match self {
46 Season::Winter => "Winter",
47 Season::Spring => "Spring",
48 Season::Summer => "Summer",
49 Season::Fall => "Fall",
50 }
51 }
52
53 pub fn summary(&self) -> &str {
55 match self {
56 Season::Winter => "Winter is the coldest season of the year in polar and temperate zones; it does not occur in most of the tropical zone.",
57 Season::Spring => "Spring is one of the four temperate seasons, following winter and preceding summer.",
58 Season::Summer => "Summer is the hottest of the four temperate seasons, falling after spring and before autumn.",
59 Season::Fall => "Autumn, also known as fall in North American English, is one of the four temperate seasons."
60 }
61 }
62}
63
64impl From<&str> for Season {
65 fn from(value: &str) -> Self {
66 match value.trim().to_uppercase().as_str() {
67 "WINTER" => Season::Winter,
68 "SPRING" => Season::Spring,
69 "SUMMER" => Season::Summer,
70 "FALL" => Season::Fall,
71 _ => Season::default(),
72 }
73 }
74}
75
76impl From<String> for Season {
77 fn from(value: String) -> Self {
78 Season::from(value.as_str())
79 }
80}
81
82impl std::fmt::Display for Season {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 write!(f, "{}", self.name())
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn test_season_name() {
94 assert_eq!(Season::Winter.name(), "Winter");
95 assert_eq!(Season::Spring.name(), "Spring");
96 assert_eq!(Season::Summer.name(), "Summer");
97 assert_eq!(Season::Fall.name(), "Fall");
98 }
99
100 #[test]
101 fn test_from_str() {
102 assert_eq!(Season::from("winter"), Season::Winter);
103 assert_eq!(Season::from("SPRING"), Season::Spring);
104 assert_eq!(Season::from("Summer"), Season::Summer);
105 assert_eq!(Season::from("fall"), Season::Fall);
106 assert_eq!(Season::from("unknown"), Season::Winter); }
108
109 #[test]
110 fn test_from_string() {
111 assert_eq!(Season::from("winter".to_string()), Season::Winter);
112 assert_eq!(Season::from("SPRING".to_string()), Season::Spring);
113 assert_eq!(Season::from("Summer".to_string()), Season::Summer);
114 assert_eq!(Season::from("fall".to_string()), Season::Fall);
115 assert_eq!(Season::from("unknown".to_string()), Season::Winter); }
117}