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
use crate::{timing, Image, RealTime, Run, Segment, TimeSpan};
use std::borrow::Cow;
use std::io::{self, BufRead};
use std::num::ParseIntError;
use std::result::Result as StdResult;
quick_error! {
#[derive(Debug)]
pub enum Error {
Empty {}
ExpectedCategoryName {}
ExpectedAttemptCount {}
ExpectedSegmentName {}
ExpectedSplitTime {}
ExpectedBestSegment {}
Attempt(err: ParseIntError) {
from()
}
Time(err: timing::ParseError) {
from()
}
Io(err: io::Error) {
from()
}
}
}
pub type Result<T> = StdResult<T, Error>;
fn unescape(text: &str) -> Cow<'_, str> {
if text.contains('‡') {
text.replace('‡', ",").into()
} else {
text.into()
}
}
pub fn parse<R: BufRead>(source: R, load_icons: bool) -> Result<Run> {
let mut run = Run::new();
let mut icon_buf = Vec::new();
let mut lines = source.lines();
let line = lines.next().ok_or(Error::Empty)??;
let mut splits = line.split(',');
run.set_category_name(unescape(splits.next().ok_or(Error::ExpectedCategoryName)?));
run.set_attempt_count(splits.next().ok_or(Error::ExpectedAttemptCount)?.parse()?);
for line in lines {
let line = line?;
if !line.is_empty() {
let mut splits = line.split(',');
let mut segment =
Segment::new(unescape(splits.next().ok_or(Error::ExpectedSegmentName)?));
let time: TimeSpan = splits.next().ok_or(Error::ExpectedSplitTime)?.parse()?;
if time != TimeSpan::zero() {
segment.set_personal_best_split_time(RealTime(Some(time)).into());
}
let time: TimeSpan = splits.next().ok_or(Error::ExpectedBestSegment)?.parse()?;
if time != TimeSpan::zero() {
segment.set_best_segment_time(RealTime(Some(time)).into());
}
if load_icons {
if let Some(icon_path) = splits.next() {
if !icon_path.is_empty() {
if let Ok(image) =
Image::from_file(unescape(icon_path).as_ref(), &mut icon_buf)
{
segment.set_icon(image);
}
}
}
}
run.push_segment(segment);
} else {
break;
}
}
Ok(run)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counters() {
const RUN: &[u8] = br#"Run Title:,1
SegmentName,0:00:00.00,0.00
SegmentName,0:00:00.00,0.00
SegmentName,0:00:00.00,0.00
Counter,1,True
Counter,1,True
"#;
let run = parse(RUN, false).unwrap();
assert_eq!(run.len(), 3);
}
}