rust_anilist/models/
season.rs

1// SPDX-License-Identifier: MIT↴↴
2// Copyright (c) 2022-2025 Andriel Ferreira <https://github.com/AndrielFR>↴↴
3
4//! This module contains the `Season` enum.
5
6use serde::{Deserialize, Serialize};
7
8/// Represents the four seasons of the year.
9///
10/// The `Season` enum defines the four seasons: Winter, Spring, Summer,
11/// and Fall. This can be used to categorize or filter data based on
12/// the season.
13///
14/// # Variants
15///
16/// * `Winter` - Represents the winter season.
17/// * `Spring` - Represents the spring season.
18/// * `Summer` - Represents the summer season.
19/// * `Fall` - Represents the fall season.
20#[derive(Debug, Default, Clone, Eq, Hash, PartialEq, Deserialize, Serialize)]
21#[serde(rename_all(deserialize = "UPPERCASE"))]
22pub enum Season {
23    /// Represents the winter season.
24    #[default]
25    Winter,
26    /// Represents the spring season.
27    Spring,
28    /// Represents the summer season.
29    Summer,
30    /// Represents the fall season.
31    Fall,
32}
33
34impl Season {
35    /// Returns the name of the season.
36    ///
37    /// # Example
38    ///
39    /// ```
40    /// # use rust_anilist::models::Season;
41    /// let season = Season::Winter;
42    /// assert_eq!(season.name(), "Winter");
43    /// ```
44    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    /// Returns a summary of the season.
54    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); // Default case
107    }
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); // Default case
116    }
117}