librunner 0.4.0

Rust library to assist runners on planning their workouts, races, and improve their health.
Documentation
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
pub mod utils {
    pub mod convert {
        use std::time::Duration;

        /// Creates a Duration based on the arguments hours, minutes, and seconds.
        ///
        /// Example:
        ///
        /// ```
        /// use librunner::utils::convert;
        ///
        /// let duration = convert::to_duration(4, 5, 19); // 04:05:19
        /// assert_eq!(duration.as_secs(), 14719);
        /// ```
        pub fn to_duration(hours: u64, minutes: u64, seconds: u64) -> Duration {
            let mins = if hours > 0 { hours * 60 } else { 0 } + minutes;
            let secs = if mins > 0 { mins * 60 } else { 0 } + seconds;
            Duration::new(secs, 0)
        }

        #[cfg(test)]
        mod tests {
            use crate::utils::convert;

            #[test]
            fn test_to_duration() {
                let duration = convert::to_duration(4, 5, 19);
                assert_eq!(duration.as_secs(), 14719);
            }
        }
    }
}

pub mod running {
    use std::time::Duration;

    /// A running race, already with common calculations that work with multiple scales.
    pub trait Race {
        /// The distance of one split in an implemented scale.
        const SPLIT_DISTANCE: u64;

        /// Creates a new instance of the race with the supported attributes.
        /// 
        /// Example:
        /// 
        /// ```
        /// use std::time::Duration;
        /// use librunner::running::Race;
        /// use librunner::running::MetricRace;
        /// 
        /// // Race measured in metric units
        /// let duration = Duration::new(14400, 0); // seconds
        /// let m_race: MetricRace = Race::new(42195, duration); // meters
        /// ```
        fn new(distance: u64, duration: Duration) -> Self;

        /// Creates a new instance of the race using the desired pace to calculate the duration.
        /// 
        /// Example:
        /// ```
        /// use std::time::Duration;
        /// use librunner::running::Race;
        /// use librunner::running::MetricRace;
        /// 
        /// // Race measured in metric units
        /// let pace = Duration::new(341, 0); // seconds
        /// let m_race: MetricRace = Race::new_from_pace(42195, pace); // meters
        /// ```
        fn new_from_pace(distance: u64, pace: Duration) -> Self;

        /// Returns the distance of the race.
        fn distance(&self) -> u64;

        /// Returns the duration of the race.
        fn duration(&self) -> Duration;
        
        /// Calculates the average pace based on distance and duration.
        fn average_pace(&self) -> Duration {
            return Duration::new(
                (Self::SPLIT_DISTANCE as f32 * (self.duration().as_secs() as f32 / self.distance() as f32)
            ) as u64, 0)
        }

        /// Calculates the speed of the runner to complete a distance within a duration.
        /// 
        /// Examples:
        /// 
        /// ```
        /// use std::time::Duration;
        /// use librunner::running::Race;
        /// use librunner::running::MetricRace;
        /// use librunner::running::ImperialRace;
        /// 
        /// // Race measured in metric units
        /// let duration = Duration::new(14400, 0); // seconds
        /// let m_race: MetricRace = Race::new(42195, duration); // meters
        /// assert_eq!(m_race.speed(), 2.9302084); // m/s
        /// 
        /// // Race measured in imperial units
        /// let i_race: ImperialRace = Race::new(46112, duration); // yards
        /// assert_eq!(i_race.speed(), 3.202222); // yd/s
        /// ```
        fn speed(&self) -> f32 {
            self.distance() as f32 / self.duration().as_secs() as f32
        }

        /// Calculates the number of splits based on the race distance and the split distance.
        /// The split distance is defined in each Race implementation. 1 km is a tipical example of split.
        /// 
        /// Example:
        /// 
        /// ```
        /// use std::time::Duration;
        /// use librunner::running::Race;
        /// use librunner::running::MetricRace;
        /// 
        /// let duration = Duration::new(14400, 0);
        /// let m_race: MetricRace = Race::new(42195, duration);
        /// assert_eq!(m_race.num_splits(), 43);
        /// ```
        fn num_splits(&self) -> u64 {
            self.distance() / Self::SPLIT_DISTANCE + if (self.distance() % Self::SPLIT_DISTANCE) > 0 { 1 } else { 0 }
        }

        /// Returns the splits of the race, with the average pace in each split.
        fn splits(&self) -> Vec<Duration> {
            let average_pace = self.average_pace();
            self.splits_with_pace(average_pace)
        }

        /// Returns the splits of the race with a custom pace.
        fn splits_with_pace(&self, pace: Duration) -> Vec<Duration> {
            let mut splits = Vec::new();
            
            for _n in 0..self.num_splits() {
                splits.push(pace);
            }

            splits
        }

        /// Returns the splits of the race from a higher to a lower pace, according to the degree of variation.
        /// 
        /// # Arguments
        /// 
        /// * `degree` - the degree of variation from the average pace in seconds.
        fn negative_splits(&self, degree: Duration) -> Vec<Duration> {
            // minutes between minimal and maximum pace
            let variation = (2 * degree.as_secs()) + 1;
            let num_splits = self.num_splits();
            // size of the block of splits with the same pace
            let block = num_splits / variation;
            let average_pace = self.average_pace();

            let mut negative_splits = Vec::new();
            // the pace starts high and decrements at every splits block
            let mut pace = Duration::new(average_pace.as_secs() + degree.as_secs(), 0);
            let mut block_count = 0;
            
            for _n in 0..num_splits as usize {
                if block == block_count {
                    // decrements the pace at every new block.
                    let secs = pace.as_secs() - 1u64;
                    pace = Duration::new(secs, 0);

                    block_count = 0;
                }
                negative_splits.push(pace);
                block_count += 1;
            }

            negative_splits
        }

        /// Returns the splits of the race from a lower to a higher pace, according to the degree of variation.
        /// 
        /// # Arguments
        /// 
        /// * `degree` - the degree of variation from the average pace in seconds.
        fn positive_splits(&self, degree: Duration) -> Vec<Duration> {
            let variation = (2 * degree.as_secs()) + 1;
            let num_splits = self.num_splits();
            // size of the block of splits with the same pace
            let block = num_splits / variation;
            let average_pace = self.average_pace();

            let mut positive_splits = Vec::new();
            // the pace starts high and decrements at every splits block
            let mut pace = Duration::new(average_pace.as_secs() - degree.as_secs(), 0);
            let mut block_count = 0;
            
            for _n in 0..num_splits as usize {
                if block == block_count {
                    // decrements the pace at every new block.
                    let secs = pace.as_secs() + 1u64;
                    pace = Duration::new(secs, 0);

                    block_count = 0;
                }
                positive_splits.push(pace);
                block_count += 1;
            }

            positive_splits
        }
    }

    /// A running race using the imperial scale, such as miles and yards.
    pub struct ImperialRace {
        pub distance: u64,
        pub duration: Option<Duration>
    }

    impl Race for ImperialRace {
        const SPLIT_DISTANCE: u64 = 1760; // yards

        fn new(distance: u64, duration: Duration) -> Self {
            ImperialRace {
                distance,
                duration: Some(duration)
            }
        }

        fn new_from_pace(distance: u64, pace: Duration) -> Self {
            // Creates an imperial race without duration
            let mut i_race = ImperialRace {
                distance,
                duration: None
            };
            let duration = (i_race.distance() as f32 / Self::SPLIT_DISTANCE as f32) * pace.as_secs() as f32;
            
            i_race.duration = Some(Duration::new(duration as u64, 0));

            i_race
        }

        fn distance(&self) -> u64 {
            self.distance
        }

        fn duration(&self) -> Duration {
            match self.duration {
                Some(p) => p,
                None => Duration::new(0, 0)
            }
        }
    }

    impl ImperialRace {
        /// Calculates the speed of the runner in miles per hour (mph).
        /// ```
        /// use std::time::Duration;
        /// use librunner::running::Race;
        /// use librunner::running::ImperialRace;
        /// 
        /// // Race measured in imperial units
        /// let duration = Duration::new(14400, 0); // seconds
        /// let i_race: ImperialRace = Race::new(46112, duration); // yards
        /// assert_eq!(i_race.speed_miles_hour(), 6.55); // mph
        /// ```
        pub fn speed_miles_hour(&self) -> f32 {
            let miles = self.distance() as f32 / 1760.0;
            miles / (self.duration().as_secs() as f32 / 60.0 / 60.0)
        }
    }

    /// A running race using the metric scale, such as kilometers and metters.
    pub struct MetricRace {
        pub distance: u64,
        pub duration: Option<Duration>
    }

    impl Race for MetricRace {
        const SPLIT_DISTANCE: u64 = 1000; // meters

        fn new(distance: u64, duration: Duration) -> Self {
            MetricRace {
                distance,
                duration: Some(duration)
            }
        }

        fn new_from_pace(distance: u64, pace: Duration) -> Self {
            let mut m_race = MetricRace {
                distance,
                duration: None
            };

            let duration = (m_race.distance() as f32 / Self::SPLIT_DISTANCE as f32) * pace.as_secs() as f32;
            
            m_race.duration = Some(Duration::new(duration as u64, 0));

            m_race
        }

        fn distance(&self) -> u64 {
            self.distance
        }

        fn duration(&self) -> Duration {
            match self.duration {
                Some(p) => p,
                None => Duration::new(0, 0)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use crate::running::Race;
    use crate::running::ImperialRace;
    use crate::running::MetricRace;

    #[test]
    fn test_new_metric_race() {
        let duration = Duration::new(14400, 0);
        let m_race: MetricRace = Race::new(42195, duration);
        assert_eq!(m_race.distance, 42195);
        assert_eq!(m_race.duration, Some(duration));
    }

    #[test]
    fn test_new_metric_from_pace() {
        let mp_race: MetricRace = Race::new_from_pace(42195, Duration::new(341, 0));
        // The duration calculated from the pace correct, 
        // but there is a precision issue that needs to be addressed in the future.
        assert_eq!(mp_race.duration, Some(Duration::new(14388, 0)));
    }

    #[test]
    fn test_metric_average_pace() {
        let duration = Duration::new(14400, 0);
        let m_race: MetricRace = Race::new(42195, duration);
        assert_eq!(m_race.average_pace().as_secs(), 341);
        assert_eq!(m_race.average_pace().as_secs() / 60, 5);
        assert_eq!(m_race.average_pace().as_secs() % 60, 41);
    }

    #[test]
    fn test_metric_num_splits() {
        let duration = Duration::new(14400, 0);
        let m_race: MetricRace = Race::new(42195, duration);
        assert_eq!(m_race.num_splits(), 43);
    }

    #[test]
    fn test_metric_splits_duration() {
        let duration = Duration::new(14400, 0);
        let m_race: MetricRace = Race::new(42195, duration);
        let splits = m_race.splits();
        let average_pace = m_race.average_pace();

        for split in splits {
            assert_eq!(split, average_pace);
        }
    }

    #[test]
    fn test_metric_negative_splits() {
        let duration = Duration::new(14400, 0);
        let m_race: MetricRace = Race::new(42195, duration);
        
        let degree = Duration::new(5, 0);
        let variation = (2 * degree.as_secs()) + 1;
        let block = m_race.num_splits() / variation;
        let negative_splits = m_race.negative_splits(degree);

        assert_eq!(negative_splits[0].as_secs(), 346);
        assert_eq!(negative_splits[block as usize].as_secs(), 346 - 1);
        assert_eq!(negative_splits[block as usize * 2].as_secs(), 346 - 2);
        assert_eq!(negative_splits[block as usize * variation as usize].as_secs(), 346 - variation as u64);
        assert_eq!(negative_splits[block as usize * degree.as_secs() as usize].as_secs(), m_race.average_pace().as_secs());
    }

    #[test]
    fn test_metric_positive_splits() {
        let duration = Duration::new(14400, 0);
        let m_race: MetricRace = Race::new(42195, duration);
        
        let degree = Duration::new(5, 0);
        let variation = (2 * degree.as_secs()) + 1;
        let block = m_race.num_splits() / variation;
        let positive_splits = m_race.positive_splits(degree);

        assert_eq!(positive_splits[0].as_secs(), 346 - (degree.as_secs() * 2) as u64);
        assert_eq!(positive_splits[block as usize].as_secs(), 346 - (degree.as_secs() * 2) as u64 + 1);
        assert_eq!(positive_splits[block as usize * 2].as_secs(), 346 - (degree.as_secs() * 2) as u64 + 2);
        assert_eq!(positive_splits[block as usize * variation as usize].as_secs(), 346 + 1);
        assert_eq!(positive_splits[block as usize * degree.as_secs() as usize].as_secs(), m_race.average_pace().as_secs());
    }

    #[test]
    fn test_new_imperial_race() {
        let duration = Duration::new(14400, 0);
        let i_race: ImperialRace = Race::new(46112, duration);
        assert_eq!(i_race.distance, 46112);
        assert_eq!(i_race.duration, Some(duration));
    }

    #[test]
    fn test_new_imperial_from_pace() {
        let ip_race: ImperialRace = Race::new_from_pace(46112, Duration::new(549, 0));
        // The duration calculated from the pace correct, 
        // but there is a precision issue that needs to be addressed in the future.
        assert_eq!(ip_race.duration, Some(Duration::new(14383, 0)));
    }

    #[test]
    fn test_imperial_average_pace() {
        let duration = Duration::new(14400, 0);
        let i_race: ImperialRace = Race::new(46112, duration);
        assert_eq!(i_race.average_pace().as_secs(), 549);
        assert_eq!(i_race.average_pace().as_secs() / 60, 9);
        assert_eq!(i_race.average_pace().as_secs() % 60, 9);
    }

    #[test]
    fn test_imperial_num_splits() {
        let duration = Duration::new(14400, 0);
        let i_race: ImperialRace = Race::new(46112, duration);
        assert_eq!(i_race.num_splits(), 27);
    }

    #[test]
    fn test_imperial_splits_duration() {
        let duration = Duration::new(14400, 0);
        let i_race: ImperialRace = Race::new(46112, duration);
        let splits = i_race.splits();
        let average_pace = i_race.average_pace();

        for split in splits {
            assert_eq!(split, average_pace);
        }
    }
}