Skip to content

Commit f9dcff8

Browse files
authored
Fix Panic When Cleaning Sum of Best (#871)
When cleaning the sum of best, the message tries to be as helpful as possible by showing as much information about the problematic segments as possible. Part of that is information about the date of the attempt. However, as it turns out, the attempt may not actually exist. This shouldn't generally happen, but we can't trust the correctness of the splits files, so for whatever reason, the attempt may be missing. In that case, we simply skip that information.
1 parent f2d7b3a commit f9dcff8

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/run/editor/cleaning.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
use time::UtcOffset;
1010

1111
use crate::{
12-
analysis::sum_of_segments::{best, track_branch, Prediction},
12+
Attempt, Run, Segment, TimeSpan, TimingMethod,
13+
analysis::sum_of_segments::{Prediction, best, track_branch},
1314
platform::{prelude::*, to_local},
1415
timing::formatter::{SegmentTime, TimeFormatter},
15-
Attempt, Run, Segment, TimeSpan, TimingMethod,
1616
};
1717
use core::{fmt, mem::replace};
1818

@@ -56,7 +56,7 @@ pub struct PotentialCleanUp<'r> {
5656
ending_segment: &'r Segment,
5757
time_between: TimeSpan,
5858
combined_sum_of_best: Option<TimeSpan>,
59-
attempt: &'r Attempt,
59+
attempt: Option<&'r Attempt>,
6060
method: TimingMethod,
6161
clean_up: CleanUp,
6262
}
@@ -98,7 +98,7 @@ impl fmt::Display for PotentialCleanUp<'_> {
9898
)?;
9999
}
100100

101-
if let Some(started) = self.attempt.started() {
101+
if let Some(started) = self.attempt.and_then(|a| a.started()) {
102102
let local = to_local(started.time);
103103
// We want to show the time zone in case it can't be resolved and
104104
// defaults to UTC.
@@ -260,11 +260,12 @@ fn check_prediction<'a>(
260260
.expect("Start time must not be empty")
261261
.time
262262
}),
263+
// The attempt may not exist, as we have all sorts of weird
264+
// malformed files.
263265
attempt: run
264266
.attempt_history()
265267
.iter()
266-
.find(|attempt| attempt.index() == run_index)
267-
.expect("The attempt has to exist"),
268+
.find(|attempt| attempt.index() == run_index),
268269
method,
269270
clean_up: CleanUp {
270271
ending_index,

tests/clean_sum_of_best.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use livesplit_core::run::{editor::SumOfBestCleaner, parser::livesplit};
2+
3+
mod run_files;
4+
5+
#[test]
6+
fn dont_panic_if_attempt_doesnt_exist() {
7+
// There's an attempt that does not exist that it wants to clean up. We
8+
// shouldn't panic then and it should just ignore it.
9+
let mut run = livesplit::parse(run_files::CLEAN_SUM_OF_BEST).unwrap();
10+
let mut cleaner = SumOfBestCleaner::new(&mut run);
11+
let cleanup = cleaner.next_potential_clean_up().unwrap().into();
12+
cleaner.apply(cleanup);
13+
assert!(cleaner.next_potential_clean_up().is_none());
14+
}

tests/run_files/clean_sum_of_best.lss

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

tests/run_files/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ pub const TIME_SPLIT_TRACKER_WITHOUT_ATTEMPT_COUNT: &str = include_str!("1734.ti
2525
pub const TIME_SPLIT_TRACKER: &str = include_str!("timesplittracker.txt");
2626
pub const URN: &str = include_str!("urn.json");
2727
pub const WSPLIT: &str = include_str!("wsplit");
28+
pub const CLEAN_SUM_OF_BEST: &str = include_str!("clean_sum_of_best.lss");

0 commit comments

Comments
 (0)