| 1 | use chatsvc::ForcedKind; |
| 2 | |
| 3 | use crate::status_choice::StatusDuration; |
| 4 | pub use crate::status_choice::{STATUS_CHOICES, STATUS_DURATIONS, choice_label}; |
| 5 | |
| 6 | pub fn duration_target(forced: Option<ForcedKind>) -> Option<ForcedKind> { |
| 7 | forced.filter(|kind| *kind != ForcedKind::Available) |
| 8 | } |
| 9 | |
| 10 | pub fn reset_label() -> &'static str { |
| 11 | "Reset status" |
| 12 | } |
| 13 | |
| 14 | pub fn duration_menu_label() -> &'static str { |
| 15 | "Duration" |
| 16 | } |
| 17 | |
| 18 | pub fn kind_id(kind: ForcedKind) -> &'static str { |
| 19 | match kind { |
| 20 | ForcedKind::Available => "status:available", |
| 21 | ForcedKind::Busy => "status:busy", |
| 22 | ForcedKind::DoNotDisturb => "status:dnd", |
| 23 | ForcedKind::BeRightBack => "status:brb", |
| 24 | ForcedKind::Away => "status:away", |
| 25 | ForcedKind::Offline => "status:offline", |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | pub fn parse_kind_id(id: &str) -> Option<ForcedKind> { |
| 30 | match id { |
| 31 | "status:available" => Some(ForcedKind::Available), |
| 32 | "status:busy" => Some(ForcedKind::Busy), |
| 33 | "status:dnd" => Some(ForcedKind::DoNotDisturb), |
| 34 | "status:brb" => Some(ForcedKind::BeRightBack), |
| 35 | "status:away" => Some(ForcedKind::Away), |
| 36 | "status:offline" => Some(ForcedKind::Offline), |
| 37 | _ => None, |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | pub fn duration_id(duration: StatusDuration) -> &'static str { |
| 42 | match duration { |
| 43 | StatusDuration::HalfHour => "status-duration:30m", |
| 44 | StatusDuration::OneHour => "status-duration:1h", |
| 45 | StatusDuration::TwoHours => "status-duration:2h", |
| 46 | StatusDuration::Today => "status-duration:today", |
| 47 | StatusDuration::ThisWeek => "status-duration:week", |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn parse_duration_id(id: &str) -> Option<StatusDuration> { |
| 52 | match id { |
| 53 | "status-duration:30m" => Some(StatusDuration::HalfHour), |
| 54 | "status-duration:1h" => Some(StatusDuration::OneHour), |
| 55 | "status-duration:2h" => Some(StatusDuration::TwoHours), |
| 56 | "status-duration:today" => Some(StatusDuration::Today), |
| 57 | "status-duration:week" => Some(StatusDuration::ThisWeek), |
| 58 | _ => None, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | #[cfg(test)] |
| 63 | mod tests { |
| 64 | use super::*; |
| 65 | |
| 66 | #[test] |
| 67 | fn every_choice_has_a_stable_id() { |
| 68 | for kind in STATUS_CHOICES { |
| 69 | assert_eq!(parse_kind_id(kind_id(kind)), Some(kind)); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn every_duration_has_a_stable_id() { |
| 75 | for duration in STATUS_DURATIONS { |
| 76 | assert_eq!(parse_duration_id(duration_id(duration)), Some(duration)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn available_has_no_duration_target() { |
| 82 | assert_eq!(duration_target(Some(ForcedKind::Available)), None); |
| 83 | assert_eq!( |
| 84 | duration_target(Some(ForcedKind::DoNotDisturb)), |
| 85 | Some(ForcedKind::DoNotDisturb) |
| 86 | ); |
| 87 | } |
| 88 | } |