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
//! Provides the parser for generic Splits I/O splits files.
use crate::{
platform::prelude::*, util::PopulateString, Run, Segment as LiveSplitSegment, Time, TimeSpan,
};
use alloc::borrow::Cow;
use core::result::Result as StdResult;
use serde::{Deserialize, Serialize};
use serde_json::Error as JsonError;
/// The Error type for splits files that couldn't be parsed by the generic
/// Splits I/O Parser.
#[derive(Debug, snafu::Snafu)]
#[snafu(context(suffix(false)))]
pub enum Error {
/// Failed to parse JSON.
Json {
/// The underlying error.
#[cfg_attr(not(feature = "std"), snafu(source(false)))]
source: JsonError,
},
}
/// The Result type for the generic Splits I/O Parser.
pub type Result<T> = StdResult<T, Error>;
/// Duration holds a realtime duration and a gametime duration.
#[derive(Clone, PartialEq, Debug, Default, Deserialize, Serialize)]
#[serde(rename = "duration")]
struct Duration {
/// Gametime (Milliseconds) is a duration of milliseconds in game-world time.
#[serde(rename = "gametimeMS")]
gametime_ms: Option<f64>,
/// Realtime (Milliseconds) is a duration of milliseconds in real-world time.
#[serde(rename = "realtimeMS")]
realtime_ms: Option<f64>,
}
/// Run Time represents a moment inside a run, and indicates the duration of the run so far at that
/// moment. It holds a realtime run duration so far and a gametime run duration so far.
#[derive(Clone, PartialEq, Debug, Default, Deserialize, Serialize)]
#[serde(rename = "runTime")]
struct RunTime {
/// Gametime (Milliseconds) is a duration a run so far in milliseconds.
#[serde(rename = "gametimeMS")]
gametime_ms: Option<f64>,
/// Realtime (Milliseconds) is a duration of a run so far in milliseconds.
#[serde(rename = "realtimeMS")]
realtime_ms: Option<f64>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
struct Attempt {
/// Attempt Number is the number of lifetime attempts the runner will have made after this one.
/// The Attempt Number for an attempt is a label, not an index; the first attempt for a
/// category has an Attempt Number of 1 (not 0).
#[serde(rename = "attemptNumber")]
attempt_number: i64,
duration: Option<Duration>,
}
#[derive(Clone, PartialEq, Debug, Default, Deserialize, Serialize)]
struct Attempts {
/// Histories is an array of previous attempts by this runner of this category.
histories: Option<Vec<Attempt>>,
/// Total holds the total number of attempts for this category.
total: Option<u32>,
}
#[derive(Clone, PartialEq, Debug, Default, Deserialize, Serialize)]
struct CategoryLinks<'a> {
/// Speedrun.com ID specifies the category's Speedrun.com ID.
#[serde(rename = "speedruncomID")]
#[serde(borrow)]
speedruncom_id: Option<Cow<'a, str>>,
/// Splits I/O ID specifies the category's Splits I/O ID.
#[serde(rename = "splitsioID")]
#[serde(borrow)]
splitsio_id: Option<Cow<'a, str>>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
struct Category<'a> {
/// Links specifies the category's identity in other services.
links: Option<CategoryLinks<'a>>,
/// Longname is a human-readable category name, intended for display to users.
#[serde(borrow)]
longname: Cow<'a, str>,
/// Shortname is a machine-readable category name, intended for use in APIs, databases, URLs,
/// and filenames.
#[serde(borrow)]
shortname: Option<Cow<'a, str>>,
}
#[derive(Clone, PartialEq, Debug, Default, Deserialize, Serialize)]
struct GameLinks<'a> {
/// Speedrun.com ID specifies the game's Speedrun.com ID.
#[serde(rename = "speedruncomID")]
#[serde(borrow)]
speedruncom_id: Option<Cow<'a, str>>,
/// Splits I/O ID specifies the game's Splits I/O ID.
#[serde(rename = "splitsioID")]
#[serde(borrow)]
splitsio_id: Option<Cow<'a, str>>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
struct Game<'a> {
/// Links specifies the game's identity in other services.
links: Option<GameLinks<'a>>,
/// Longname is a human-readable game name, intended for display to users.
#[serde(borrow)]
longname: Cow<'a, str>,
/// Shortname is a machine-readable game name, intended for use in APIs, databases, URLs, and
/// filenames.
#[serde(borrow)]
shortname: Option<Cow<'a, str>>,
}
#[derive(Clone, PartialEq, Debug, Default, Deserialize, Serialize)]
struct RunLinks<'a> {
/// Speedrun.com ID is the run's ID on Speedrun.com. This can be used to communicate with the
/// Speedrun.com API.
#[serde(rename = "speedruncomID")]
#[serde(borrow)]
speedruncom_id: Option<Cow<'a, str>>,
/// Splits I/O ID is the run's ID on Splits I/O. This can be used to communicate with the
/// Splits I/O API.
#[serde(rename = "splitsioID")]
#[serde(borrow)]
splitsio_id: Option<Cow<'a, str>>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
struct Pause<'a> {
/// Ended At is the date and time at which the pause was ended, specified in RFC 3339 format.
#[serde(rename = "endedAt")]
#[serde(borrow)]
ended_at: Option<Cow<'a, str>>,
/// Started At is the date and time at which the pause was started, specified in RFC 3339
/// format.
#[serde(rename = "startedAt")]
#[serde(borrow)]
started_at: Cow<'a, str>,
}
#[derive(Clone, PartialEq, Debug, Default, Deserialize, Serialize)]
struct RunnerLinks<'a> {
/// Speedrun.com ID specifies the runner's Speedrun.com ID.
#[serde(rename = "speedruncomID")]
#[serde(borrow)]
speedruncom_id: Option<Cow<'a, str>>,
/// Splits I/O ID specifies the runner's Splits I/O ID.
#[serde(rename = "splitsioID")]
#[serde(borrow)]
splitsio_id: Option<Cow<'a, str>>,
/// Twitch ID specifies the runner's Twitch ID.
#[serde(rename = "twitchID")]
#[serde(borrow)]
twitch_id: Option<Cow<'a, str>>,
/// Twitter ID specifies the runner's Twitter ID.
#[serde(rename = "twitterID")]
#[serde(borrow)]
twitter_id: Option<Cow<'a, str>>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
struct Runner<'a> {
/// Links specifies the runner's identity in other services.
links: Option<RunnerLinks<'a>>,
/// Longname is a human-readable runner name, intended for display to users.
#[serde(borrow)]
longname: Option<Cow<'a, str>>,
/// Shortname is a machine-readable runner name, intended for use in APIs, databases, URLs, and
/// filenames.
#[serde(borrow)]
shortname: Cow<'a, str>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
struct SegmentHistoryElement {
/// Attempt Number is the number of lifetime attempts the runner will have made on this
/// category after this one. Generally these attempt numbers should correspond to those in
/// Attempts -> History, although a number given here may not be present there if the run was
/// reset before completion.
#[serde(rename = "attemptNumber")]
attempt_number: i64,
#[serde(rename = "endedAt")]
ended_at: Option<RunTime>,
/// Is Reset should be true if the runner reset the run during this segment. If so, this and
/// all future segments' Ended Ats for this run are ignored.
#[serde(rename = "isReset")]
is_reset: Option<bool>,
/// Is Skipped should be true if the runner skipped over the split that ends this segment,
/// rather than splitting. If so, this segment's Ended At is ignored.
#[serde(rename = "isSkipped")]
is_skipped: Option<bool>,
}
#[derive(Clone, PartialEq, Debug, Default, Deserialize, Serialize)]
struct Segment<'a> {
#[serde(rename = "bestDuration")]
best_duration: Option<Duration>,
#[serde(rename = "endedAt")]
ended_at: Option<RunTime>,
/// Histories is an array of previous completions of this segment by this runner.
histories: Option<Vec<SegmentHistoryElement>>,
/// Is Reset should be true if the runner reset the run during this segment. If so, this and
/// all future segments' Ended Ats for this run are ignored.
#[serde(rename = "isReset")]
is_reset: Option<bool>,
/// Is Skipped should be true if the runner skipped over the split that ends this segment,
/// rather than splitting. If so, this segment's Ended At is ignored.
#[serde(rename = "isSkipped")]
is_skipped: Option<bool>,
/// Name is the runner-provided name of this segment
#[serde(borrow)]
name: Option<Cow<'a, str>>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
struct Timer<'a> {
/// Longname is a human-readable timer name, intended for display to users.
#[serde(borrow)]
longname: Cow<'a, str>,
/// Shortname is a machine-readable timer name, intended for use in APIs, databases, URLs, and
/// filenames.
#[serde(borrow)]
shortname: Cow<'a, str>,
/// Version is the version of the timer used to record this run. Semantic Versioning is
/// strongly recommended but not enforced.
#[serde(borrow)]
version: Cow<'a, str>,
/// Website is the URL for the timer's website.
#[serde(borrow)]
website: Option<Cow<'a, str>>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
struct Splits<'a> {
/// Schema Version specifies which version of the Splits I/O JSON Schema is being used. This
/// schema specifies only v1.0.0.
#[serde(rename = "_schemaVersion")]
#[serde(borrow)]
_schemaversion: Cow<'a, str>,
/// Attempts contains historical information about previous runs by this runner in this
/// category.
attempts: Option<Attempts>,
/// Category specifies information about the category being run.
category: Option<Category<'a>>,
/// Ended At is the date and time at which the run was ended, specified in RFC 3339 format.
#[serde(rename = "endedAt")]
#[serde(borrow)]
ended_at: Option<Cow<'a, str>>,
/// Game specifies information about the game being run.
game: Option<Game<'a>>,
/// Image URL is the location of an image associated with this run. Often this is a screenshot
/// of the timer at run completion, but can be anything the runner wants displayed alongside
/// the run.
#[serde(rename = "imageURL")]
#[serde(borrow)]
image_url: Option<Cow<'a, str>>,
/// Links specifies the run's identity in other services.
links: Option<RunLinks<'a>>,
/// Pauses holds runner-caused pauses that took place during the run.
pauses: Option<Vec<Pause<'a>>>,
/// Runners is an array of people who participated in this run. Some games and categories call
/// for cooperative play, but otherwise this will usually be just one person.
runners: Option<Vec<Runner<'a>>>,
/// Segments is an array of all segments for this run.
segments: Option<Vec<Segment<'a>>>,
/// Started At is the date and time at which the run was started, specified in RFC 3339 format.
#[serde(rename = "startedAt")]
#[serde(borrow)]
started_at: Option<Cow<'a, str>>,
/// Timer holds information about the timer used to record the run.
timer: Timer<'a>,
/// Video URL is the location of a VOD of the run.
#[serde(rename = "videoURL")]
#[serde(borrow)]
video_url: Option<Cow<'a, str>>,
}
impl From<Option<Duration>> for Time {
fn from(duration: Option<Duration>) -> Time {
duration.map(Into::into).unwrap_or_default()
}
}
impl From<Duration> for Time {
fn from(duration: Duration) -> Time {
let mut time = Time::new();
if let Some(ms) = duration.realtime_ms {
time.real_time = Some(TimeSpan::from_milliseconds(ms));
}
if let Some(ms) = duration.gametime_ms {
time.game_time = Some(TimeSpan::from_milliseconds(ms));
}
time
}
}
impl From<Option<RunTime>> for Time {
fn from(time: Option<RunTime>) -> Time {
time.map(Into::into).unwrap_or_default()
}
}
impl From<RunTime> for Time {
fn from(run_time: RunTime) -> Time {
let mut time = Time::new();
if let Some(ms) = run_time.realtime_ms {
time.real_time = Some(TimeSpan::from_milliseconds(ms));
}
if let Some(ms) = run_time.gametime_ms {
time.game_time = Some(TimeSpan::from_milliseconds(ms));
}
time
}
}
/// Attempts to parse a generic Splits I/O splits file.
pub fn parse(source: &str) -> Result<(Run, Cow<'_, str>)> {
let splits: Splits<'_> =
serde_json::from_str(source).map_err(|source| Error::Json { source })?;
let mut run = Run::new();
if let Some(game) = splits.game {
run.set_game_name(game.longname);
}
if let Some(category) = splits.category {
run.set_category_name(category.longname);
}
if let Some(attempts) = splits.attempts {
if let Some(total) = attempts.total {
run.set_attempt_count(total);
}
for attempt in attempts.histories.into_iter().flatten() {
run.add_attempt_with_index(
attempt.duration.into(),
attempt.attempt_number as i32,
None,
None,
None,
);
}
}
if let Some(runner) = splits
.runners
.and_then(|runners| runners.into_iter().next())
{
let name = runner.longname.unwrap_or(runner.shortname);
if !name.trim_start().is_empty() {
run.metadata_mut()
.custom_variable_mut("Runner")
.permanent()
.set_value(name);
}
if let Some(links) = runner.links {
if let Some(twitter_id) = links.twitter_id {
run.metadata_mut()
.custom_variable_mut("Twitter")
.permanent()
.set_value(twitter_id);
}
if let Some(twitch_id) = links.twitch_id {
run.metadata_mut()
.custom_variable_mut("Twitch")
.permanent()
.set_value(twitch_id);
}
if let Some(speedruncom_id) = links.speedruncom_id {
run.metadata_mut()
.custom_variable_mut("speedrun.com")
.permanent()
.set_value(speedruncom_id);
}
if let Some(splitsio_id) = links.splitsio_id {
run.metadata_mut()
.custom_variable_mut("Splits I/O")
.permanent()
.set_value(splitsio_id);
}
}
}
if let Some(segments) = splits.segments {
run.segments_mut().extend(segments.into_iter().map(|split| {
let mut segment = LiveSplitSegment::new(split.name.unwrap_or_default());
segment.set_personal_best_split_time(split.ended_at.into());
segment.set_best_segment_time(split.best_duration.into());
if let Some(mut history) = split.histories {
let segment_history = segment.segment_history_mut();
history.sort_unstable_by_key(|x| x.attempt_number);
for element in history {
segment_history.insert(element.attempt_number as i32, element.ended_at.into());
}
}
segment
}));
}
if let Some(links) = splits.links {
if let Some(link) = links.speedruncom_id {
link.populate(&mut run.metadata_mut().run_id);
}
}
let timer = if splits.timer.longname.is_empty() {
"Generic Timer".into()
} else {
splits.timer.longname
};
Ok((run, timer))
}