nfl_model/models/
dfs_position.rs1#[derive(
18 Debug, Clone, Copy, PartialEq, Eq, Hash, sqlx::Type, strum::IntoStaticStr, strum::EnumString,
19)]
20#[sqlx(rename_all = "UPPERCASE")]
21#[strum(serialize_all = "UPPERCASE", ascii_case_insensitive)]
22pub enum DfsPosition {
23 Qb,
25 Rb,
27 Wr,
29 Te,
31 #[strum(
33 to_string = "DST",
34 serialize = "DST",
35 serialize = "DEF",
36 serialize = "D"
37 )]
38 Dst,
39}
40
41impl DfsPosition {
42 pub fn is_defense(&self) -> bool {
44 matches!(self, DfsPosition::Dst)
45 }
46}
47
48impl From<DfsPosition> for String {
49 fn from(value: DfsPosition) -> Self {
50 let s: &str = value.into();
51 s.to_owned()
52 }
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
57pub enum DfsPositionSlotType {
58 Position(DfsPosition),
59 Flex,
60}
61
62impl DfsPositionSlotType {
63 pub fn allows(self, position: DfsPosition) -> bool {
64 match self {
65 Self::Position(slot) => slot == position,
66 Self::Flex => matches!(
67 position,
68 DfsPosition::Rb | DfsPosition::Wr | DfsPosition::Te
69 ),
70 }
71 }
72}
73
74impl From<DfsPosition> for DfsPositionSlotType {
75 fn from(position: DfsPosition) -> Self {
76 Self::Position(position)
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83 #[test]
84 fn parses_positions_case_insensitively_and_accepts_defense_aliases() {
85 assert_eq!(DfsPosition::try_from("qb"), Ok(DfsPosition::Qb));
86 assert_eq!(DfsPosition::try_from("RB"), Ok(DfsPosition::Rb));
87 assert_eq!(DfsPosition::try_from("Wr"), Ok(DfsPosition::Wr));
88 assert_eq!(DfsPosition::try_from("TE"), Ok(DfsPosition::Te));
89 for alias in ["DST", "DEF", "D"] {
90 assert_eq!(DfsPosition::try_from(alias), Ok(DfsPosition::Dst));
91 }
92 assert!(DfsPosition::try_from("CPT").is_err());
93 }
94
95 #[test]
96 fn uses_dst_as_string() {
97 assert_eq!(String::from(DfsPosition::Dst), "DST".to_owned())
98 }
99}