-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathprotocol.rs
410 lines (335 loc) · 15.9 KB
/
protocol.rs
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
use std::path::PathBuf;
use crate::renderer_client::RendererClient;
use crate::renderer_server::{TurtleId, ExportError};
use crate::radians::Radians;
use crate::{Distance, Point, Color, Speed, Event, Size, async_turtle::AngleUnit, debug};
use super::{
ConnectionError,
ClientRequest,
ServerResponse,
ExportFormat,
DrawingProp,
DrawingPropValue,
TurtleProp,
TurtlePropValue,
PenProp,
PenPropValue,
RotationDirection,
};
/// A wrapper for `RendererClient` that encodes the the IPC protocol in a type-safe manner
pub struct ProtocolClient {
client: RendererClient,
}
impl From<RendererClient> for ProtocolClient {
fn from(client: RendererClient) -> Self {
Self {client}
}
}
impl ProtocolClient {
/// Spawns a new server process and creates a connection to it
pub async fn new() -> Result<Self, ConnectionError> {
let client = RendererClient::new().await?;
Ok(client.into())
}
/// Creates a new renderer client that can also communicate to the same server
pub async fn split(&self) -> Self {
self.client.split().await.into()
}
pub async fn create_turtle(&self) -> TurtleId {
self.client.send(ClientRequest::CreateTurtle);
let response = self.client.recv().await;
match response {
ServerResponse::NewTurtle(id) => id,
_ => unreachable!("bug: expected to receive `NewTurtle` in response to `CreateTurtle` request"),
}
}
pub async fn export_svg(&self, path: PathBuf) -> Result<(), ExportError> {
self.client.send(ClientRequest::Export(path, ExportFormat::Svg));
let response = self.client.recv().await;
match response {
ServerResponse::ExportComplete(res) => res,
_ => unreachable!("bug: expected to receive `ExportComplete` in response to `Export` request"),
}
}
pub async fn poll_event(&self) -> Option<Event> {
self.client.send(ClientRequest::PollEvent);
let response = self.client.recv().await;
match response {
ServerResponse::Event(event) => event,
_ => unreachable!("bug: expected to receive `Event` in response to `NextEvent` request"),
}
}
pub async fn drawing_title(&self) -> String {
self.client.send(ClientRequest::DrawingProp(DrawingProp::Title));
let response = self.client.recv().await;
match response {
ServerResponse::DrawingProp(DrawingPropValue::Title(value)) => value,
_ => unreachable!("bug: expected to receive `DrawingProp` in response to `DrawingProp` request"),
}
}
pub async fn drawing_background(&self) -> Color {
self.client.send(ClientRequest::DrawingProp(DrawingProp::Background));
let response = self.client.recv().await;
match response {
ServerResponse::DrawingProp(DrawingPropValue::Background(value)) => value,
_ => unreachable!("bug: expected to receive `DrawingProp` in response to `DrawingProp` request"),
}
}
pub async fn drawing_center(&self) -> Point {
self.client.send(ClientRequest::DrawingProp(DrawingProp::Center));
let response = self.client.recv().await;
match response {
ServerResponse::DrawingProp(DrawingPropValue::Center(value)) => value,
_ => unreachable!("bug: expected to receive `DrawingProp` in response to `DrawingProp` request"),
}
}
pub async fn drawing_size(&self) -> Size {
self.client.send(ClientRequest::DrawingProp(DrawingProp::Size));
let response = self.client.recv().await;
match response {
ServerResponse::DrawingProp(DrawingPropValue::Size(value)) => value,
_ => unreachable!("bug: expected to receive `DrawingProp` in response to `DrawingProp` request"),
}
}
pub async fn drawing_is_maximized(&self) -> bool {
self.client.send(ClientRequest::DrawingProp(DrawingProp::IsMaximized));
let response = self.client.recv().await;
match response {
ServerResponse::DrawingProp(DrawingPropValue::IsMaximized(value)) => value,
_ => unreachable!("bug: expected to receive `DrawingProp` in response to `DrawingProp` request"),
}
}
pub async fn drawing_is_fullscreen(&self) -> bool {
self.client.send(ClientRequest::DrawingProp(DrawingProp::IsFullscreen));
let response = self.client.recv().await;
match response {
ServerResponse::DrawingProp(DrawingPropValue::IsFullscreen(value)) => value,
_ => unreachable!("bug: expected to receive `DrawingProp` in response to `DrawingProp` request"),
}
}
pub fn drawing_set_title(&self, value: String) {
self.client.send(ClientRequest::SetDrawingProp(DrawingPropValue::Title(value)))
}
pub fn drawing_set_background(&self, value: Color) {
debug_assert!(value.is_valid(), "bug: colors should be validated before sending to renderer server");
self.client.send(ClientRequest::SetDrawingProp(DrawingPropValue::Background(value)))
}
pub fn drawing_set_center(&self, value: Point) {
debug_assert!(value.is_finite(), "bug: center should be validated before sending to renderer server");
self.client.send(ClientRequest::SetDrawingProp(DrawingPropValue::Center(value)))
}
pub fn drawing_set_size(&self, value: Size) {
debug_assert!(value.width > 0 && value.height > 0, "bug: size should be validated before sending to renderer server");
self.client.send(ClientRequest::SetDrawingProp(DrawingPropValue::Size(value)))
}
pub fn drawing_set_is_maximized(&self, value: bool) {
self.client.send(ClientRequest::SetDrawingProp(DrawingPropValue::IsMaximized(value)))
}
pub fn drawing_set_is_fullscreen(&self, value: bool) {
self.client.send(ClientRequest::SetDrawingProp(DrawingPropValue::IsFullscreen(value)))
}
pub fn drawing_reset_center(&self) {
self.client.send(ClientRequest::ResetDrawingProp(DrawingProp::Center))
}
pub fn drawing_reset_size(&self) {
self.client.send(ClientRequest::ResetDrawingProp(DrawingProp::Size))
}
pub async fn turtle_pen_is_enabled(&self, id: TurtleId) -> bool {
self.client.send(ClientRequest::TurtleProp(id, TurtleProp::Pen(PenProp::IsEnabled)));
let response = self.client.recv().await;
match response {
ServerResponse::TurtleProp(recv_id, TurtlePropValue::Pen(PenPropValue::IsEnabled(value))) => {
debug_assert_eq!(id, recv_id, "bug: received data for incorrect turtle");
value
},
_ => unreachable!("bug: expected to receive `TurtleProp` in response to `TurtleProp` request"),
}
}
pub async fn turtle_pen_thickness(&self, id: TurtleId) -> f64 {
self.client.send(ClientRequest::TurtleProp(id, TurtleProp::Pen(PenProp::Thickness)));
let response = self.client.recv().await;
match response {
ServerResponse::TurtleProp(recv_id, TurtlePropValue::Pen(PenPropValue::Thickness(value))) => {
debug_assert_eq!(id, recv_id, "bug: received data for incorrect turtle");
value
},
_ => unreachable!("bug: expected to receive `TurtleProp` in response to `TurtleProp` request"),
}
}
pub async fn turtle_pen_color(&self, id: TurtleId) -> Color {
self.client.send(ClientRequest::TurtleProp(id, TurtleProp::Pen(PenProp::Color)));
let response = self.client.recv().await;
match response {
ServerResponse::TurtleProp(recv_id, TurtlePropValue::Pen(PenPropValue::Color(value))) => {
debug_assert_eq!(id, recv_id, "bug: received data for incorrect turtle");
value
},
_ => unreachable!("bug: expected to receive `TurtleProp` in response to `TurtleProp` request"),
}
}
pub async fn turtle_fill_color(&self, id: TurtleId) -> Color {
self.client.send(ClientRequest::TurtleProp(id, TurtleProp::FillColor));
let response = self.client.recv().await;
match response {
ServerResponse::TurtleProp(recv_id, TurtlePropValue::FillColor(value)) => {
debug_assert_eq!(id, recv_id, "bug: received data for incorrect turtle");
value
},
_ => unreachable!("bug: expected to receive `TurtleProp` in response to `TurtleProp` request"),
}
}
pub async fn turtle_is_filling(&self, id: TurtleId) -> bool {
self.client.send(ClientRequest::TurtleProp(id, TurtleProp::IsFilling));
let response = self.client.recv().await;
match response {
ServerResponse::TurtleProp(recv_id, TurtlePropValue::IsFilling(value)) => {
debug_assert_eq!(id, recv_id, "bug: received data for incorrect turtle");
value
},
_ => unreachable!("bug: expected to receive `TurtleProp` in response to `TurtleProp` request"),
}
}
pub async fn turtle_position(&self, id: TurtleId) -> Point {
self.client.send(ClientRequest::TurtleProp(id, TurtleProp::Position));
let response = self.client.recv().await;
match response {
ServerResponse::TurtleProp(recv_id, TurtlePropValue::Position(value)) => {
debug_assert_eq!(id, recv_id, "bug: received data for incorrect turtle");
value
},
_ => unreachable!("bug: expected to receive `TurtleProp` in response to `TurtleProp` request"),
}
}
pub async fn turtle_heading(&self, id: TurtleId) -> Radians {
self.client.send(ClientRequest::TurtleProp(id, TurtleProp::Heading));
let response = self.client.recv().await;
match response {
ServerResponse::TurtleProp(recv_id, TurtlePropValue::Heading(value)) => {
debug_assert_eq!(id, recv_id, "bug: received data for incorrect turtle");
value
},
_ => unreachable!("bug: expected to receive `TurtleProp` in response to `TurtleProp` request"),
}
}
pub async fn turtle_speed(&self, id: TurtleId) -> Speed {
self.client.send(ClientRequest::TurtleProp(id, TurtleProp::Speed));
let response = self.client.recv().await;
match response {
ServerResponse::TurtleProp(recv_id, TurtlePropValue::Speed(value)) => {
debug_assert_eq!(id, recv_id, "bug: received data for incorrect turtle");
value
},
_ => unreachable!("bug: expected to receive `TurtleProp` in response to `TurtleProp` request"),
}
}
pub async fn turtle_is_visible(&self, id: TurtleId) -> bool {
self.client.send(ClientRequest::TurtleProp(id, TurtleProp::IsVisible));
let response = self.client.recv().await;
match response {
ServerResponse::TurtleProp(recv_id, TurtlePropValue::IsVisible(value)) => {
debug_assert_eq!(id, recv_id, "bug: received data for incorrect turtle");
value
},
_ => unreachable!("bug: expected to receive `TurtleProp` in response to `TurtleProp` request"),
}
}
pub fn turtle_pen_set_is_enabled(&self, id: TurtleId, value: bool) {
self.client.send(ClientRequest::SetTurtleProp(id, TurtlePropValue::Pen(PenPropValue::IsEnabled(value))))
}
pub fn turtle_pen_set_thickness(&self, id: TurtleId, value: f64) {
debug_assert!(value >= 0.0 && value.is_finite(), "bug: pen size should be validated before sending to renderer server");
self.client.send(ClientRequest::SetTurtleProp(id, TurtlePropValue::Pen(PenPropValue::Thickness(value))))
}
pub fn turtle_pen_set_color(&self, id: TurtleId, value: Color) {
debug_assert!(value.is_valid(), "bug: colors should be validated before sending to renderer server");
self.client.send(ClientRequest::SetTurtleProp(id, TurtlePropValue::Pen(PenPropValue::Color(value))))
}
pub fn turtle_set_fill_color(&self, id: TurtleId, value: Color) {
debug_assert!(value.is_valid(), "bug: colors should be validated before sending to renderer server");
self.client.send(ClientRequest::SetTurtleProp(id, TurtlePropValue::FillColor(value)))
}
pub fn turtle_set_speed(&self, id: TurtleId, value: Speed) {
self.client.send(ClientRequest::SetTurtleProp(id, TurtlePropValue::Speed(value)))
}
pub fn turtle_set_is_visible(&self, id: TurtleId, value: bool) {
self.client.send(ClientRequest::SetTurtleProp(id, TurtlePropValue::IsVisible(value)))
}
pub fn turtle_reset_heading(&self, id: TurtleId) {
self.client.send(ClientRequest::ResetTurtleProp(id, TurtleProp::Heading))
}
pub fn reset_turtle(&self, id: TurtleId) {
self.client.send(ClientRequest::ResetTurtle(id))
}
pub async fn move_forward(&self, id: TurtleId, distance: Distance) {
if !distance.is_normal() {
return;
}
self.client.send(ClientRequest::MoveForward(id, distance));
let response = self.client.recv().await;
match response {
ServerResponse::AnimationComplete(recv_id) => {
debug_assert_eq!(id, recv_id, "bug: notified of complete animation for incorrect turtle");
},
_ => unreachable!("bug: expected to receive `AnimationComplete` in response to `MoveForward` request"),
}
}
pub async fn move_to(&self, id: TurtleId, target: Point) {
if !target.is_finite() {
return;
}
self.client.send(ClientRequest::MoveTo(id, target));
let response = self.client.recv().await;
match response {
ServerResponse::AnimationComplete(recv_id) => {
debug_assert_eq!(id, recv_id, "bug: notified of complete animation for incorrect turtle");
},
_ => unreachable!("bug: expected to receive `AnimationComplete` in response to `MoveTo` request"),
}
}
pub async fn rotate_in_place(&self, id: TurtleId, angle: Radians, direction: RotationDirection) {
if !angle.is_normal() {
return;
}
self.client.send(ClientRequest::RotateInPlace(id, angle, direction));
let response = self.client.recv().await;
match response {
ServerResponse::AnimationComplete(recv_id) => {
debug_assert_eq!(id, recv_id, "bug: notified of complete animation for incorrect turtle");
},
_ => unreachable!("bug: expected to receive `AnimationComplete` in response to `RotateInPlace` request"),
}
}
pub fn begin_fill(&self, id: TurtleId) {
self.client.send(ClientRequest::BeginFill(id))
}
pub fn end_fill(&self, id: TurtleId) {
self.client.send(ClientRequest::EndFill(id))
}
pub fn clear_all(&self) {
self.client.send(ClientRequest::ClearAll)
}
pub fn clear_turtle(&self, id: TurtleId) {
self.client.send(ClientRequest::ClearTurtle(id))
}
pub async fn debug_turtle(&self, id: TurtleId, angle_unit: AngleUnit) -> debug::Turtle {
self.client.send(ClientRequest::DebugTurtle(id, angle_unit));
let response = self.client.recv().await;
match response {
ServerResponse::DebugTurtle(recv_id, state) => {
debug_assert_eq!(id, recv_id, "bug: received debug turtle for incorrect turtle");
state
},
_ => unreachable!("bug: expected to receive `DebugTurtle` in response to `DebugTurtle` request"),
}
}
pub async fn debug_drawing(&self) -> debug::Drawing {
self.client.send(ClientRequest::DebugDrawing);
let response = self.client.recv().await;
match response {
ServerResponse::DebugDrawing(state) => {
state
},
_ => unreachable!("bug: expected to receive `DebugDrawing` in response to `DebugDrawing` request"),
}
}
}