use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Clone, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[serde(rename_all(deserialize = "UPPERCASE"))]
pub enum Season {
#[default]
Winter,
Spring,
Summer,
Fall,
}
impl Season {
pub fn name(&self) -> &str {
match self {
Season::Winter => "Winter",
Season::Spring => "Spring",
Season::Summer => "Summer",
Season::Fall => "Fall",
}
}
pub fn summary(&self) -> &str {
match self {
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.",
Season::Spring => "Spring is one of the four temperate seasons, following winter and preceding summer.",
Season::Summer => "Summer is the hottest of the four temperate seasons, falling after spring and before autumn.",
Season::Fall => "Autumn, also known as fall in North American English, is one of the four temperate seasons."
}
}
}
impl From<&str> for Season {
fn from(value: &str) -> Self {
match value.trim().to_uppercase().as_str() {
"WINTER" => Season::Winter,
"SPRING" => Season::Spring,
"SUMMER" => Season::Summer,
"FALL" => Season::Fall,
_ => Season::default(),
}
}
}
impl From<String> for Season {
fn from(value: String) -> Self {
Season::from(value.as_str())
}
}
impl std::fmt::Display for Season {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_season_name() {
assert_eq!(Season::Winter.name(), "Winter");
assert_eq!(Season::Spring.name(), "Spring");
assert_eq!(Season::Summer.name(), "Summer");
assert_eq!(Season::Fall.name(), "Fall");
}
#[test]
fn test_from_str() {
assert_eq!(Season::from("winter"), Season::Winter);
assert_eq!(Season::from("SPRING"), Season::Spring);
assert_eq!(Season::from("Summer"), Season::Summer);
assert_eq!(Season::from("fall"), Season::Fall);
assert_eq!(Season::from("unknown"), Season::Winter); }
#[test]
fn test_from_string() {
assert_eq!(Season::from("winter".to_string()), Season::Winter);
assert_eq!(Season::from("SPRING".to_string()), Season::Spring);
assert_eq!(Season::from("Summer".to_string()), Season::Summer);
assert_eq!(Season::from("fall".to_string()), Season::Fall);
assert_eq!(Season::from("unknown".to_string()), Season::Winter); }
}