1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
#![feature(get_mut_unchecked)]
use std::collections::BTreeMap;
use std::io::{self, Read, Stdin, Stdout, Write};
use std::iter::RepeatN;
use std::rc::Rc;
struct InputHelper {
stdin: Stdin,
stdout: Stdout,
buf: Vec<u8>,
}
impl InputHelper {
fn with_capacity(cap: usize) -> Self {
let stdin = io::stdin();
let stdout = io::stdout();
Self {
stdin,
stdout,
buf: vec![0u8; cap],
}
}
fn ask(&mut self, msg: &str) -> &[u8] {
self.stdout.write(msg.as_bytes()).unwrap();
self.stdout.write(b"\n").unwrap();
let len = self.stdin.read(&mut self.buf).unwrap();
&self.buf[..len].trim_ascii()
}
fn ask_num(&mut self, msg: &str) -> i64 {
let buf = self.ask(msg);
std::str::from_utf8(buf).unwrap().parse().unwrap()
}
}
#[derive(Debug)]
struct Exercise {
name: Vec<u8>,
description: Vec<u8>,
}
#[derive(Debug, Clone)]
struct Workout {
exercises: Vec<RepeatN<Rc<Exercise>>>,
}
fn main() {
let mut exercises = BTreeMap::new();
let mut workouts = Vec::new();
let mut input = InputHelper::with_capacity(0x100);
println!("Welcome to your personal training helper! Here are your options:");
loop {
println!("1. : add a new exercise to your portfolio");
println!("2. : plan a new workout");
println!("3. : start a training session");
println!("4. : edit an exercise");
println!("5. : exit the app");
let line = input.ask("Choose an option: ").trim_ascii();
match &*line {
b"1" => {
let name = input.ask("What's the name of your exercise? ").to_owned();
let description = input
.ask("what is the description of your exercise? ")
.to_owned();
let name2 = name.clone();
let exercise: Exercise = Exercise { name, description };
exercises.insert(name2, Rc::new(exercise));
println!("Exercise added!");
}
b"2" => {
let num_exercises = input.ask_num("How many exercises should your workout have? ");
let mut workout = Workout {
exercises: Vec::new(),
};
for _ in 0..num_exercises {
let name = input.ask("Enter the name of the exercise: ");
if let Some(exercise) = exercises.get(name) {
let num_repetitions =
input.ask_num("How many times should your exercise be repeated? ");
workout.exercises.push(std::iter::repeat_n(
Rc::clone(exercise),
num_repetitions as usize,
));
} else {
println!("No exercise found with that name.");
}
}
println!("Your workout has id {}", workouts.len());
workouts.push(workout);
}
b"3" => {
let id = input.ask_num("what's the id of your workout? ");
let workout = &workouts[id as usize];
for exercise in workout.exercises.iter().cloned() {
for ex in exercise {
println!("{:?} - {:?}", ex.name, ex.description); // pls help, this looks weird :(
}
}
}
b"4" => {
let name = input.ask("Enter the name of the exercise you want to edit: ");
if let Some(exercise) = exercises.get_mut(name) {
let description = input.ask("Enter the new description: ");
unsafe {
Rc::get_mut_unchecked(exercise)
.description
.copy_from_slice(description)
}
println!("Exercise updated!");
} else {
println!("No exercise found with that name.");
}
}
b"5" => break,
_ => println!("That was not a valid option"),
}
}
}
|