Skip to content

Commit 3c3762b

Browse files
authored
Expose more of the Server Protocol (#827)
This exposes more of the Server Protocol publicly, so it can also be used as a client.
1 parent 8d5b0f6 commit 3c3762b

File tree

7 files changed

+269
-87
lines changed

7 files changed

+269
-87
lines changed

capi/src/command_sink.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! processing a command, changes to the timer are reported as events. Various
99
//! error conditions can occur if the command couldn't be processed.
1010
11-
use std::{future::Future, ops::Deref, pin::Pin, sync::Arc};
11+
use std::{borrow::Cow, future::Future, ops::Deref, pin::Pin, sync::Arc};
1212

1313
use livesplit_core::{
1414
event::{self, Result},
@@ -50,15 +50,15 @@ pub(crate) trait CommandSinkAndQuery: Send + Sync + 'static {
5050
fn dyn_undo_all_pauses(&self) -> Fut;
5151
fn dyn_switch_to_previous_comparison(&self) -> Fut;
5252
fn dyn_switch_to_next_comparison(&self) -> Fut;
53-
fn dyn_set_current_comparison(&self, comparison: &str) -> Fut;
53+
fn dyn_set_current_comparison(&self, comparison: Cow<'_, str>) -> Fut;
5454
fn dyn_toggle_timing_method(&self) -> Fut;
5555
fn dyn_set_current_timing_method(&self, method: TimingMethod) -> Fut;
5656
fn dyn_initialize_game_time(&self) -> Fut;
5757
fn dyn_set_game_time(&self, time: TimeSpan) -> Fut;
5858
fn dyn_pause_game_time(&self) -> Fut;
5959
fn dyn_resume_game_time(&self) -> Fut;
6060
fn dyn_set_loading_times(&self, time: TimeSpan) -> Fut;
61-
fn dyn_set_custom_variable(&self, name: &str, value: &str) -> Fut;
61+
fn dyn_set_custom_variable(&self, name: Cow<'_, str>, value: Cow<'_, str>) -> Fut;
6262
}
6363

6464
type Fut = Pin<Box<dyn Future<Output = Result> + 'static>>;
@@ -107,7 +107,7 @@ where
107107
fn dyn_switch_to_next_comparison(&self) -> Fut {
108108
Box::pin(self.switch_to_next_comparison())
109109
}
110-
fn dyn_set_current_comparison(&self, comparison: &str) -> Fut {
110+
fn dyn_set_current_comparison(&self, comparison: Cow<'_, str>) -> Fut {
111111
Box::pin(self.set_current_comparison(comparison))
112112
}
113113
fn dyn_toggle_timing_method(&self) -> Fut {
@@ -131,7 +131,7 @@ where
131131
fn dyn_set_loading_times(&self, time: TimeSpan) -> Fut {
132132
Box::pin(self.set_loading_times(time))
133133
}
134-
fn dyn_set_custom_variable(&self, name: &str, value: &str) -> Fut {
134+
fn dyn_set_custom_variable(&self, name: Cow<'_, str>, value: Cow<'_, str>) -> Fut {
135135
Box::pin(self.set_custom_variable(name, value))
136136
}
137137
}
@@ -185,7 +185,10 @@ impl event::CommandSink for CommandSink {
185185
self.0.dyn_switch_to_next_comparison()
186186
}
187187

188-
fn set_current_comparison(&self, comparison: &str) -> impl Future<Output = Result> + 'static {
188+
fn set_current_comparison(
189+
&self,
190+
comparison: Cow<'_, str>,
191+
) -> impl Future<Output = Result> + 'static {
189192
self.0.dyn_set_current_comparison(comparison)
190193
}
191194

@@ -222,8 +225,8 @@ impl event::CommandSink for CommandSink {
222225

223226
fn set_custom_variable(
224227
&self,
225-
name: &str,
226-
value: &str,
228+
name: Cow<'_, str>,
229+
value: Cow<'_, str>,
227230
) -> impl Future<Output = Result> + 'static {
228231
self.0.dyn_set_custom_variable(name, value)
229232
}

capi/src/web_command_sink.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! timer commands. All of them are optional except for `getTimer`.
44
55
use core::ptr;
6-
use std::{cell::Cell, convert::TryFrom, future::Future, sync::Arc};
6+
use std::{borrow::Cow, cell::Cell, convert::TryFrom, future::Future, sync::Arc};
77

88
use livesplit_core::{
99
event::{CommandSink, Error, Event, Result, TimerQuery},
@@ -221,12 +221,15 @@ impl CommandSink for WebCommandSink {
221221
)
222222
}
223223

224-
fn set_current_comparison(&self, comparison: &str) -> impl Future<Output = Result> + 'static {
224+
fn set_current_comparison(
225+
&self,
226+
comparison: Cow<'_, str>,
227+
) -> impl Future<Output = Result> + 'static {
225228
debug_assert!(!self.locked.get());
226229
handle_action_value(
227230
self.set_current_comparison
228231
.as_ref()
229-
.and_then(|f| f.call1(&self.obj, &JsValue::from_str(comparison)).ok()),
232+
.and_then(|f| f.call1(&self.obj, &JsValue::from_str(&comparison)).ok()),
230233
)
231234
}
232235

@@ -301,15 +304,15 @@ impl CommandSink for WebCommandSink {
301304

302305
fn set_custom_variable(
303306
&self,
304-
name: &str,
305-
value: &str,
307+
name: Cow<'_, str>,
308+
value: Cow<'_, str>,
306309
) -> impl Future<Output = Result> + 'static {
307310
debug_assert!(!self.locked.get());
308311
handle_action_value(self.set_custom_variable.as_ref().and_then(|f| {
309312
f.call2(
310313
&self.obj,
311-
&JsValue::from_str(name),
312-
&JsValue::from_str(value),
314+
&JsValue::from_str(&name),
315+
&JsValue::from_str(&value),
313316
)
314317
.ok()
315318
}))

src/auto_splitting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ impl<E: event::CommandSink + TimerQuery + Send> AutoSplitTimer for Timer<E> {
761761
}
762762

763763
fn set_variable(&mut self, name: &str, value: &str) {
764-
drop(self.0.set_custom_variable(name, value));
764+
drop(self.0.set_custom_variable(name.into(), value.into()));
765765
}
766766

767767
fn log(&mut self, message: fmt::Arguments<'_>) {

src/component/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! the chosen comparison throughout the whole attempt. Every point of the graph
44
//! represents a split. Its x-coordinate is proportional to the split time and
55
//! its y-coordinate is proportional to the split delta. The entire diagram is
6-
//! refered to as the chart and it contains the graph. The x-axis is the
6+
//! referred to as the chart and it contains the graph. The x-axis is the
77
//! horizontal line that separates positive deltas from negative ones.
88
99
// The words "padding" and "content" are from the CSS box model. "Padding" is an

src/event.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
use core::{future::Future, ops::Deref};
1313

14-
use alloc::sync::Arc;
14+
use alloc::{borrow::Cow, sync::Arc};
1515

1616
use crate::{TimeSpan, Timer, TimingMethod};
1717

@@ -59,13 +59,14 @@ pub enum Event {
5959
LoadingTimesSet = 16,
6060
/// A custom variable has been set.
6161
CustomVariableSet = 17,
62+
/// An unknown event occurred.
63+
#[serde(other)]
64+
Unknown,
6265
}
6366

64-
impl TryFrom<u32> for Event {
65-
type Error = ();
66-
67-
fn try_from(value: u32) -> Result<Self, Self::Error> {
68-
Ok(match value {
67+
impl From<u32> for Event {
68+
fn from(value: u32) -> Self {
69+
match value {
6970
0 => Event::Started,
7071
1 => Event::Splitted,
7172
2 => Event::Finished,
@@ -84,8 +85,8 @@ impl TryFrom<u32> for Event {
8485
15 => Event::GameTimeResumed,
8586
16 => Event::LoadingTimesSet,
8687
17 => Event::CustomVariableSet,
87-
_ => return Err(()),
88-
})
88+
_ => Event::Unknown,
89+
}
8990
}
9091
}
9192

@@ -238,7 +239,10 @@ pub trait CommandSink {
238239
fn switch_to_next_comparison(&self) -> impl Future<Output = Result> + 'static;
239240
/// Tries to set the current comparison to the comparison specified. If the
240241
/// comparison doesn't exist an error is returned.
241-
fn set_current_comparison(&self, comparison: &str) -> impl Future<Output = Result> + 'static;
242+
fn set_current_comparison(
243+
&self,
244+
comparison: Cow<'_, str>,
245+
) -> impl Future<Output = Result> + 'static;
242246
/// Toggles between the `Real Time` and `Game Time` timing methods.
243247
fn toggle_timing_method(&self) -> impl Future<Output = Result> + 'static;
244248
/// Sets the current timing method to the timing method provided.
@@ -269,8 +273,8 @@ pub trait CommandSink {
269273
/// be stored in the splits file.
270274
fn set_custom_variable(
271275
&self,
272-
name: &str,
273-
value: &str,
276+
name: Cow<'_, str>,
277+
value: Cow<'_, str>,
274278
) -> impl Future<Output = Result> + 'static;
275279
}
276280

@@ -347,7 +351,10 @@ impl CommandSink for crate::SharedTimer {
347351
async { Ok(Event::ComparisonChanged) }
348352
}
349353

350-
fn set_current_comparison(&self, comparison: &str) -> impl Future<Output = Result> + 'static {
354+
fn set_current_comparison(
355+
&self,
356+
comparison: Cow<'_, str>,
357+
) -> impl Future<Output = Result> + 'static {
351358
let result = self.write().unwrap().set_current_comparison(comparison);
352359
async move { result }
353360
}
@@ -392,8 +399,8 @@ impl CommandSink for crate::SharedTimer {
392399

393400
fn set_custom_variable(
394401
&self,
395-
name: &str,
396-
value: &str,
402+
name: Cow<'_, str>,
403+
value: Cow<'_, str>,
397404
) -> impl Future<Output = Result> + 'static {
398405
self.write().unwrap().set_custom_variable(name, value);
399406
async { Ok(Event::CustomVariableSet) }
@@ -457,7 +464,10 @@ impl<T: CommandSink + ?Sized> CommandSink for Arc<T> {
457464
CommandSink::switch_to_next_comparison(&**self)
458465
}
459466

460-
fn set_current_comparison(&self, comparison: &str) -> impl Future<Output = Result> + 'static {
467+
fn set_current_comparison(
468+
&self,
469+
comparison: Cow<'_, str>,
470+
) -> impl Future<Output = Result> + 'static {
461471
CommandSink::set_current_comparison(&**self, comparison)
462472
}
463473

@@ -494,8 +504,8 @@ impl<T: CommandSink + ?Sized> CommandSink for Arc<T> {
494504

495505
fn set_custom_variable(
496506
&self,
497-
name: &str,
498-
value: &str,
507+
name: Cow<'_, str>,
508+
value: Cow<'_, str>,
499509
) -> impl Future<Output = Result> + 'static {
500510
CommandSink::set_custom_variable(&**self, name, value)
501511
}

0 commit comments

Comments
 (0)