league.rs (2082B)
1 #[derive(Debug, Clone, Copy, PartialEq, Eq)] 2 #[allow(clippy::enum_variant_names)] 3 pub enum League { 4 PremierLeague, 5 Championship, 6 LaLiga, 7 ChampionsLeague, 8 } 9 10 impl League { 11 pub fn code(self) -> &'static str { 12 match self { 13 Self::PremierLeague => "PL", 14 Self::Championship => "ELC", 15 Self::LaLiga => "PD", 16 Self::ChampionsLeague => "CL", 17 } 18 } 19 20 pub fn from_arg(arg: &str) -> Option<Self> { 21 match arg.to_lowercase().as_str() { 22 "epl" => Some(Self::PremierLeague), 23 "efl" => Some(Self::Championship), 24 "laliga" => Some(Self::LaLiga), 25 "ucl" => Some(Self::ChampionsLeague), 26 _ => None, 27 } 28 } 29 } 30 31 #[cfg(test)] 32 mod tests { 33 use super::*; 34 35 #[test] 36 fn test_premier_league_aliases() { 37 assert_eq!(League::from_arg("epl"), Some(League::PremierLeague)); 38 assert_eq!(League::from_arg("EPL"), Some(League::PremierLeague)); 39 } 40 41 #[test] 42 fn test_championship_aliases() { 43 assert_eq!(League::from_arg("efl"), Some(League::Championship)); 44 assert_eq!(League::from_arg("EFL"), Some(League::Championship)); 45 } 46 47 #[test] 48 fn test_la_liga_aliases() { 49 assert_eq!(League::from_arg("laliga"), Some(League::LaLiga)); 50 assert_eq!(League::from_arg("LALIGA"), Some(League::LaLiga)); 51 } 52 53 #[test] 54 fn test_champions_league_aliases() { 55 assert_eq!(League::from_arg("ucl"), Some(League::ChampionsLeague)); 56 assert_eq!(League::from_arg("UCL"), Some(League::ChampionsLeague)); 57 } 58 59 #[test] 60 fn test_invalid_league() { 61 assert_eq!(League::from_arg("invalid"), None); 62 assert_eq!(League::from_arg(""), None); 63 assert_eq!(League::from_arg("bundesliga"), None); 64 } 65 66 #[test] 67 fn test_league_codes() { 68 assert_eq!(League::PremierLeague.code(), "PL"); 69 assert_eq!(League::Championship.code(), "ELC"); 70 assert_eq!(League::LaLiga.code(), "PD"); 71 assert_eq!(League::ChampionsLeague.code(), "CL"); 72 } 73 }