From 0e20ebbecd7c7757e4299c60ecb1b702002deb15 Mon Sep 17 00:00:00 2001 From: Tobias Reiher <15232394+treiher@users.noreply.github.com> Date: Thu, 5 Dec 2024 21:21:18 +0100 Subject: [PATCH] Refactor domain --- frontend/src/domain.rs | 18 ++++++++-------- frontend/src/storage.rs | 4 +--- frontend/src/ui/common.rs | 4 ++-- frontend/src/ui/component/exercise_list.rs | 4 ++-- frontend/src/ui/data.rs | 2 +- frontend/src/ui/page/exercise.rs | 25 +++++++--------------- frontend/src/ui/page/muscles.rs | 6 +++--- 7 files changed, 26 insertions(+), 37 deletions(-) diff --git a/frontend/src/domain.rs b/frontend/src/domain.rs index fee677e..afbdc4c 100644 --- a/frontend/src/domain.rs +++ b/frontend/src/domain.rs @@ -58,8 +58,8 @@ impl Muscle { MUSCLES.iter() } - pub fn id(muscle: Muscle) -> u8 { - muscle as u8 + pub fn id(self) -> u8 { + self as u8 } pub fn from_repr(repr: u8) -> Option { @@ -86,8 +86,8 @@ impl Muscle { } } - pub fn name(muscle: Muscle) -> &'static str { - match muscle { + pub fn name(self) -> &'static str { + match self { Muscle::Neck => "Neck", Muscle::Pecs => "Pecs", Muscle::Traps => "Traps", @@ -109,9 +109,9 @@ impl Muscle { } } - pub fn description(muscle: Muscle) -> &'static str { + pub fn description(self) -> &'static str { #[allow(clippy::match_same_arms)] - match muscle { + match self { Muscle::Neck => "", Muscle::Pecs => "Chest", Muscle::Traps => "Upper back", @@ -154,7 +154,7 @@ mod tests { #[test] fn test_muscle_id() { for muscle in Muscle::iter() { - assert_eq!(Muscle::from_repr(Muscle::id(*muscle)).unwrap(), *muscle); + assert_eq!(Muscle::from_repr(muscle.id()).unwrap(), *muscle); } assert_eq!(Muscle::from_repr(u8::MAX), None); @@ -165,7 +165,7 @@ mod tests { let mut names = HashSet::new(); for muscle in Muscle::iter() { - let name = Muscle::name(*muscle); + let name = muscle.name(); assert!(!name.is_empty()); assert!(!names.contains(name)); @@ -179,7 +179,7 @@ mod tests { let mut descriptions = HashSet::new(); for muscle in Muscle::iter() { - let description = Muscle::description(*muscle); + let description = muscle.description(); assert!(description.is_empty() || !descriptions.contains(description)); diff --git a/frontend/src/storage.rs b/frontend/src/storage.rs index 694328c..55b8066 100644 --- a/frontend/src/storage.rs +++ b/frontend/src/storage.rs @@ -225,9 +225,7 @@ impl Routine { } pub fn stimulus_per_muscle(&self, exercises: &BTreeMap) -> BTreeMap { - let mut result: BTreeMap = domain::Muscle::iter() - .map(|m| (domain::Muscle::id(*m), 0)) - .collect(); + let mut result: BTreeMap = domain::Muscle::iter().map(|m| (m.id(), 0)).collect(); for section in &self.sections { for (id, stimulus) in section.stimulus_per_muscle(exercises) { if result.contains_key(&id) { diff --git a/frontend/src/ui/common.rs b/frontend/src/ui/common.rs index f6d60a5..337e7cf 100644 --- a/frontend/src/ui/common.rs +++ b/frontend/src/ui/common.rs @@ -960,8 +960,8 @@ where stimulus_per_muscle.sort_by(|a, b| b.1.cmp(&a.1)); let mut groups = [vec![], vec![], vec![], vec![]]; for (muscle, stimulus) in stimulus_per_muscle { - let name = domain::Muscle::name(muscle); - let description = domain::Muscle::description(muscle); + let name = muscle.name(); + let description = muscle.description(); let sets = f64::from(stimulus) / 100.0; let sets_str = format!("{:.1$}", sets, usize::from(sets.fract() != 0.0)); if sets > 10.0 { diff --git a/frontend/src/ui/component/exercise_list.rs b/frontend/src/ui/component/exercise_list.rs index 3855e7d..20430cd 100644 --- a/frontend/src/ui/component/exercise_list.rs +++ b/frontend/src/ui/component/exercise_list.rs @@ -197,7 +197,7 @@ pub fn view(model: &Model, loading: bool, data_model: &data::Model) -> Vec Node { .map(|m| { let stimulus = model .muscle_stimulus - .get(&domain::Muscle::id(*m)) + .get(&m.id()) .copied() .unwrap_or_default(); (m, stimulus) @@ -362,8 +362,8 @@ fn view_muscles(model: &Model) -> Node { C!["is-mobile"], div![ C!["column"], - p![domain::Muscle::name(m)], - p![C!["is-size-7"], domain::Muscle::description(m)] + p![m.name()], + p![C!["is-size-7"], m.description()] ], div![ C!["column"], @@ -379,10 +379,7 @@ fn view_muscles(model: &Model) -> Node { C!["button"], C!["is-small"], C![IF![*stimulus == 100 => "is-link"]], - &ev(Ev::Click, move |_| Msg::SetMuscleStimulus( - domain::Muscle::id(m), - 100 - )), + &ev(Ev::Click, move |_| Msg::SetMuscleStimulus(m.id(), 100)), "primary", ] ], @@ -392,10 +389,7 @@ fn view_muscles(model: &Model) -> Node { C!["button"], C!["is-small"], C![IF![*stimulus > 0 && *stimulus < 100 => "is-link"]], - &ev(Ev::Click, move |_| Msg::SetMuscleStimulus( - domain::Muscle::id(m), - 50 - )), + &ev(Ev::Click, move |_| Msg::SetMuscleStimulus(m.id(), 50)), "secondary", ] ], @@ -405,10 +399,7 @@ fn view_muscles(model: &Model) -> Node { C!["button"], C!["is-small"], C![IF![*stimulus == 0 => "is-link"]], - &ev(Ev::Click, move |_| Msg::SetMuscleStimulus( - domain::Muscle::id(m), - 0 - )), + &ev(Ev::Click, move |_| Msg::SetMuscleStimulus(m.id(), 0)), "none", ] ], @@ -437,9 +428,9 @@ fn view_muscles(model: &Model) -> Node { C!["tag"], C!["is-link"], C![IF![*stimulus < 100 => "is-light"]], - domain::Muscle::name(**m) + m.name() ], - domain::Muscle::description(**m), + m.description(), ) }) ] diff --git a/frontend/src/ui/page/muscles.rs b/frontend/src/ui/page/muscles.rs index 4e60aa0..6e94561 100644 --- a/frontend/src/ui/page/muscles.rs +++ b/frontend/src/ui/page/muscles.rs @@ -76,7 +76,7 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node { .values() .filter_map(|s| { s.stimulus_per_muscle(&data_model.exercises) - .get(&domain::Muscle::id(*m)) + .get(&m.id()) .map(|stimulus| (s.date, *stimulus as f32 / 100.)) }) .collect::>(), @@ -85,12 +85,12 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node { ); div![ - common::view_title(&span![domain::Muscle::name(*m)], 1), + common::view_title(&span![m.name()], 1), div![ C!["block"], C!["is-size-7"], C!["has-text-centered"], - domain::Muscle::description(*m) + m.description() ], common::view_chart( &[("Set volume (7 day total)", common::COLOR_SET_VOLUME)],