libjaka-rs 0.1.0

Rust bindings for the Jaka robot
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
use std::marker::ConstParamTy;

use serde::{Deserialize, Serialize, de::DeserializeOwned};
use serde_json::Value;
use serde_with::serde_as;

pub trait CommandSerde: Sized {
    fn serialize(&self) -> String;
    fn deserialize(data: &str) -> Option<Self>;
}

#[derive(ConstParamTy, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub enum Command {
    PowerOn,
    PowerOff,
    EnableRobot,
    JointMove,
    EndMove,
    Shutdown,
    Quit,
    GetRobotState,
    DisableRobot,
    TorqueControlEnable,
    TorqueFeedforward,
    ServoMove,
    ServoJ,
    ServoP,
    GetData,
    RapidRate,
    LoadProgram,
    GetLoadedProgram,
    PlayProgram,
    PauseProgram,
    ResumeProgram,
    StopProgram,
    GetProgramState,
    SetDigitalOutput,
    GetDigitalInputStatus,
    SetAnalogOutput,
    SetToolOffsets,
    SetToolId,
    SetUserOffsets,
    SetUserId,
    GetExtioStatus,
    GetFuncdiStatus,
    DragStatus,
    QueryUserDefinedVariable,
    ModifyUserDefinedVariable,
    ProtectiveStopStatus,
    Jog,
    MoveL,
    WaitComplete, //DEPRECATED
    SetPayload,
    GetPayload,
    SetClsnSensitivity,
    GetClsnSensitivity,
    KineForward,
    KineInverse,
    ClearError,
    GetJointPos,
    GetTcpPos,
}
pub struct Request<const C: Command, D> {
    pub data: D,
}

pub struct Response<const C: Command, S> {
    pub state: S,
}

pub enum ErrorCode {
    Success,
    Exception,
    Error,
}

#[derive(Serialize, Deserialize)]
pub struct DefaultState {
    pub error_code: String,
    pub error_msg: String,
}

// power on
pub type PowerOnRequest = Request<{ Command::PowerOn }, ()>;
pub type PowerOnResponse = Response<{ Command::PowerOn }, PowerOnState>;
pub type PowerOnState = DefaultState;

// power off
pub type PowerOffRequest = Request<{ Command::PowerOff }, ()>;
pub type PowerOffResponse = Response<{ Command::PowerOff }, PowerOffState>;
pub type PowerOffState = DefaultState;

// enable robot
pub type EnableRobotRequest = Request<{ Command::EnableRobot }, ()>;
pub type EnableRobotResponse = Response<{ Command::EnableRobot }, EnableRobotState>;
pub type EnableRobotState = DefaultState;

// joint move
pub type JointMoveRequest = Request<{ Command::JointMove }, JointMoveData>;
pub type JointMoveResponse = Response<{ Command::JointMove }, JointMoveState>;
pub type JointMoveState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct JointMoveData {
    pub joint_angles: [f64; 6],
    pub speed: f64,
    pub accel: f64,
    pub relflag: u8,
}

// end move
pub type EndMoveRequest = Request<{ Command::EndMove }, EndMoveData>;
pub type EndMoveResponse = Response<{ Command::EndMove }, EndMoveState>;
pub type EndMoveState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct EndMoveData {
    pub end_position: [f64; 6],
    pub speed: f64,
    pub accel: f64,
}

// shutdown
pub type ShutdownRequest = Request<{ Command::Shutdown }, ()>;
pub type ShutdownResponse = Response<{ Command::Shutdown }, ShutdownState>;
pub type ShutdownState = DefaultState;

// quit
pub type QuitRequest = Request<{ Command::Quit }, ()>;
pub type QuitResponse = Response<{ Command::Quit }, QuitState>;
pub type QuitState = DefaultState;

// get robot state
pub type GetRobotStateRequest = Request<{ Command::GetRobotState }, ()>;
pub type GetRobotStateResponse = Response<{ Command::GetRobotState }, GetRobotStateState>;
#[derive(Serialize, Deserialize)]
pub struct GetRobotStateState {
    pub error_code: String,
    pub error_msg: String,
    pub enable: String,
    pub power: String,
}

// disable robot
pub type DisableRobotRequest = Request<{ Command::DisableRobot }, ()>;
pub type DisableRobotResponse = Response<{ Command::DisableRobot }, DisableRobotState>;
pub type DisableRobotState = DefaultState;

// torque control enable
pub type TorqueControlEnableRequest =
    Request<{ Command::TorqueControlEnable }, TorqueControlEnableData>;
pub type TorqueControlEnableResponse =
    Response<{ Command::TorqueControlEnable }, TorqueControlEnableState>;
pub type TorqueControlEnableState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct TorqueControlEnableData {
    pub enable_flag: u8,
}

// torque feedforward
pub type TorqueFeedforwardRequest = Request<{ Command::TorqueFeedforward }, TorqueFeedforwardData>;
pub type TorqueFeedforwardResponse =
    Response<{ Command::TorqueFeedforward }, TorqueFeedforwardState>;
pub type TorqueFeedforwardState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct TorqueFeedforwardData {
    pub grv_current: [f64; 6],
    pub includegrvflag: u8,
}

// servo move
pub type ServoMoveRequest = Request<{ Command::ServoMove }, ServoMoveData>;
pub type ServoMoveResponse = Response<{ Command::ServoMove }, ServoMoveState>;
pub type ServoMoveState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct ServoMoveData {
    pub relflag: u8,
}

// servo j
pub type ServoJRequest = Request<{ Command::ServoJ }, ServoJData>;
pub type ServoJResponse = Response<{ Command::ServoJ }, ServoJState>;
pub type ServoJState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct ServoJData {
    pub joint_angles: [f64; 6],
    pub relflag: u8,
}

// servo p
pub type ServoPRequest = Request<{ Command::ServoP }, ServoPData>;
pub type ServoPResponse = Response<{ Command::ServoP }, ServoPState>;
pub type ServoPState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct ServoPData {
    pub cart_position: [f64; 6], // 文档中变量名有拼写错误
    pub relflag: u8,
}

// get data
pub type GetDataRequest = Request<{ Command::GetData }, ()>;
pub type GetDataResponse = Response<{ Command::GetData }, GetDataState>;
#[derive(Serialize, Deserialize)]
pub struct GetDataState {
    pub error_code: String,
    pub error_msg: String,
    pub tio_dout: [u8; 8],
    pub tool_position: [f64; 9],
    pub tio_ain: [u8; 2],
    pub paused: u8,
    pub cmd_name: String,
    pub estop: u8,
    pub current_tool_id: u8,
    pub actual_position: [f64; 9],
    pub joint_actual_position: [f64; 9],
    pub rapidrate: f64,
    pub enabled: u8,
}

// rapid rate
pub type RapidRateRequest = Request<{ Command::RapidRate }, RapidRateData>;
pub type RapidRateResponse = Response<{ Command::RapidRate }, RapidRateState>;
pub type RapidRateState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct RapidRateData {
    pub rate_value: f64,
}

// load program
pub type LoadProgramRequest = Request<{ Command::LoadProgram }, LoadProgramData>;
pub type LoadProgramResponse = Response<{ Command::LoadProgram }, LoadProgramState>;
pub type LoadProgramState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct LoadProgramData {
    pub program_name: String,
}

// get loaded program
pub type GetLoadedProgramRequest = Request<{ Command::GetLoadedProgram }, ()>;
pub type GetLoadedProgramResponse = Response<{ Command::GetLoadedProgram }, GetLoadedProgramState>;
#[derive(Serialize, Deserialize)]
pub struct GetLoadedProgramState {
    pub error_code: String,
    pub error_msg: String,
    pub program_name: String,
}

// play program
pub type PlayProgramRequest = Request<{ Command::PlayProgram }, ()>;
pub type PlayProgramResponse = Response<{ Command::PlayProgram }, PlayProgramState>;
pub type PlayProgramState = DefaultState;

// pause program
pub type PauseProgramRequest = Request<{ Command::PauseProgram }, ()>;
pub type PauseProgramResponse = Response<{ Command::PauseProgram }, PauseProgramState>;
pub type PauseProgramState = DefaultState;

// resume program
pub type ResumeProgramRequest = Request<{ Command::ResumeProgram }, ()>;
pub type ResumeProgramResponse = Response<{ Command::ResumeProgram }, ResumeProgramState>;
pub type ResumeProgramState = DefaultState;

// stop program
pub type StopProgramRequest = Request<{ Command::StopProgram }, ()>;
pub type StopProgramResponse = Response<{ Command::StopProgram }, StopProgramState>;
pub type StopProgramState = DefaultState;

// get program state
pub type GetProgramStateRequest = Request<{ Command::GetProgramState }, ()>;
pub type GetProgramStateResponse = Response<{ Command::GetProgramState }, GetProgramStateState>;
#[derive(Serialize, Deserialize)]
pub struct GetProgramStateState {
    pub error_code: String,
    pub error_msg: String,
    pub program_state: String,
}

// set digital output
pub type SetDigitalOutputRequest = Request<{ Command::SetDigitalOutput }, SetDigitalOutputData>;
pub type SetDigitalOutputResponse = Response<{ Command::SetDigitalOutput }, SetDigitalOutputState>;
pub type SetDigitalOutputState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct SetDigitalOutputData {
    pub type_number: u8,
    pub index: u8,
    pub value: u8,
}

// get digital input status
pub type GetDigitalInputStatusRequest = Request<{ Command::GetDigitalInputStatus }, ()>;
pub type GetDigitalInputStatusResponse =
    Response<{ Command::GetDigitalInputStatus }, GetDigitalInputStatusState>;
#[serde_as]
#[derive(Serialize, Deserialize)]
pub struct GetDigitalInputStatusState {
    pub error_code: String,
    pub error_msg: String,
    #[serde_as(as = "[_; 64]")]
    pub din_status: [u8; 64],
}

// set analog output
pub type SetAnalogOutputRequest = Request<{ Command::SetAnalogOutput }, SetAnalogOutputData>;
pub type SetAnalogOutputResponse = Response<{ Command::SetAnalogOutput }, SetAnalogOutputState>;
pub type SetAnalogOutputState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct SetAnalogOutputData {
    pub type_number: u8,
    pub index: u8,
    pub value: f64,
}

// set tool offsets
pub type SetToolOffsetsRequest = Request<{ Command::SetToolOffsets }, SetToolOffsetsData>;
pub type SetToolOffsetsResponse = Response<{ Command::SetToolOffsets }, SetToolOffsetsState>;
pub type SetToolOffsetsState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct SetToolOffsetsData {
    pub tooloffset: [f64; 6],
    pub id: u8,
    pub name: String,
}

// set tool id
pub type SetToolIdRequest = Request<{ Command::SetToolId }, SetToolIdData>;
pub type SetToolIdResponse = Response<{ Command::SetToolId }, SetToolIdState>;
pub type SetToolIdState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct SetToolIdData {
    pub tool_id: u8,
}

// pub set user offsets
pub type SetUserOffsetsRequest = Request<{ Command::SetUserOffsets }, SetUserOffsetsData>;
pub type SetUserOffsetsResponse = Response<{ Command::SetUserOffsets }, SetUserOffsetsState>;
pub type SetUserOffsetsState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct SetUserOffsetsData {
    pub useroffset: [f64; 6], //文档中的变量名有拼写错误
    pub id: u8,
    pub name: String,
}

// set user id
pub type SetUserIdRequest = Request<{ Command::SetUserId }, SetUserIdData>;
pub type SetUserIdResponse = Response<{ Command::SetUserId }, SetUserIdState>;
pub type SetUserIdState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct SetUserIdData {
    pub user_frame_id: u8, //文档中的变量名说明有错误
}

// get extio status
pub type GetExtioStatusRequest = Request<{ Command::GetExtioStatus }, ()>;
pub type GetExtioStatusResponse = Response<{ Command::GetExtioStatus }, GetExtioStatusState>;
#[derive(Serialize, Deserialize)]
pub struct GetExtioStatusState {
    pub error_code: String,
    pub error_msg: String,
    pub extio_status: ExtioStatus,
}
#[derive(Serialize, Deserialize)]
pub struct ExtioStatus {
    pub version: u8,
    pub setups: Vec<Value>,
}
// struct ExtioSetup {
//     setup_id: u8,
//     mobus_io_name: String,
//     address: SocketAddrV4,
//     channel_counts: ChannelCounts,
//     pinmap: HashMap<String, u8>,
// }
// struct ChannelCounts {
//     ai: (u8, u8),
//     do_: (u8, u8),
//     ao: (u8, u8),
//     di: (u8, u8),
// }
// struct ExtioCurrentState {
//     state: u8,
//     di: (u8, u8, u8, u8, u8, u8, u8, u8),
// }

// get funcdi status
pub type GetFuncdiStatusRequest = Request<{ Command::GetFuncdiStatus }, ()>;
pub type GetFuncdiStatusResponse = Response<{ Command::GetFuncdiStatus }, GetFuncdiStatusState>;
#[derive(Serialize, Deserialize)]
pub struct GetFuncdiStatusState {
    pub error_code: String,
    pub error_msg: String,
    pub funcdi_status: [[i8; 2]; 12],
}

// pub drag status
pub type DragStatusRequest = Request<{ Command::DragStatus }, ()>;
pub type DragStatusResponse = Response<{ Command::DragStatus }, DragStatusState>;
#[derive(Serialize, Deserialize)]
pub struct DragStatusState {
    pub error_code: String,
    pub error_msg: String,
    pub drag_status: bool,
}

// query user defined variable
pub type QueryUserDefinedVariableRequest = Request<{ Command::QueryUserDefinedVariable }, ()>;
pub type QueryUserDefinedVariableResponse =
    Response<{ Command::QueryUserDefinedVariable }, QueryUserDefinedVariableState>;
#[derive(Serialize, Deserialize)]
pub struct QueryUserDefinedVariableState {
    pub error_code: String,
    pub error_msg: String,
    pub var_list: Vec<Variable>,
}
#[derive(Serialize, Deserialize)]
pub struct Variable {
    pub alias: String,
    pub id: u32,
    pub value: f64,
}

// modify user defined variable
pub type ModifyUserDefinedVariableRequest =
    Request<{ Command::ModifyUserDefinedVariable }, ModifyUserDefinedVariableData>;
pub type ModifyUserDefinedVariableResponse =
    Response<{ Command::ModifyUserDefinedVariable }, ModifyUserDefinedVariableState>;
pub type ModifyUserDefinedVariableState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct ModifyUserDefinedVariableData {
    pub id_new: u32,
    pub alias_new: String,
    pub value_new: f64,
}

// protective stop status
pub type ProtectiveStopStatusRequest = Request<{ Command::ProtectiveStopStatus }, ()>;
pub type ProtectiveStopStatusResponse =
    Response<{ Command::ProtectiveStopStatus }, ProtectiveStopStatusState>;
#[derive(Serialize, Deserialize)]
pub struct ProtectiveStopStatusState {
    pub error_code: String,
    pub error_msg: String,
    pub protective_stop: u8,
}

// jog
pub type JogRequest = Request<{ Command::Jog }, JogData>;
pub type JogResponse = Response<{ Command::Jog }, JogState>;
pub type JogState = DefaultState;
#[derive(Serialize, Deserialize)]
pub enum JogData {
    Mode0 {
        coord_map: u8,
        jnum: u8,
    },
    Mode1 {
        coord_map: u8,
        jnum: u8,
        jogvel: f64,
    },
    Mode2 {
        coord_map: u8,
        jnum: u8,
        jogvel: f64,
        poscmd: f64,
    },
}

impl JogData {
    pub fn jog_mode(&self) -> u8 {
        match self {
            JogData::Mode0 { .. } => 0,
            JogData::Mode1 { .. } => 1,
            JogData::Mode2 { .. } => 2,
        }
    }
}

// move l
pub type MoveLRequest = Request<{ Command::MoveL }, MoveLData>;
pub type MoveLResponse = Response<{ Command::MoveL }, MoveLState>;
pub type MoveLState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct MoveLData {
    pub cart_position: [f64; 6],
    pub speed: f64,
    pub accel: f64,
    pub relflag: u8,
}

// wait complete DEPRECATED
pub type WaitCompleteRequest = Request<{ Command::WaitComplete }, ()>;
pub type WaitCompleteResponse = Response<{ Command::WaitComplete }, WaitCompleteState>;
pub type WaitCompleteState = DefaultState;

// set payload
pub type SetPayloadRequest = Request<{ Command::SetPayload }, SetPayloadData>;
pub type SetPayloadResponse = Response<{ Command::SetPayload }, SetPayloadState>;
pub type SetPayloadState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct SetPayloadData {
    pub mass: f64,
    pub centroid: [f64; 3],
}

// get payload
pub type GetPayloadRequest = Request<{ Command::GetPayload }, ()>;
pub type GetPayloadResponse = Response<{ Command::GetPayload }, GetPayloadState>;
#[derive(Serialize, Deserialize)]
pub struct GetPayloadState {
    pub error_code: String,
    pub error_msg: String,
    pub mass: f64,
    pub centroid: [f64; 3],
}

// set clsn sensitivity
pub type SetClsnSensitivityRequest =
    Request<{ Command::SetClsnSensitivity }, SetClsnSensitivityData>;
pub type SetClsnSensitivityResponse =
    Response<{ Command::SetClsnSensitivity }, SetClsnSensitivityState>;
pub type SetClsnSensitivityState = DefaultState;
#[derive(Serialize, Deserialize)]
pub struct SetClsnSensitivityData {
    pub sensitivity_level: u8, // 更改了文档中的变量名以与下一命令一致
}

// get clsn sensitivity
pub type GetClsnSensitivityRequest = Request<{ Command::GetClsnSensitivity }, ()>;
pub type GetClsnSensitivityResponse =
    Response<{ Command::GetClsnSensitivity }, GetClsnSensitivityState>;
#[derive(Serialize, Deserialize)]
pub struct GetClsnSensitivityState {
    pub error_code: String,
    pub error_msg: String,
    pub sensitivity_level: u8,
}

// kine forward
pub type KineForwardRequest = Request<{ Command::KineForward }, ()>;
pub type KineForwardResponse = Response<{ Command::KineForward }, KineForwardState>;
#[derive(Serialize, Deserialize)]
pub struct KineForwardState {
    pub error_code: String,
    pub error_msg: String,
    pub cart_position: [f64; 6],
}
#[derive(Serialize, Deserialize)]
pub struct KineForwardData {
    pub joint_angles: [f64; 6],
}

// kine inverse
pub type KineInverseRequest = Request<{ Command::KineInverse }, KineInverseData>;
pub type KineInverseResponse = Response<{ Command::KineInverse }, KineInverseState>;
#[derive(Serialize, Deserialize)]
pub struct KineInverseState {
    pub error_code: String,
    pub error_msg: String,
    pub joint_angles: [f64; 6],
}
#[derive(Serialize, Deserialize)]
pub struct KineInverseData {
    pub joint_angles: [f64; 6],
    pub cart_position: [f64; 6],
}

// clear error
pub type ClearErrorRequest = Request<{ Command::ClearError }, ()>;
pub type ClearErrorResponse = Response<{ Command::ClearError }, ClearErrorState>;
pub type ClearErrorState = DefaultState;

// get joint pos
pub type GetJointPosRequest = Request<{ Command::GetJointPos }, ()>;
pub type GetJointPosResponse = Response<{ Command::GetJointPos }, GetJointPosState>;
#[derive(Serialize, Deserialize)]
pub struct GetJointPosState {
    pub error_code: String,
    pub error_msg: String,
    pub joint_angles: [f64; 6],
}

// get tcp pos
pub type GetTcpPosRequest = Request<{ Command::GetTcpPos }, ()>;
pub type GetTcpPosResponse = Response<{ Command::GetTcpPos }, GetTcpPosState>;
#[derive(Serialize, Deserialize)]
pub struct GetTcpPosState {
    pub error_code: String,
    pub error_msg: String,
    pub tcp_pos: [f64; 6],
}

impl<const C: Command, D> From<D> for Request<C, D> {
    fn from(data: D) -> Self {
        Self { data }
    }
}

impl<const C: Command, S> From<S> for Response<C, S> {
    fn from(state: S) -> Self {
        Self { state }
    }
}

impl<const C: Command, S> Response<C, S> {
    pub fn into_state(self) -> S {
        self.state
    }
}

impl<const C: Command, D> CommandSerde for Request<C, D>
where
    D: Serialize + DeserializeOwned,
{
    fn serialize(&self) -> String {
        let mut value = serde_json::to_value(&self.data).unwrap();
        match &mut value {
            Value::Object(obj) => {
                obj.insert("command".to_string(), serde_json::to_value(C).unwrap());
            }
            Value::Null => {
                let mut obj = serde_json::Map::new();
                obj.insert("command".to_string(), serde_json::to_value(C).unwrap());
                value = Value::Object(obj);
            }
            _ => {}
        }
        value.to_string()
    }
    fn deserialize(data: &str) -> Option<Self> {
        let mut value: Value = serde_json::from_str(data).unwrap();
        if let Value::Object(obj) = &mut value {
            obj.remove("command");
        }
        if let Ok(data) = serde_json::from_value::<D>(value) {
            Some(Self::from(data))
        } else {
            None
        }
    }
}

impl<const C: Command, S> CommandSerde for Response<C, S>
where
    S: Serialize + DeserializeOwned,
{
    fn serialize(&self) -> String {
        let mut value = serde_json::to_value(&self.state).unwrap();
        match &mut value {
            Value::Object(obj) => {
                obj.insert("command".to_string(), serde_json::to_value(C).unwrap());
            }
            Value::Null => {
                let mut obj = serde_json::Map::new();
                obj.insert("command".to_string(), serde_json::to_value(C).unwrap());
                value = Value::Object(obj);
            }
            _ => {}
        }
        value.to_string()
    }
    fn deserialize(data: &str) -> Option<Self> {
        let mut value: Value = serde_json::from_str(data).unwrap();
        if let Value::Object(obj) = &mut value {
            obj.remove("command");
        }
        if let Ok(state) = serde_json::from_value::<S>(value) {
            Some(Self::from(state))
        } else {
            None
        }
    }
}

#[cfg(test)]
mod tests {}