livesplit-core 0.13.0

livesplit-core is a library that provides a lot of functionality for creating a speedrun timer.
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
use crate::{
    component::{
        splits::{ColumnStartWith, ColumnUpdateTrigger, ColumnUpdateWith},
        timer::DeltaGradient,
    },
    hotkey::Hotkey,
    layout::LayoutDirection,
    platform::prelude::*,
    settings::{Alignment, Color, Font, Gradient, ListGradient},
    timing::formatter::{Accuracy, DigitsFormat},
    TimingMethod,
};
use core::result::Result as StdResult;
use serde::{Deserialize, Serialize};

/// Describes the kind of a column.
#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ColumnKind {
    /// The column shows a time.
    Time,
    /// The column shows a variable.
    Variable,
}

/// Describes a setting's value. Such a value can be of a variety of different
/// types.
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub enum Value {
    /// A boolean value.
    Bool(bool),
    /// An unsigned integer.
    UInt(u64),
    /// An integer.
    Int(i64),
    /// A string.
    String(String),
    /// An optional string.
    OptionalString(Option<String>),
    /// An accuracy, describing how many digits to show for the fractional part
    /// of a time.
    Accuracy(Accuracy),
    /// A digits format, describing how many digits to show for the main part of
    /// a time.
    DigitsFormat(DigitsFormat),
    /// An optional timing method.
    OptionalTimingMethod(Option<TimingMethod>),
    /// A color.
    Color(Color),
    /// An optional color.
    OptionalColor(Option<Color>),
    /// A gradient.
    Gradient(Gradient),
    /// A gradient designed for use with lists.
    ListGradient(ListGradient),
    /// An alignment for the Title Component's title.
    Alignment(Alignment),
    /// A column kind.
    ColumnKind(ColumnKind),
    /// A value describing what a column of the Splits Component starts out
    /// with.
    ColumnStartWith(ColumnStartWith),
    /// A value describing what a column of the Splits Component gets updated
    /// with.
    ColumnUpdateWith(ColumnUpdateWith),
    /// A value describing when to update a column of the Splits Component.
    ColumnUpdateTrigger(ColumnUpdateTrigger),
    /// A value describing what hotkey to press to trigger a certain action.
    Hotkey(Option<Hotkey>),
    /// A value describing the direction of a layout.
    LayoutDirection(LayoutDirection),
    /// A value describing a font to use. `None` if a default font should be
    /// used.
    Font(Option<Font>),
    /// A gradient that may or may not take one of it's colors from the current
    /// delta.
    DeltaGradient(DeltaGradient),
}

impl From<bool> for Value {
    fn from(x: bool) -> Self {
        Value::Bool(x)
    }
}

impl From<u64> for Value {
    fn from(x: u64) -> Self {
        Value::UInt(x)
    }
}

impl From<i64> for Value {
    fn from(x: i64) -> Self {
        Value::Int(x)
    }
}

impl From<String> for Value {
    fn from(x: String) -> Self {
        Value::String(x)
    }
}

impl From<Option<String>> for Value {
    fn from(x: Option<String>) -> Self {
        Value::OptionalString(x)
    }
}

impl From<Accuracy> for Value {
    fn from(x: Accuracy) -> Self {
        Value::Accuracy(x)
    }
}

impl From<DigitsFormat> for Value {
    fn from(x: DigitsFormat) -> Self {
        Value::DigitsFormat(x)
    }
}

impl From<Option<TimingMethod>> for Value {
    fn from(x: Option<TimingMethod>) -> Self {
        Value::OptionalTimingMethod(x)
    }
}

impl From<Color> for Value {
    fn from(x: Color) -> Self {
        Value::Color(x)
    }
}

impl From<Option<Color>> for Value {
    fn from(x: Option<Color>) -> Self {
        Value::OptionalColor(x)
    }
}

impl From<Gradient> for Value {
    fn from(x: Gradient) -> Self {
        Value::Gradient(x)
    }
}

impl From<ListGradient> for Value {
    fn from(x: ListGradient) -> Self {
        Value::ListGradient(x)
    }
}

impl From<Alignment> for Value {
    fn from(x: Alignment) -> Self {
        Value::Alignment(x)
    }
}

impl From<ColumnStartWith> for Value {
    fn from(x: ColumnStartWith) -> Self {
        Value::ColumnStartWith(x)
    }
}

impl From<ColumnUpdateWith> for Value {
    fn from(x: ColumnUpdateWith) -> Self {
        Value::ColumnUpdateWith(x)
    }
}

impl From<ColumnUpdateTrigger> for Value {
    fn from(x: ColumnUpdateTrigger) -> Self {
        Value::ColumnUpdateTrigger(x)
    }
}

impl From<Option<Hotkey>> for Value {
    fn from(x: Option<Hotkey>) -> Self {
        Value::Hotkey(x)
    }
}

impl From<LayoutDirection> for Value {
    fn from(x: LayoutDirection) -> Self {
        Value::LayoutDirection(x)
    }
}

impl From<Option<Font>> for Value {
    fn from(x: Option<Font>) -> Self {
        Value::Font(x)
    }
}

impl From<DeltaGradient> for Value {
    fn from(x: DeltaGradient) -> Self {
        Value::DeltaGradient(x)
    }
}

impl From<ColumnKind> for Value {
    fn from(x: ColumnKind) -> Self {
        Value::ColumnKind(x)
    }
}

/// The Error type for values that couldn't be converted.
#[derive(Debug, snafu::Snafu)]
pub enum Error {
    /// The value couldn't be converted because it had an incompatible type.
    WrongType,
}

/// The Result type for conversions from Values to other types.
pub type Result<T> = StdResult<T, Error>;

#[allow(clippy::missing_const_for_fn)] // FIXME: Drop is unsupported.
impl Value {
    /// Tries to convert the value into a boolean.
    pub fn into_bool(self) -> Result<bool> {
        match self {
            Value::Bool(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into an unsigned integer.
    pub fn into_uint(self) -> Result<u64> {
        match self {
            Value::UInt(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into an integer.
    pub fn into_int(self) -> Result<i64> {
        match self {
            Value::Int(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a string.
    pub fn into_string(self) -> Result<String> {
        match self {
            Value::String(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into an optional string.
    pub fn into_optional_string(self) -> Result<Option<String>> {
        match self {
            Value::OptionalString(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into an accuracy.
    pub fn into_accuracy(self) -> Result<Accuracy> {
        match self {
            Value::Accuracy(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a digits format.
    pub fn into_digits_format(self) -> Result<DigitsFormat> {
        match self {
            Value::DigitsFormat(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into an optional timing method.
    pub fn into_optional_timing_method(self) -> Result<Option<TimingMethod>> {
        match self {
            Value::OptionalTimingMethod(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a color.
    pub fn into_color(self) -> Result<Color> {
        match self {
            Value::Color(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into an optional color.
    pub fn into_optional_color(self) -> Result<Option<Color>> {
        match self {
            Value::OptionalColor(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a gradient.
    pub fn into_gradient(self) -> Result<Gradient> {
        match self {
            Value::Color(v) => Ok(Gradient::Plain(v)),
            Value::Gradient(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a list gradient.
    pub fn into_list_gradient(self) -> Result<ListGradient> {
        match self {
            Value::Color(v) => Ok(ListGradient::Same(Gradient::Plain(v))),
            Value::Gradient(v) => Ok(ListGradient::Same(v)),
            Value::ListGradient(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into an alignment.
    pub fn into_alignment(self) -> Result<Alignment> {
        match self {
            Value::Alignment(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a value describing what a splits
    /// component's column starts out with.
    pub fn into_column_start_with(self) -> Result<ColumnStartWith> {
        match self {
            Value::ColumnStartWith(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a value describing what a splits
    /// component's column gets updated with.
    pub fn into_column_update_with(self) -> Result<ColumnUpdateWith> {
        match self {
            Value::ColumnUpdateWith(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a Column Update Trigger.
    pub fn into_column_update_trigger(self) -> Result<ColumnUpdateTrigger> {
        match self {
            Value::ColumnUpdateTrigger(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a hotkey.
    pub fn into_hotkey(self) -> Result<Option<Hotkey>> {
        match self {
            Value::Hotkey(v) => Ok(v),
            Value::String(v) | Value::OptionalString(Some(v)) => {
                v.parse().map_err(|_| Error::WrongType).map(Some)
            }
            Value::OptionalString(None) => Ok(None),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a layout direction.
    pub fn into_layout_direction(self) -> Result<LayoutDirection> {
        match self {
            Value::LayoutDirection(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a font.
    pub fn into_font(self) -> Result<Option<Font>> {
        match self {
            Value::Font(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a delta gradient.
    pub fn into_delta_gradient(self) -> Result<DeltaGradient> {
        match self {
            Value::Color(v) => Ok(Gradient::Plain(v).into()),
            Value::Gradient(v) => Ok(v.into()),
            Value::DeltaGradient(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }

    /// Tries to convert the value into a column kind.
    pub fn into_column_kind(self) -> Result<ColumnKind> {
        match self {
            Value::ColumnKind(v) => Ok(v),
            _ => Err(Error::WrongType),
        }
    }
}

impl From<Value> for bool {
    fn from(value: Value) -> Self {
        value.into_bool().unwrap()
    }
}

impl From<Value> for u64 {
    fn from(value: Value) -> Self {
        value.into_uint().unwrap()
    }
}

impl From<Value> for i64 {
    fn from(value: Value) -> Self {
        value.into_int().unwrap()
    }
}

impl From<Value> for String {
    fn from(value: Value) -> Self {
        value.into_string().unwrap()
    }
}

impl From<Value> for Option<String> {
    fn from(value: Value) -> Self {
        value.into_optional_string().unwrap()
    }
}

impl From<Value> for Accuracy {
    fn from(value: Value) -> Self {
        value.into_accuracy().unwrap()
    }
}

impl From<Value> for DigitsFormat {
    fn from(value: Value) -> Self {
        value.into_digits_format().unwrap()
    }
}

impl From<Value> for Option<TimingMethod> {
    fn from(value: Value) -> Self {
        value.into_optional_timing_method().unwrap()
    }
}

impl From<Value> for Color {
    fn from(value: Value) -> Self {
        value.into_color().unwrap()
    }
}

impl From<Value> for Option<Color> {
    fn from(value: Value) -> Self {
        value.into_optional_color().unwrap()
    }
}

impl From<Value> for Gradient {
    fn from(value: Value) -> Self {
        value.into_gradient().unwrap()
    }
}

impl From<Value> for ListGradient {
    fn from(value: Value) -> Self {
        value.into_list_gradient().unwrap()
    }
}

impl From<Value> for Alignment {
    fn from(value: Value) -> Self {
        value.into_alignment().unwrap()
    }
}

impl From<Value> for ColumnStartWith {
    fn from(value: Value) -> Self {
        value.into_column_start_with().unwrap()
    }
}

impl From<Value> for ColumnUpdateWith {
    fn from(value: Value) -> Self {
        value.into_column_update_with().unwrap()
    }
}

impl From<Value> for ColumnUpdateTrigger {
    fn from(value: Value) -> Self {
        value.into_column_update_trigger().unwrap()
    }
}

impl From<Value> for Option<Hotkey> {
    fn from(value: Value) -> Self {
        value.into_hotkey().unwrap()
    }
}

impl From<Value> for LayoutDirection {
    fn from(value: Value) -> Self {
        value.into_layout_direction().unwrap()
    }
}

impl From<Value> for Option<Font> {
    fn from(value: Value) -> Self {
        value.into_font().unwrap()
    }
}

impl From<Value> for DeltaGradient {
    fn from(value: Value) -> Self {
        value.into_delta_gradient().unwrap()
    }
}

impl From<Value> for ColumnKind {
    fn from(value: Value) -> Self {
        value.into_column_kind().unwrap()
    }
}