Skip to content

Commit

Permalink
Refactor domain
Browse files Browse the repository at this point in the history
  • Loading branch information
treiher committed Dec 5, 2024
1 parent 7c479d7 commit 0e20ebb
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 37 deletions.
18 changes: 9 additions & 9 deletions frontend/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Muscle> {
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -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));

Expand Down
4 changes: 1 addition & 3 deletions frontend/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ impl Routine {
}

pub fn stimulus_per_muscle(&self, exercises: &BTreeMap<u32, Exercise>) -> BTreeMap<u8, u32> {
let mut result: BTreeMap<u8, u32> = domain::Muscle::iter()
.map(|m| (domain::Muscle::id(*m), 0))
.collect();
let mut result: BTreeMap<u8, u32> = 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) {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/ui/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/ui/component/exercise_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub fn view(model: &Model, loading: bool, data_model: &data::Model) -> Vec<Node<
let muscle = *muscle;
move |_| Msg::FilterChanged(*muscle)
}),
&domain::Muscle::name(**muscle)
&muscle.name()
]
})
],
Expand Down Expand Up @@ -282,7 +282,7 @@ fn view_filter_dialog(
let muscle = **muscle;
move |_| Msg::FilterChanged(muscle)
}),
&domain::Muscle::name(**muscle)
&muscle.name()
]
})
]
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/ui/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Model {
|| filter
.muscles
.iter()
.all(|m| e.muscle_stimulus().contains_key(&domain::Muscle::id(*m)))
.all(|m| e.muscle_stimulus().contains_key(&m.id()))
})
.collect()
}
Expand Down
25 changes: 8 additions & 17 deletions frontend/src/ui/page/exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ fn view_muscles(model: &Model) -> Node<Msg> {
.map(|m| {
let stimulus = model
.muscle_stimulus
.get(&domain::Muscle::id(*m))
.get(&m.id())
.copied()
.unwrap_or_default();
(m, stimulus)
Expand All @@ -362,8 +362,8 @@ fn view_muscles(model: &Model) -> Node<Msg> {
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"],
Expand All @@ -379,10 +379,7 @@ fn view_muscles(model: &Model) -> Node<Msg> {
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",
]
],
Expand All @@ -392,10 +389,7 @@ fn view_muscles(model: &Model) -> Node<Msg> {
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",
]
],
Expand All @@ -405,10 +399,7 @@ fn view_muscles(model: &Model) -> Node<Msg> {
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",
]
],
Expand Down Expand Up @@ -437,9 +428,9 @@ fn view_muscles(model: &Model) -> Node<Msg> {
C!["tag"],
C!["is-link"],
C![IF![*stimulus < 100 => "is-light"]],
domain::Muscle::name(**m)
m.name()
],
domain::Muscle::description(**m),
m.description(),
)
})
]
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/ui/page/muscles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node<Msg> {
.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::<Vec<_>>(),
Expand All @@ -85,12 +85,12 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node<Msg> {
);

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)],
Expand Down

0 comments on commit 0e20ebb

Please sign in to comment.