Skip to content

Commit

Permalink
Add more split loading checks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexozer committed Sep 23, 2024
1 parent 64bba9c commit b0a1f0b
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/split_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::time::Duration;

use crate::utils::{format_duration, Prefix, Sign};

#[derive(Serialize, Deserialize, Debug)]
pub struct SplitFile {
pub title: String,
Expand Down Expand Up @@ -125,18 +127,54 @@ pub fn read_split_file(path: &Path) -> anyhow::Result<SplitFile> {
}

if split_file.golds.len() != split_file.split_names.len() {
return Err(anyhow!("Split name count does not match gold count"));
return Err(anyhow!(
"Split name count ({}) does not match gold count ({})",
split_file.split_names.len(),
split_file.golds.len()
));
}
if split_file.personal_best.splits.len() != split_file.split_names.len() {
return Err(anyhow!(
"Split name count does not match personal best split count"
"Split name count ({}) does not match personal best split count ({})",
split_file.split_names.len(),
split_file.personal_best.splits.len(),
));
}

if split_file.personal_best.splits.last().unwrap().is_none() {
return Err(anyhow!("Last split of personal best cannot be null"));
}

for i in 0..split_file.split_names.len() {
let pb_split = split_file.personal_best.splits[i].as_ref();
let gold = split_file.golds[i].as_ref();
let prev_pb_split = if i == 0 {
None
} else {
split_file.personal_best.splits[i - 1].as_ref()
};

if let (Some(pb_split), Some(prev_pb_split)) = (pb_split, prev_pb_split) {
if pb_split.time < prev_pb_split.time {
return Err(anyhow!(
"Split {} ({}) is earlier than previous split",
i + 1,
format_duration(pb_split.time, 3, Sign::Positive, Prefix::NoneOrMinus)
));
}
if let Some(gold) = gold {
let pb_dur = pb_split.time - prev_pb_split.time;
if gold.duration > pb_dur {
return Err(anyhow!(
"Gold {} ({}) is longer than the PB segment",
i + 1,
format_duration(gold.duration, 3, Sign::Positive, Prefix::NoneOrMinus)
));
}
}
}
}

Ok(split_file)
}

Expand Down

0 comments on commit b0a1f0b

Please sign in to comment.