buttplug 0.0.2

Buttplug Intimate Hardware Control Library
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
// Buttplug Rust Source Code File - See https://buttplug.io for more info.
//
// Copyright 2016-2019 Nonpolynomial Labs LLC. All rights reserved.
//
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
// for full license information.

//! Structs representing low level [Buttplug
//! Protocol](https://buttplug-spec.docs.buttplug.io) messages

use super::errors::*;
#[cfg(feature = "serialize_json")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "serialize_json")]
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::collections::HashMap;

/// Base trait for all Buttplug Protocol Message Structs. Handles management of
/// message ids, as well as implementing conveinence functions for converting
/// between message structs and [ButtplugMessageUnion] enums, serialization, etc...
pub trait ButtplugMessage: Send + Sync + Clone {
    /// Returns the id number of the message
    fn get_id(&self) -> u32;
    /// Sets the id number of the message
    fn set_id(&mut self, id: u32);
    /// Returns the message as a [ButtplugMessageUnion] enum.
    fn as_union(self) -> ButtplugMessageUnion;
    /// Returns the message as a string in Buttplug JSON Protocol format.
    #[cfg(feature = "serialize_json")]
    fn as_protocol_json(self) -> String
    where
        Self: ButtplugMessage + Serialize + Deserialize<'static>,
    {
        "[".to_owned() + &serde_json::to_string(&self).unwrap() + "]"
    }
}

/// Represents the Buttplug Protocol Ok message, as documented in the [Buttplug
/// Protocol Spec](https://buttplug-spec.docs.buttplug.io/status.html#ok).
#[derive(Debug, PartialEq, Default, ButtplugMessage, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct Ok {
    /// Message Id, used for matching message pairs in remote connection instances.
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
}

impl Ok {
    /// Creates a new Ok message with the given Id.
    pub fn new(id: u32) -> Self {
        Self { id }
    }
}

/// Error codes pertaining to error classes that can be represented in the
/// Buttplug [Error] message.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize_repr, Deserialize_repr))]
#[repr(u8)]
pub enum ErrorCode {
    ErrorUnknown = 0,
    ErrorHandshake,
    ErrorPing,
    ErrorMessage,
    ErrorDevice,
}

/// Represents the Buttplug Protocol Error message, as documented in the [Buttplug
/// Protocol Spec](https://buttplug-spec.docs.buttplug.io/status.html#error).
#[derive(Debug, ButtplugMessage, Clone, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct Error {
    /// Message Id, used for matching message pairs in remote connection instances.
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
    /// Specifies the class of the error.
    #[cfg_attr(feature = "serialize_json", serde(rename = "ErrorCode"))]
    pub error_code: ErrorCode,
    /// Description of the error.
    #[cfg_attr(feature = "serialize_json", serde(rename = "ErrorMessage"))]
    pub error_message: String,
}

impl Error {
    /// Creates a new error object.
    pub fn new(error_code: ErrorCode, error_message: &str) -> Self {
        Self {
            id: 0,
            error_code,
            error_message: error_message.to_string(),
        }
    }
}

impl From<ButtplugError> for Error {
    /// Converts a [super::errors::ButtplugError] object into a Buttplug Protocol
    /// [Error] message.
    fn from(error: ButtplugError) -> Self {
        let code = match error {
            ButtplugError::ButtplugDeviceError(_) => ErrorCode::ErrorDevice,
            ButtplugError::ButtplugMessageError(_) => ErrorCode::ErrorMessage,
            ButtplugError::ButtplugPingError(_) => ErrorCode::ErrorPing,
            ButtplugError::ButtplugHandshakeError(_) => ErrorCode::ErrorHandshake,
            ButtplugError::ButtplugUnknownError(_) => ErrorCode::ErrorUnknown,
        };
        // Gross but was having problems with naming collisions on the error trait
        let msg = match error {
            ButtplugError::ButtplugDeviceError(_s) => _s.message,
            ButtplugError::ButtplugMessageError(_s) => _s.message,
            ButtplugError::ButtplugPingError(_s) => _s.message,
            ButtplugError::ButtplugHandshakeError(_s) => _s.message,
            ButtplugError::ButtplugUnknownError(_s) => _s.message,
        };
        Error::new(code, &msg)
    }
}

#[derive(Debug, ButtplugMessage, Clone, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct Ping {
    /// Message Id, used for matching message pairs in remote connection instances.
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
}

impl Default for Ping {
    /// Creates a new Ping message with the given Id.
    fn default() -> Self {
        Self { id: 1 }
    }
}

#[derive(Debug, Default, ButtplugMessage, Clone, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct Test {
    /// Message Id, used for matching message pairs in remote connection instances.
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
    /// Test string, which will be echo'd back to client when sent to server.
    #[cfg_attr(feature = "serialize_json", serde(rename = "TestString"))]
    test_string: String,
}

impl Test {
    /// Creates a new Ping message with the given Id.
    pub fn new(test: &str) -> Self {
        Self {
            id: 1,
            test_string: test.to_owned(),
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct MessageAttributes {
    #[cfg_attr(feature = "serialize_json", serde(rename = "FeatureCount"))]
    pub feature_count: Option<u32>,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct DeviceMessageInfo {
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceName"))]
    pub device_name: String,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceMessages"))]
    pub device_messages: HashMap<String, MessageAttributes>,
}

impl From<&DeviceAdded> for DeviceMessageInfo {
    fn from(device_added: &DeviceAdded) -> Self {
        Self {
            device_index: device_added.device_index,
            device_name: device_added.device_name.clone(),
            device_messages: device_added.device_messages.clone(),
        }
    }
}

#[derive(Default, ButtplugMessage, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct DeviceList {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Devices"))]
    pub devices: Vec<DeviceMessageInfo>,
}

#[derive(Default, ButtplugMessage, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct DeviceAdded {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceName"))]
    pub device_name: String,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceMessages"))]
    pub device_messages: HashMap<String, MessageAttributes>,
}

#[derive(Debug, Default, ButtplugMessage, Clone, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct DeviceRemoved {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
}

#[derive(Debug, ButtplugMessage, Clone, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct StartScanning {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
}

impl Default for StartScanning {
    fn default() -> Self {
        Self { id: 1 }
    }
}

#[derive(Debug, ButtplugMessage, Clone, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct StopScanning {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
}

impl Default for StopScanning {
    fn default() -> Self {
        Self { id: 1 }
    }
}

#[derive(Debug, Default, ButtplugMessage, Clone, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct ScanningFinished {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
}

#[derive(Debug, ButtplugMessage, Clone, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct RequestDeviceList {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
}

impl Default for RequestDeviceList {
    fn default() -> Self {
        Self { id: 1 }
    }
}

#[derive(Debug, Default, ButtplugMessage, Clone, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct RequestServerInfo {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "ClientName"))]
    pub client_name: String,
    #[cfg_attr(feature = "serialize_json", serde(rename = "MessageVersion"))]
    pub message_version: u32,
}

impl RequestServerInfo {
    pub fn new(client_name: &str, message_version: u32) -> Self {
        Self {
            id: 1,
            client_name: client_name.to_string(),
            message_version,
        }
    }
}

#[derive(Debug, Default, ButtplugMessage, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct ServerInfo {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "MajorVersion"))]
    pub major_version: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "MinorVersion"))]
    pub minor_version: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "BuildVersion"))]
    pub build_version: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "MessageVersion"))]
    pub message_version: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "MaxPingTime"))]
    pub max_ping_time: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "ServerName"))]
    pub server_name: String,
}

impl ServerInfo {
    pub fn new(server_name: &str, message_version: u32, max_ping_time: u32) -> Self {
        Self {
            id: 0,
            major_version: 0,
            minor_version: 0,
            build_version: 0,
            message_version,
            max_ping_time,
            server_name: server_name.to_string(),
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub enum LogLevel {
    Off = 0,
    Fatal,
    Error,
    Warn,
    Info,
    Debug,
    Trace,
}

#[derive(Debug, ButtplugMessage, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct RequestLog {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "LogLevel"))]
    pub log_level: LogLevel,
}

impl RequestLog {
    pub fn new(log_level: LogLevel) -> Self {
        Self { id: 1, log_level }
    }
}

#[derive(Debug, ButtplugMessage, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct Log {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "LogLevel"))]
    pub log_level: LogLevel,
    #[cfg_attr(feature = "serialize_json", serde(rename = "LogMessage"))]
    pub log_message: String,
}

impl Log {
    pub fn new(log_level: LogLevel, log_message: String) -> Self {
        Self {
            id: 0,
            log_level,
            log_message,
        }
    }
}

#[derive(Debug, Default, ButtplugMessage, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct StopDeviceCmd {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    pub id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
}

impl StopDeviceCmd {
    pub fn new(device_index: u32) -> Self {
        Self {
            id: 1,
            device_index,
        }
    }
}

#[derive(Debug, Default, ButtplugMessage, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct StopAllDevices {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    pub id: u32,
}

#[derive(Debug, Default, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct VibrateSubcommand {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Index"))]
    pub index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Speed"))]
    pub speed: f64,
}

impl VibrateSubcommand {
    pub fn new(index: u32, speed: f64) -> Self {
        Self { index, speed }
    }
}

#[derive(Debug, Default, ButtplugMessage, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct VibrateCmd {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    pub id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Speeds"))]
    pub speeds: Vec<VibrateSubcommand>,
}

impl VibrateCmd {
    pub fn new(device_index: u32, speeds: Vec<VibrateSubcommand>) -> Self {
        Self {
            id: 1,
            device_index,
            speeds,
        }
    }
}

#[derive(Debug, Default, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct VectorSubcommand {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Index"))]
    pub index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Duration"))]
    pub duration: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Position"))]
    pub position: f64,
}

impl VectorSubcommand {
    pub fn new(index: u32, duration: u32, position: f64) -> Self {
        Self {
            index,
            duration,
            position,
        }
    }
}

#[derive(Debug, Default, ButtplugMessage, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct LinearCmd {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    pub id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Vectors"))]
    pub vectors: Vec<VectorSubcommand>,
}

impl LinearCmd {
    pub fn new(device_index: u32, vectors: Vec<VectorSubcommand>) -> Self {
        Self {
            id: 1,
            device_index,
            vectors,
        }
    }
}

#[derive(Debug, Default, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct RotationSubcommand {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Index"))]
    pub index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Speed"))]
    pub speed: f64,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Clockwise"))]
    pub clockwise: bool,
}

impl RotationSubcommand {
    pub fn new(index: u32, speed: f64, clockwise: bool) -> Self {
        Self {
            index,
            speed,
            clockwise,
        }
    }
}

#[derive(Debug, Default, ButtplugMessage, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct RotateCmd {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    pub id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Rotations"))]
    pub rotations: Vec<RotationSubcommand>,
}

impl RotateCmd {
    pub fn new(device_index: u32, rotations: Vec<RotationSubcommand>) -> Self {
        Self {
            id: 1,
            device_index,
            rotations,
        }
    }
}

#[derive(Debug, Default, ButtplugMessage, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct FleshlightLaunchFW12Cmd {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    pub id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Position"))]
    pub position: u8,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Speed"))]
    pub speed: u8,
}

impl FleshlightLaunchFW12Cmd {
    pub fn new(device_index: u32, position: u8, speed: u8) -> Self {
        Self {
            id: 1,
            device_index,
            position,
            speed,
        }
    }
}

#[derive(Debug, ButtplugMessage, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct LovenseCmd {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    pub id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Command"))]
    pub command: String,
}

impl LovenseCmd {
    pub fn new(device_index: u32, command: &str) -> Self {
        Self {
            id: 1,
            device_index,
            command: command.to_owned(),
        }
    }
}

// Dear god this needs to be deprecated
#[derive(Debug, ButtplugMessage, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct KiirooCmd {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    pub id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Command"))]
    pub command: String,
}

impl KiirooCmd {
    pub fn new(device_index: u32, command: &str) -> Self {
        Self {
            id: 1,
            device_index,
            command: command.to_owned(),
        }
    }
}

#[derive(Debug, ButtplugMessage, Default, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct VorzeA10CycloneCmd {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    pub id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Speed"))]
    pub speed: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Clockwise"))]
    pub clockwise: bool,
}

impl VorzeA10CycloneCmd {
    pub fn new(device_index: u32, speed: u32, clockwise: bool) -> Self {
        Self {
            id: 1,
            device_index,
            speed,
            clockwise,
        }
    }
}

#[derive(Debug, ButtplugMessage, Default, PartialEq, Clone)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub struct SingleMotorVibrateCmd {
    #[cfg_attr(feature = "serialize_json", serde(rename = "Id"))]
    pub id: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "DeviceIndex"))]
    pub device_index: u32,
    #[cfg_attr(feature = "serialize_json", serde(rename = "Speed"))]
    pub speed: f64,
}

impl SingleMotorVibrateCmd {
    pub fn new(device_index: u32, speed: f64) -> Self {
        Self {
            id: 1,
            device_index,
            speed,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serialize_json", derive(Serialize, Deserialize))]
pub enum ButtplugMessageUnion {
    Ok(Ok),
    Error(Error),
    Ping(Ping),
    Test(Test),
    RequestLog(RequestLog),
    Log(Log),
    RequestServerInfo(RequestServerInfo),
    ServerInfo(ServerInfo),
    DeviceList(DeviceList),
    DeviceAdded(DeviceAdded),
    DeviceRemoved(DeviceRemoved),
    StartScanning(StartScanning),
    StopScanning(StopScanning),
    ScanningFinished(ScanningFinished),
    RequestDeviceList(RequestDeviceList),
    VibrateCmd(VibrateCmd),
    LinearCmd(LinearCmd),
    RotateCmd(RotateCmd),
    FleshlightLaunchFW12Cmd(FleshlightLaunchFW12Cmd),
    LovenseCmd(LovenseCmd),
    KiirooCmd(KiirooCmd),
    VorzeA10CycloneCmd(VorzeA10CycloneCmd),
    SingleMotorVibrateCmd(SingleMotorVibrateCmd),
    StopDeviceCmd(StopDeviceCmd),
    StopAllDevices(StopAllDevices),
}

impl ButtplugMessage for ButtplugMessageUnion {
    fn get_id(&self) -> u32 {
        match self {
            ButtplugMessageUnion::Ok(ref msg) => msg.id,
            ButtplugMessageUnion::Error(ref msg) => msg.id,
            ButtplugMessageUnion::Log(ref msg) => msg.id,
            ButtplugMessageUnion::RequestLog(ref msg) => msg.id,
            ButtplugMessageUnion::Ping(ref msg) => msg.id,
            ButtplugMessageUnion::Test(ref msg) => msg.id,
            ButtplugMessageUnion::RequestServerInfo(ref msg) => msg.id,
            ButtplugMessageUnion::ServerInfo(ref msg) => msg.id,
            ButtplugMessageUnion::DeviceList(ref msg) => msg.id,
            ButtplugMessageUnion::DeviceAdded(ref msg) => msg.id,
            ButtplugMessageUnion::DeviceRemoved(ref msg) => msg.id,
            ButtplugMessageUnion::StartScanning(ref msg) => msg.id,
            ButtplugMessageUnion::StopScanning(ref msg) => msg.id,
            ButtplugMessageUnion::ScanningFinished(ref msg) => msg.id,
            ButtplugMessageUnion::RequestDeviceList(ref msg) => msg.id,
            ButtplugMessageUnion::VibrateCmd(ref msg) => msg.id,
            ButtplugMessageUnion::LinearCmd(ref msg) => msg.id,
            ButtplugMessageUnion::RotateCmd(ref msg) => msg.id,
            ButtplugMessageUnion::FleshlightLaunchFW12Cmd(ref msg) => msg.id,
            ButtplugMessageUnion::LovenseCmd(ref msg) => msg.id,
            ButtplugMessageUnion::KiirooCmd(ref msg) => msg.id,
            ButtplugMessageUnion::VorzeA10CycloneCmd(ref msg) => msg.id,
            ButtplugMessageUnion::SingleMotorVibrateCmd(ref msg) => msg.id,
            ButtplugMessageUnion::StopDeviceCmd(ref msg) => msg.id,
            ButtplugMessageUnion::StopAllDevices(ref msg) => msg.id,
        }
    }

    fn set_id(&mut self, id: u32) {
        match self {
            ButtplugMessageUnion::Ok(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::Error(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::Log(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::RequestLog(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::Ping(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::Test(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::RequestServerInfo(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::ServerInfo(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::DeviceList(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::DeviceAdded(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::DeviceRemoved(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::StartScanning(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::StopScanning(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::ScanningFinished(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::RequestDeviceList(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::VibrateCmd(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::LinearCmd(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::RotateCmd(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::FleshlightLaunchFW12Cmd(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::LovenseCmd(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::KiirooCmd(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::VorzeA10CycloneCmd(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::SingleMotorVibrateCmd(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::StopDeviceCmd(ref mut msg) => msg.set_id(id),
            ButtplugMessageUnion::StopAllDevices(ref mut msg) => msg.set_id(id),
        }
    }

    fn as_union(self) -> ButtplugMessageUnion {
        panic!("as_union shouldn't be called on union.");
    }
}

#[cfg(feature = "serialize_json")]
#[cfg(test)]
mod test {
    use super::{ButtplugMessageUnion, Error, ErrorCode, Ok};

    const OK_STR: &str = "{\"Ok\":{\"Id\":0}}";
    const ERROR_STR: &str =
        "{\"Error\":{\"Id\":0,\"ErrorCode\":1,\"ErrorMessage\":\"Test Error\"}}";

    #[test]
    fn test_ok_serialize() {
        let ok = ButtplugMessageUnion::Ok(Ok::new(0));
        let js = serde_json::to_string(&ok).unwrap();
        assert_eq!(OK_STR, js);
    }

    #[test]
    fn test_ok_deserialize() {
        let union: ButtplugMessageUnion = serde_json::from_str(&OK_STR).unwrap();
        assert_eq!(ButtplugMessageUnion::Ok(Ok::new(0)), union);
    }

    #[test]
    fn test_error_serialize() {
        let error =
            ButtplugMessageUnion::Error(Error::new(ErrorCode::ErrorHandshake, "Test Error"));
        let js = serde_json::to_string(&error).unwrap();
        assert_eq!(ERROR_STR, js);
    }

    #[test]
    fn test_error_deserialize() {
        let union: ButtplugMessageUnion = serde_json::from_str(&ERROR_STR).unwrap();
        assert_eq!(
            ButtplugMessageUnion::Error(Error::new(ErrorCode::ErrorHandshake, "Test Error")),
            union
        );
    }
}