distant_protocol/
response.rs

1use std::io;
2
3use derive_more::IsVariant;
4use serde::{Deserialize, Serialize};
5use strum::{AsRefStr, EnumDiscriminants, EnumIter, EnumMessage, EnumString};
6
7use crate::common::{
8    Change, DirEntry, Error, Metadata, ProcessId, SearchId, SearchQueryMatch, SystemInfo, Version,
9};
10
11/// Represents the payload of a successful response
12#[derive(
13    Clone, Debug, PartialEq, Eq, AsRefStr, IsVariant, EnumDiscriminants, Serialize, Deserialize,
14)]
15#[strum_discriminants(derive(
16    AsRefStr,
17    strum::Display,
18    EnumIter,
19    EnumMessage,
20    EnumString,
21    Hash,
22    PartialOrd,
23    Ord,
24    IsVariant,
25    Serialize,
26    Deserialize
27))]
28#[strum_discriminants(name(ResponseKind))]
29#[strum_discriminants(strum(serialize_all = "snake_case"))]
30#[serde(rename_all = "snake_case", deny_unknown_fields, tag = "type")]
31#[strum(serialize_all = "snake_case")]
32pub enum Response {
33    /// General okay with no extra data, returned in cases like
34    /// creating or removing a directory, copying a file, or renaming
35    /// a file
36    Ok,
37
38    /// General-purpose failure that occurred from some request
39    Error(Error),
40
41    /// Response containing some arbitrary, binary data
42    Blob {
43        /// Binary data associated with the response
44        #[serde(with = "serde_bytes")]
45        data: Vec<u8>,
46    },
47
48    /// Response containing some arbitrary, text data
49    Text {
50        /// Text data associated with the response
51        data: String,
52    },
53
54    /// Response to reading a directory
55    DirEntries {
56        /// Entries contained within the requested directory
57        entries: Vec<DirEntry>,
58
59        /// Errors encountered while scanning for entries
60        errors: Vec<Error>,
61    },
62
63    /// Response to a filesystem change for some watched file, directory, or symlink
64    Changed(Change),
65
66    /// Response to checking if a path exists
67    Exists { value: bool },
68
69    /// Represents metadata about some filesystem object (file, directory, symlink) on remote machine
70    Metadata(Metadata),
71
72    /// Represents a search being started
73    SearchStarted {
74        /// Arbitrary id associated with search
75        id: SearchId,
76    },
77
78    /// Represents some subset of results for a search query (may not be all of them)
79    SearchResults {
80        /// Arbitrary id associated with search
81        id: SearchId,
82
83        /// Collection of matches from performing a query
84        matches: Vec<SearchQueryMatch>,
85    },
86
87    /// Represents a search being completed
88    SearchDone {
89        /// Arbitrary id associated with search
90        id: SearchId,
91    },
92
93    /// Response to starting a new process
94    ProcSpawned {
95        /// Arbitrary id associated with running process
96        id: ProcessId,
97    },
98
99    /// Actively-transmitted stdout as part of running process
100    ProcStdout {
101        /// Arbitrary id associated with running process
102        id: ProcessId,
103
104        /// Data read from a process' stdout pipe
105        #[serde(with = "serde_bytes")]
106        data: Vec<u8>,
107    },
108
109    /// Actively-transmitted stderr as part of running process
110    ProcStderr {
111        /// Arbitrary id associated with running process
112        id: ProcessId,
113
114        /// Data read from a process' stderr pipe
115        #[serde(with = "serde_bytes")]
116        data: Vec<u8>,
117    },
118
119    /// Response to a process finishing
120    ProcDone {
121        /// Arbitrary id associated with running process
122        id: ProcessId,
123
124        /// Whether or not termination was successful
125        success: bool,
126
127        /// Exit code associated with termination, will be missing if terminated by signal
128        #[serde(default, skip_serializing_if = "Option::is_none")]
129        code: Option<i32>,
130    },
131
132    /// Response to retrieving information about the server and the system it is on
133    SystemInfo(SystemInfo),
134
135    /// Response to retrieving information about the server's version
136    Version(Version),
137}
138
139impl From<io::Error> for Response {
140    fn from(x: io::Error) -> Self {
141        Self::Error(Error::from(x))
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148
149    mod ok {
150        use super::*;
151
152        #[test]
153        fn should_be_able_to_serialize_to_json() {
154            let payload = Response::Ok;
155
156            let value = serde_json::to_value(payload).unwrap();
157            assert_eq!(
158                value,
159                serde_json::json!({
160                    "type": "ok",
161                })
162            );
163        }
164
165        #[test]
166        fn should_be_able_to_deserialize_from_json() {
167            let value = serde_json::json!({
168                "type": "ok",
169            });
170
171            let payload: Response = serde_json::from_value(value).unwrap();
172            assert_eq!(payload, Response::Ok);
173        }
174
175        #[test]
176        fn should_be_able_to_serialize_to_msgpack() {
177            let payload = Response::Ok;
178
179            // NOTE: We don't actually check the output here because it's an implementation detail
180            // and could change as we change how serialization is done. This is merely to verify
181            // that we can serialize since there are times when serde fails to serialize at
182            // runtime.
183            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
184        }
185
186        #[test]
187        fn should_be_able_to_deserialize_from_msgpack() {
188            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
189            // verify that we are not corrupting or causing issues when serializing on a
190            // client/server and then trying to deserialize on the other side. This has happened
191            // enough times with minor changes that we need tests to verify.
192            let buf = rmp_serde::encode::to_vec_named(&Response::Ok).unwrap();
193
194            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
195            assert_eq!(payload, Response::Ok);
196        }
197    }
198
199    mod error {
200        use super::*;
201        use crate::common::ErrorKind;
202
203        #[test]
204        fn should_be_able_to_serialize_to_json() {
205            let payload = Response::Error(Error {
206                kind: ErrorKind::AddrInUse,
207                description: String::from("some description"),
208            });
209
210            let value = serde_json::to_value(payload).unwrap();
211            assert_eq!(
212                value,
213                serde_json::json!({
214                    "type": "error",
215                    "kind": "addr_in_use",
216                    "description": "some description",
217                })
218            );
219        }
220
221        #[test]
222        fn should_be_able_to_deserialize_from_json() {
223            let value = serde_json::json!({
224                "type": "error",
225                "kind": "addr_in_use",
226                "description": "some description",
227            });
228
229            let payload: Response = serde_json::from_value(value).unwrap();
230            assert_eq!(
231                payload,
232                Response::Error(Error {
233                    kind: ErrorKind::AddrInUse,
234                    description: String::from("some description"),
235                })
236            );
237        }
238
239        #[test]
240        fn should_be_able_to_serialize_to_msgpack() {
241            let payload = Response::Error(Error {
242                kind: ErrorKind::AddrInUse,
243                description: String::from("some description"),
244            });
245
246            // NOTE: We don't actually check the output here because it's an implementation detail
247            // and could change as we change how serialization is done. This is merely to verify
248            // that we can serialize since there are times when serde fails to serialize at
249            // runtime.
250            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
251        }
252
253        #[test]
254        fn should_be_able_to_deserialize_from_msgpack() {
255            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
256            // verify that we are not corrupting or causing issues when serializing on a
257            // client/server and then trying to deserialize on the other side. This has happened
258            // enough times with minor changes that we need tests to verify.
259            let buf = rmp_serde::encode::to_vec_named(&Response::Error(Error {
260                kind: ErrorKind::AddrInUse,
261                description: String::from("some description"),
262            }))
263            .unwrap();
264
265            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
266            assert_eq!(
267                payload,
268                Response::Error(Error {
269                    kind: ErrorKind::AddrInUse,
270                    description: String::from("some description"),
271                })
272            );
273        }
274    }
275
276    mod blob {
277        use super::*;
278
279        #[test]
280        fn should_be_able_to_serialize_to_json() {
281            let payload = Response::Blob {
282                data: vec![0, 1, 2, u8::MAX],
283            };
284
285            let value = serde_json::to_value(payload).unwrap();
286            assert_eq!(
287                value,
288                serde_json::json!({
289                    "type": "blob",
290                    "data": [0, 1, 2, u8::MAX],
291                })
292            );
293        }
294
295        #[test]
296        fn should_be_able_to_deserialize_from_json() {
297            let value = serde_json::json!({
298                "type": "blob",
299                "data": [0, 1, 2, u8::MAX],
300            });
301
302            let payload: Response = serde_json::from_value(value).unwrap();
303            assert_eq!(
304                payload,
305                Response::Blob {
306                    data: vec![0, 1, 2, u8::MAX],
307                }
308            );
309        }
310
311        #[test]
312        fn should_be_able_to_serialize_to_msgpack() {
313            let payload = Response::Blob {
314                data: vec![0, 1, 2, u8::MAX],
315            };
316
317            // NOTE: We don't actually check the output here because it's an implementation detail
318            // and could change as we change how serialization is done. This is merely to verify
319            // that we can serialize since there are times when serde fails to serialize at
320            // runtime.
321            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
322        }
323
324        #[test]
325        fn should_be_able_to_deserialize_from_msgpack() {
326            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
327            // verify that we are not corrupting or causing issues when serializing on a
328            // client/server and then trying to deserialize on the other side. This has happened
329            // enough times with minor changes that we need tests to verify.
330            let buf = rmp_serde::encode::to_vec_named(&Response::Blob {
331                data: vec![0, 1, 2, u8::MAX],
332            })
333            .unwrap();
334
335            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
336            assert_eq!(
337                payload,
338                Response::Blob {
339                    data: vec![0, 1, 2, u8::MAX],
340                }
341            );
342        }
343    }
344
345    mod text {
346        use super::*;
347
348        #[test]
349        fn should_be_able_to_serialize_to_json() {
350            let payload = Response::Text {
351                data: String::from("some text"),
352            };
353
354            let value = serde_json::to_value(payload).unwrap();
355            assert_eq!(
356                value,
357                serde_json::json!({
358                    "type": "text",
359                    "data": "some text",
360                })
361            );
362        }
363
364        #[test]
365        fn should_be_able_to_deserialize_from_json() {
366            let value = serde_json::json!({
367                "type": "text",
368                "data": "some text",
369            });
370
371            let payload: Response = serde_json::from_value(value).unwrap();
372            assert_eq!(
373                payload,
374                Response::Text {
375                    data: String::from("some text"),
376                }
377            );
378        }
379
380        #[test]
381        fn should_be_able_to_serialize_to_msgpack() {
382            let payload = Response::Text {
383                data: String::from("some text"),
384            };
385
386            // NOTE: We don't actually check the output here because it's an implementation detail
387            // and could change as we change how serialization is done. This is merely to verify
388            // that we can serialize since there are times when serde fails to serialize at
389            // runtime.
390            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
391        }
392
393        #[test]
394        fn should_be_able_to_deserialize_from_msgpack() {
395            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
396            // verify that we are not corrupting or causing issues when serializing on a
397            // client/server and then trying to deserialize on the other side. This has happened
398            // enough times with minor changes that we need tests to verify.
399            let buf = rmp_serde::encode::to_vec_named(&Response::Text {
400                data: String::from("some text"),
401            })
402            .unwrap();
403
404            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
405            assert_eq!(
406                payload,
407                Response::Text {
408                    data: String::from("some text"),
409                }
410            );
411        }
412    }
413
414    mod dir_entries {
415        use std::path::PathBuf;
416
417        use super::*;
418        use crate::common::{ErrorKind, FileType};
419
420        #[test]
421        fn should_be_able_to_serialize_minimal_payload_to_json() {
422            let payload = Response::DirEntries {
423                entries: Vec::new(),
424                errors: Vec::new(),
425            };
426
427            let value = serde_json::to_value(payload).unwrap();
428            assert_eq!(
429                value,
430                serde_json::json!({
431                    "type": "dir_entries",
432                    "entries": [],
433                    "errors": [],
434                })
435            );
436        }
437
438        #[test]
439        fn should_be_able_to_serialize_full_payload_to_json() {
440            let payload = Response::DirEntries {
441                entries: vec![DirEntry {
442                    path: PathBuf::from("path"),
443                    file_type: FileType::File,
444                    depth: usize::MAX,
445                }],
446                errors: vec![Error {
447                    kind: ErrorKind::AddrInUse,
448                    description: String::from("some description"),
449                }],
450            };
451
452            let value = serde_json::to_value(payload).unwrap();
453            assert_eq!(
454                value,
455                serde_json::json!({
456                    "type": "dir_entries",
457                    "entries": [{
458                        "path": "path",
459                        "file_type": "file",
460                        "depth": usize::MAX,
461                    }],
462                    "errors": [{
463                        "kind": "addr_in_use",
464                        "description": "some description",
465                    }],
466                })
467            );
468        }
469
470        #[test]
471        fn should_be_able_to_deserialize_minimal_payload_from_json() {
472            let value = serde_json::json!({
473                "type": "dir_entries",
474                "entries": [],
475                "errors": [],
476            });
477
478            let payload: Response = serde_json::from_value(value).unwrap();
479            assert_eq!(
480                payload,
481                Response::DirEntries {
482                    entries: Vec::new(),
483                    errors: Vec::new(),
484                }
485            );
486        }
487
488        #[test]
489        fn should_be_able_to_deserialize_full_payload_from_json() {
490            let value = serde_json::json!({
491                "type": "dir_entries",
492                "entries": [{
493                    "path": "path",
494                    "file_type": "file",
495                    "depth": usize::MAX,
496                }],
497                "errors": [{
498                    "kind": "addr_in_use",
499                    "description": "some description",
500                }],
501            });
502
503            let payload: Response = serde_json::from_value(value).unwrap();
504            assert_eq!(
505                payload,
506                Response::DirEntries {
507                    entries: vec![DirEntry {
508                        path: PathBuf::from("path"),
509                        file_type: FileType::File,
510                        depth: usize::MAX,
511                    }],
512                    errors: vec![Error {
513                        kind: ErrorKind::AddrInUse,
514                        description: String::from("some description"),
515                    }],
516                }
517            );
518        }
519
520        #[test]
521        fn should_be_able_to_serialize_minimal_payload_to_msgpack() {
522            let payload = Response::DirEntries {
523                entries: Vec::new(),
524                errors: Vec::new(),
525            };
526
527            // NOTE: We don't actually check the output here because it's an implementation detail
528            // and could change as we change how serialization is done. This is merely to verify
529            // that we can serialize since there are times when serde fails to serialize at
530            // runtime.
531            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
532        }
533
534        #[test]
535        fn should_be_able_to_serialize_full_payload_to_msgpack() {
536            let payload = Response::DirEntries {
537                entries: vec![DirEntry {
538                    path: PathBuf::from("path"),
539                    file_type: FileType::File,
540                    depth: usize::MAX,
541                }],
542                errors: vec![Error {
543                    kind: ErrorKind::AddrInUse,
544                    description: String::from("some description"),
545                }],
546            };
547
548            // NOTE: We don't actually check the output here because it's an implementation detail
549            // and could change as we change how serialization is done. This is merely to verify
550            // that we can serialize since there are times when serde fails to serialize at
551            // runtime.
552            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
553        }
554
555        #[test]
556        fn should_be_able_to_deserialize_minimal_payload_from_msgpack() {
557            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
558            // verify that we are not corrupting or causing issues when serializing on a
559            // client/server and then trying to deserialize on the other side. This has happened
560            // enough times with minor changes that we need tests to verify.
561            let buf = rmp_serde::encode::to_vec_named(&Response::DirEntries {
562                entries: Vec::new(),
563                errors: Vec::new(),
564            })
565            .unwrap();
566
567            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
568            assert_eq!(
569                payload,
570                Response::DirEntries {
571                    entries: Vec::new(),
572                    errors: Vec::new(),
573                }
574            );
575        }
576
577        #[test]
578        fn should_be_able_to_deserialize_full_payload_from_msgpack() {
579            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
580            // verify that we are not corrupting or causing issues when serializing on a
581            // client/server and then trying to deserialize on the other side. This has happened
582            // enough times with minor changes that we need tests to verify.
583            let buf = rmp_serde::encode::to_vec_named(&Response::DirEntries {
584                entries: vec![DirEntry {
585                    path: PathBuf::from("path"),
586                    file_type: FileType::File,
587                    depth: usize::MAX,
588                }],
589                errors: vec![Error {
590                    kind: ErrorKind::AddrInUse,
591                    description: String::from("some description"),
592                }],
593            })
594            .unwrap();
595
596            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
597            assert_eq!(
598                payload,
599                Response::DirEntries {
600                    entries: vec![DirEntry {
601                        path: PathBuf::from("path"),
602                        file_type: FileType::File,
603                        depth: usize::MAX,
604                    }],
605                    errors: vec![Error {
606                        kind: ErrorKind::AddrInUse,
607                        description: String::from("some description"),
608                    }],
609                }
610            );
611        }
612    }
613
614    mod changed {
615        use std::path::PathBuf;
616
617        use super::*;
618        use crate::common::{ChangeDetails, ChangeDetailsAttribute, ChangeKind};
619
620        #[test]
621        fn should_be_able_to_serialize_minimal_payload_to_json() {
622            let payload = Response::Changed(Change {
623                timestamp: u64::MAX,
624                kind: ChangeKind::Access,
625                path: PathBuf::from("path"),
626                details: ChangeDetails::default(),
627            });
628
629            let value = serde_json::to_value(payload).unwrap();
630            assert_eq!(
631                value,
632                serde_json::json!({
633                    "type": "changed",
634                    "timestamp": u64::MAX,
635                    "kind": "access",
636                    "path": "path",
637                })
638            );
639        }
640
641        #[test]
642        fn should_be_able_to_serialize_full_payload_to_json() {
643            let payload = Response::Changed(Change {
644                timestamp: u64::MAX,
645                kind: ChangeKind::Access,
646                path: PathBuf::from("path"),
647                details: ChangeDetails {
648                    attribute: Some(ChangeDetailsAttribute::Permissions),
649                    renamed: Some(PathBuf::from("renamed")),
650                    timestamp: Some(u64::MAX),
651                    extra: Some(String::from("info")),
652                },
653            });
654
655            let value = serde_json::to_value(payload).unwrap();
656            assert_eq!(
657                value,
658                serde_json::json!({
659                    "type": "changed",
660                    "timestamp": u64::MAX,
661                    "kind": "access",
662                    "path": "path",
663                    "details": {
664                        "attribute": "permissions",
665                        "renamed": "renamed",
666                        "timestamp": u64::MAX,
667                        "extra": "info",
668                    },
669                })
670            );
671        }
672
673        #[test]
674        fn should_be_able_to_deserialize_minimal_payload_from_json() {
675            let value = serde_json::json!({
676                "type": "changed",
677                "timestamp": u64::MAX,
678                "kind": "access",
679                "path": "path",
680            });
681
682            let payload: Response = serde_json::from_value(value).unwrap();
683            assert_eq!(
684                payload,
685                Response::Changed(Change {
686                    timestamp: u64::MAX,
687                    kind: ChangeKind::Access,
688                    path: PathBuf::from("path"),
689                    details: ChangeDetails::default(),
690                })
691            );
692        }
693
694        #[test]
695        fn should_be_able_to_deserialize_full_payload_from_json() {
696            let value = serde_json::json!({
697                "type": "changed",
698                "timestamp": u64::MAX,
699                "kind": "access",
700                "path": "path",
701                "details": {
702                    "attribute": "permissions",
703                    "renamed": "renamed",
704                    "timestamp": u64::MAX,
705                    "extra": "info",
706                },
707            });
708
709            let payload: Response = serde_json::from_value(value).unwrap();
710            assert_eq!(
711                payload,
712                Response::Changed(Change {
713                    timestamp: u64::MAX,
714                    kind: ChangeKind::Access,
715                    path: PathBuf::from("path"),
716                    details: ChangeDetails {
717                        attribute: Some(ChangeDetailsAttribute::Permissions),
718                        renamed: Some(PathBuf::from("renamed")),
719                        timestamp: Some(u64::MAX),
720                        extra: Some(String::from("info")),
721                    },
722                })
723            );
724        }
725
726        #[test]
727        fn should_be_able_to_serialize_minimal_payload_to_msgpack() {
728            let payload = Response::Changed(Change {
729                timestamp: u64::MAX,
730                kind: ChangeKind::Access,
731                path: PathBuf::from("path"),
732                details: ChangeDetails::default(),
733            });
734
735            // NOTE: We don't actually check the output here because it's an implementation detail
736            // and could change as we change how serialization is done. This is merely to verify
737            // that we can serialize since there are times when serde fails to serialize at
738            // runtime.
739            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
740        }
741
742        #[test]
743        fn should_be_able_to_serialize_full_payload_to_msgpack() {
744            let payload = Response::Changed(Change {
745                timestamp: u64::MAX,
746                kind: ChangeKind::Access,
747                path: PathBuf::from("path"),
748                details: ChangeDetails {
749                    attribute: Some(ChangeDetailsAttribute::Permissions),
750                    renamed: Some(PathBuf::from("renamed")),
751                    timestamp: Some(u64::MAX),
752                    extra: Some(String::from("info")),
753                },
754            });
755
756            // NOTE: We don't actually check the output here because it's an implementation detail
757            // and could change as we change how serialization is done. This is merely to verify
758            // that we can serialize since there are times when serde fails to serialize at
759            // runtime.
760            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
761        }
762
763        #[test]
764        fn should_be_able_to_deserialize_minimal_payload_from_msgpack() {
765            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
766            // verify that we are not corrupting or causing issues when serializing on a
767            // client/server and then trying to deserialize on the other side. This has happened
768            // enough times with minor changes that we need tests to verify.
769            let buf = rmp_serde::encode::to_vec_named(&Response::Changed(Change {
770                timestamp: u64::MAX,
771                kind: ChangeKind::Access,
772                path: PathBuf::from("path"),
773                details: ChangeDetails::default(),
774            }))
775            .unwrap();
776
777            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
778            assert_eq!(
779                payload,
780                Response::Changed(Change {
781                    timestamp: u64::MAX,
782                    kind: ChangeKind::Access,
783                    path: PathBuf::from("path"),
784                    details: ChangeDetails::default(),
785                })
786            );
787        }
788
789        #[test]
790        fn should_be_able_to_deserialize_full_payload_from_msgpack() {
791            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
792            // verify that we are not corrupting or causing issues when serializing on a
793            // client/server and then trying to deserialize on the other side. This has happened
794            // enough times with minor changes that we need tests to verify.
795            let buf = rmp_serde::encode::to_vec_named(&Response::Changed(Change {
796                timestamp: u64::MAX,
797                kind: ChangeKind::Access,
798                path: PathBuf::from("path"),
799                details: ChangeDetails {
800                    attribute: Some(ChangeDetailsAttribute::Permissions),
801                    renamed: Some(PathBuf::from("renamed")),
802                    timestamp: Some(u64::MAX),
803                    extra: Some(String::from("info")),
804                },
805            }))
806            .unwrap();
807
808            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
809            assert_eq!(
810                payload,
811                Response::Changed(Change {
812                    timestamp: u64::MAX,
813                    kind: ChangeKind::Access,
814                    path: PathBuf::from("path"),
815                    details: ChangeDetails {
816                        attribute: Some(ChangeDetailsAttribute::Permissions),
817                        renamed: Some(PathBuf::from("renamed")),
818                        timestamp: Some(u64::MAX),
819                        extra: Some(String::from("info")),
820                    },
821                })
822            );
823        }
824    }
825
826    mod exists {
827        use super::*;
828
829        #[test]
830        fn should_be_able_to_serialize_to_json() {
831            let payload = Response::Exists { value: true };
832
833            let value = serde_json::to_value(payload).unwrap();
834            assert_eq!(
835                value,
836                serde_json::json!({
837                    "type": "exists",
838                    "value": true,
839                })
840            );
841        }
842
843        #[test]
844        fn should_be_able_to_deserialize_from_json() {
845            let value = serde_json::json!({
846                "type": "exists",
847                "value": true,
848            });
849
850            let payload: Response = serde_json::from_value(value).unwrap();
851            assert_eq!(payload, Response::Exists { value: true });
852        }
853
854        #[test]
855        fn should_be_able_to_serialize_to_msgpack() {
856            let payload = Response::Exists { value: true };
857
858            // NOTE: We don't actually check the output here because it's an implementation detail
859            // and could change as we change how serialization is done. This is merely to verify
860            // that we can serialize since there are times when serde fails to serialize at
861            // runtime.
862            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
863        }
864
865        #[test]
866        fn should_be_able_to_deserialize_from_msgpack() {
867            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
868            // verify that we are not corrupting or causing issues when serializing on a
869            // client/server and then trying to deserialize on the other side. This has happened
870            // enough times with minor changes that we need tests to verify.
871            let buf = rmp_serde::encode::to_vec_named(&Response::Exists { value: true }).unwrap();
872
873            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
874            assert_eq!(payload, Response::Exists { value: true });
875        }
876    }
877
878    mod metadata {
879        use std::path::PathBuf;
880
881        use super::*;
882        use crate::common::{FileType, UnixMetadata, WindowsMetadata};
883
884        #[test]
885        fn should_be_able_to_serialize_minimal_payload_to_json() {
886            let payload = Response::Metadata(Metadata {
887                canonicalized_path: None,
888                file_type: FileType::File,
889                len: 0,
890                readonly: false,
891                accessed: None,
892                created: None,
893                modified: None,
894                unix: None,
895                windows: None,
896            });
897
898            let value = serde_json::to_value(payload).unwrap();
899            assert_eq!(
900                value,
901                serde_json::json!({
902                    "type": "metadata",
903                    "file_type": "file",
904                    "len": 0,
905                    "readonly": false,
906                })
907            );
908        }
909
910        #[test]
911        fn should_be_able_to_serialize_full_payload_to_json() {
912            let payload = Response::Metadata(Metadata {
913                canonicalized_path: Some(PathBuf::from("path")),
914                file_type: FileType::File,
915                len: u64::MAX,
916                readonly: true,
917                accessed: Some(u64::MAX),
918                created: Some(u64::MAX),
919                modified: Some(u64::MAX),
920                unix: Some(UnixMetadata {
921                    owner_read: true,
922                    owner_write: false,
923                    owner_exec: false,
924                    group_read: true,
925                    group_write: false,
926                    group_exec: false,
927                    other_read: true,
928                    other_write: false,
929                    other_exec: false,
930                }),
931                windows: Some(WindowsMetadata {
932                    archive: true,
933                    compressed: false,
934                    encrypted: true,
935                    hidden: false,
936                    integrity_stream: true,
937                    normal: false,
938                    not_content_indexed: true,
939                    no_scrub_data: false,
940                    offline: true,
941                    recall_on_data_access: false,
942                    recall_on_open: true,
943                    reparse_point: false,
944                    sparse_file: true,
945                    system: false,
946                    temporary: true,
947                }),
948            });
949
950            let value = serde_json::to_value(payload).unwrap();
951            assert_eq!(
952                value,
953                serde_json::json!({
954                    "type": "metadata",
955                    "canonicalized_path": "path",
956                    "file_type": "file",
957                    "len": u64::MAX,
958                    "readonly": true,
959                    "accessed": u64::MAX,
960                    "created": u64::MAX,
961                    "modified": u64::MAX,
962                    "unix": {
963                        "owner_read": true,
964                        "owner_write": false,
965                        "owner_exec": false,
966                        "group_read": true,
967                        "group_write": false,
968                        "group_exec": false,
969                        "other_read": true,
970                        "other_write": false,
971                        "other_exec": false,
972                    },
973                    "windows": {
974                        "archive": true,
975                        "compressed": false,
976                        "encrypted": true,
977                        "hidden": false,
978                        "integrity_stream": true,
979                        "normal": false,
980                        "not_content_indexed": true,
981                        "no_scrub_data": false,
982                        "offline": true,
983                        "recall_on_data_access": false,
984                        "recall_on_open": true,
985                        "reparse_point": false,
986                        "sparse_file": true,
987                        "system": false,
988                        "temporary": true,
989                    }
990                })
991            );
992        }
993
994        #[test]
995        fn should_be_able_to_deserialize_minimal_payload_from_json() {
996            let value = serde_json::json!({
997                "type": "metadata",
998                "file_type": "file",
999                "len": 0,
1000                "readonly": false,
1001            });
1002
1003            let payload: Response = serde_json::from_value(value).unwrap();
1004            assert_eq!(
1005                payload,
1006                Response::Metadata(Metadata {
1007                    canonicalized_path: None,
1008                    file_type: FileType::File,
1009                    len: 0,
1010                    readonly: false,
1011                    accessed: None,
1012                    created: None,
1013                    modified: None,
1014                    unix: None,
1015                    windows: None,
1016                })
1017            );
1018        }
1019
1020        #[test]
1021        fn should_be_able_to_deserialize_full_payload_from_json() {
1022            let value = serde_json::json!({
1023                "type": "metadata",
1024                "canonicalized_path": "path",
1025                "file_type": "file",
1026                "len": u64::MAX,
1027                "readonly": true,
1028                "accessed": u64::MAX,
1029                "created": u64::MAX,
1030                "modified": u64::MAX,
1031                "unix": {
1032                    "owner_read": true,
1033                    "owner_write": false,
1034                    "owner_exec": false,
1035                    "group_read": true,
1036                    "group_write": false,
1037                    "group_exec": false,
1038                    "other_read": true,
1039                    "other_write": false,
1040                    "other_exec": false,
1041                },
1042                "windows": {
1043                    "archive": true,
1044                    "compressed": false,
1045                    "encrypted": true,
1046                    "hidden": false,
1047                    "integrity_stream": true,
1048                    "normal": false,
1049                    "not_content_indexed": true,
1050                    "no_scrub_data": false,
1051                    "offline": true,
1052                    "recall_on_data_access": false,
1053                    "recall_on_open": true,
1054                    "reparse_point": false,
1055                    "sparse_file": true,
1056                    "system": false,
1057                    "temporary": true,
1058                }
1059            });
1060
1061            let payload: Response = serde_json::from_value(value).unwrap();
1062            assert_eq!(
1063                payload,
1064                Response::Metadata(Metadata {
1065                    canonicalized_path: Some(PathBuf::from("path")),
1066                    file_type: FileType::File,
1067                    len: u64::MAX,
1068                    readonly: true,
1069                    accessed: Some(u64::MAX),
1070                    created: Some(u64::MAX),
1071                    modified: Some(u64::MAX),
1072                    unix: Some(UnixMetadata {
1073                        owner_read: true,
1074                        owner_write: false,
1075                        owner_exec: false,
1076                        group_read: true,
1077                        group_write: false,
1078                        group_exec: false,
1079                        other_read: true,
1080                        other_write: false,
1081                        other_exec: false,
1082                    }),
1083                    windows: Some(WindowsMetadata {
1084                        archive: true,
1085                        compressed: false,
1086                        encrypted: true,
1087                        hidden: false,
1088                        integrity_stream: true,
1089                        normal: false,
1090                        not_content_indexed: true,
1091                        no_scrub_data: false,
1092                        offline: true,
1093                        recall_on_data_access: false,
1094                        recall_on_open: true,
1095                        reparse_point: false,
1096                        sparse_file: true,
1097                        system: false,
1098                        temporary: true,
1099                    }),
1100                })
1101            );
1102        }
1103
1104        #[test]
1105        fn should_be_able_to_serialize_minimal_payload_to_msgpack() {
1106            let payload = Response::Metadata(Metadata {
1107                canonicalized_path: None,
1108                file_type: FileType::File,
1109                len: 0,
1110                readonly: false,
1111                accessed: None,
1112                created: None,
1113                modified: None,
1114                unix: None,
1115                windows: None,
1116            });
1117
1118            // NOTE: We don't actually check the output here because it's an implementation detail
1119            // and could change as we change how serialization is done. This is merely to verify
1120            // that we can serialize since there are times when serde fails to serialize at
1121            // runtime.
1122            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
1123        }
1124
1125        #[test]
1126        fn should_be_able_to_serialize_full_payload_to_msgpack() {
1127            let payload = Response::Metadata(Metadata {
1128                canonicalized_path: Some(PathBuf::from("path")),
1129                file_type: FileType::File,
1130                len: u64::MAX,
1131                readonly: true,
1132                accessed: Some(u64::MAX),
1133                created: Some(u64::MAX),
1134                modified: Some(u64::MAX),
1135                unix: Some(UnixMetadata {
1136                    owner_read: true,
1137                    owner_write: false,
1138                    owner_exec: false,
1139                    group_read: true,
1140                    group_write: false,
1141                    group_exec: false,
1142                    other_read: true,
1143                    other_write: false,
1144                    other_exec: false,
1145                }),
1146                windows: Some(WindowsMetadata {
1147                    archive: true,
1148                    compressed: false,
1149                    encrypted: true,
1150                    hidden: false,
1151                    integrity_stream: true,
1152                    normal: false,
1153                    not_content_indexed: true,
1154                    no_scrub_data: false,
1155                    offline: true,
1156                    recall_on_data_access: false,
1157                    recall_on_open: true,
1158                    reparse_point: false,
1159                    sparse_file: true,
1160                    system: false,
1161                    temporary: true,
1162                }),
1163            });
1164
1165            // NOTE: We don't actually check the output here because it's an implementation detail
1166            // and could change as we change how serialization is done. This is merely to verify
1167            // that we can serialize since there are times when serde fails to serialize at
1168            // runtime.
1169            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
1170        }
1171
1172        #[test]
1173        fn should_be_able_to_deserialize_minimal_payload_from_msgpack() {
1174            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
1175            // verify that we are not corrupting or causing issues when serializing on a
1176            // client/server and then trying to deserialize on the other side. This has happened
1177            // enough times with minor changes that we need tests to verify.
1178            let buf = rmp_serde::encode::to_vec_named(&Response::Metadata(Metadata {
1179                canonicalized_path: None,
1180                file_type: FileType::File,
1181                len: 0,
1182                readonly: false,
1183                accessed: None,
1184                created: None,
1185                modified: None,
1186                unix: None,
1187                windows: None,
1188            }))
1189            .unwrap();
1190
1191            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
1192            assert_eq!(
1193                payload,
1194                Response::Metadata(Metadata {
1195                    canonicalized_path: None,
1196                    file_type: FileType::File,
1197                    len: 0,
1198                    readonly: false,
1199                    accessed: None,
1200                    created: None,
1201                    modified: None,
1202                    unix: None,
1203                    windows: None,
1204                })
1205            );
1206        }
1207
1208        #[test]
1209        fn should_be_able_to_deserialize_full_payload_from_msgpack() {
1210            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
1211            // verify that we are not corrupting or causing issues when serializing on a
1212            // client/server and then trying to deserialize on the other side. This has happened
1213            // enough times with minor changes that we need tests to verify.
1214            let buf = rmp_serde::encode::to_vec_named(&Response::Metadata(Metadata {
1215                canonicalized_path: Some(PathBuf::from("path")),
1216                file_type: FileType::File,
1217                len: u64::MAX,
1218                readonly: true,
1219                accessed: Some(u64::MAX),
1220                created: Some(u64::MAX),
1221                modified: Some(u64::MAX),
1222                unix: Some(UnixMetadata {
1223                    owner_read: true,
1224                    owner_write: false,
1225                    owner_exec: false,
1226                    group_read: true,
1227                    group_write: false,
1228                    group_exec: false,
1229                    other_read: true,
1230                    other_write: false,
1231                    other_exec: false,
1232                }),
1233                windows: Some(WindowsMetadata {
1234                    archive: true,
1235                    compressed: false,
1236                    encrypted: true,
1237                    hidden: false,
1238                    integrity_stream: true,
1239                    normal: false,
1240                    not_content_indexed: true,
1241                    no_scrub_data: false,
1242                    offline: true,
1243                    recall_on_data_access: false,
1244                    recall_on_open: true,
1245                    reparse_point: false,
1246                    sparse_file: true,
1247                    system: false,
1248                    temporary: true,
1249                }),
1250            }))
1251            .unwrap();
1252
1253            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
1254            assert_eq!(
1255                payload,
1256                Response::Metadata(Metadata {
1257                    canonicalized_path: Some(PathBuf::from("path")),
1258                    file_type: FileType::File,
1259                    len: u64::MAX,
1260                    readonly: true,
1261                    accessed: Some(u64::MAX),
1262                    created: Some(u64::MAX),
1263                    modified: Some(u64::MAX),
1264                    unix: Some(UnixMetadata {
1265                        owner_read: true,
1266                        owner_write: false,
1267                        owner_exec: false,
1268                        group_read: true,
1269                        group_write: false,
1270                        group_exec: false,
1271                        other_read: true,
1272                        other_write: false,
1273                        other_exec: false,
1274                    }),
1275                    windows: Some(WindowsMetadata {
1276                        archive: true,
1277                        compressed: false,
1278                        encrypted: true,
1279                        hidden: false,
1280                        integrity_stream: true,
1281                        normal: false,
1282                        not_content_indexed: true,
1283                        no_scrub_data: false,
1284                        offline: true,
1285                        recall_on_data_access: false,
1286                        recall_on_open: true,
1287                        reparse_point: false,
1288                        sparse_file: true,
1289                        system: false,
1290                        temporary: true,
1291                    }),
1292                })
1293            );
1294        }
1295    }
1296
1297    mod search_started {
1298        use super::*;
1299
1300        #[test]
1301        fn should_be_able_to_serialize_to_json() {
1302            let payload = Response::SearchStarted { id: SearchId::MAX };
1303
1304            let value = serde_json::to_value(payload).unwrap();
1305            assert_eq!(
1306                value,
1307                serde_json::json!({
1308                    "type": "search_started",
1309                    "id": SearchId::MAX,
1310                })
1311            );
1312        }
1313
1314        #[test]
1315        fn should_be_able_to_deserialize_from_json() {
1316            let value = serde_json::json!({
1317                "type": "search_started",
1318                "id": SearchId::MAX,
1319            });
1320
1321            let payload: Response = serde_json::from_value(value).unwrap();
1322            assert_eq!(payload, Response::SearchStarted { id: SearchId::MAX });
1323        }
1324
1325        #[test]
1326        fn should_be_able_to_serialize_to_msgpack() {
1327            let payload = Response::SearchStarted { id: SearchId::MAX };
1328
1329            // NOTE: We don't actually check the output here because it's an implementation detail
1330            // and could change as we change how serialization is done. This is merely to verify
1331            // that we can serialize since there are times when serde fails to serialize at
1332            // runtime.
1333            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
1334        }
1335
1336        #[test]
1337        fn should_be_able_to_deserialize_from_msgpack() {
1338            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
1339            // verify that we are not corrupting or causing issues when serializing on a
1340            // client/server and then trying to deserialize on the other side. This has happened
1341            // enough times with minor changes that we need tests to verify.
1342            let buf =
1343                rmp_serde::encode::to_vec_named(&Response::SearchStarted { id: SearchId::MAX })
1344                    .unwrap();
1345
1346            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
1347            assert_eq!(payload, Response::SearchStarted { id: SearchId::MAX });
1348        }
1349    }
1350
1351    mod search_results {
1352        use std::path::PathBuf;
1353
1354        use super::*;
1355        use crate::common::{SearchQueryContentsMatch, SearchQueryMatch, SearchQuerySubmatch};
1356
1357        #[test]
1358        fn should_be_able_to_serialize_to_json() {
1359            let payload = Response::SearchResults {
1360                id: SearchId::MAX,
1361                matches: vec![SearchQueryMatch::Contents(SearchQueryContentsMatch {
1362                    path: PathBuf::from("path"),
1363                    lines: "some lines".into(),
1364                    line_number: u64::MAX,
1365                    absolute_offset: u64::MAX,
1366                    submatches: vec![SearchQuerySubmatch::new("text", u64::MAX, u64::MAX)],
1367                })],
1368            };
1369
1370            let value = serde_json::to_value(payload).unwrap();
1371            assert_eq!(
1372                value,
1373                serde_json::json!({
1374                    "type": "search_results",
1375                    "id": SearchId::MAX,
1376                    "matches": [{
1377                        "type": "contents",
1378                        "path": "path",
1379                        "lines": "some lines",
1380                        "line_number": u64::MAX,
1381                        "absolute_offset": u64::MAX,
1382                        "submatches": [{
1383                            "match": "text",
1384                            "start": u64::MAX,
1385                            "end": u64::MAX,
1386                        }],
1387                    }],
1388                })
1389            );
1390        }
1391
1392        #[test]
1393        fn should_be_able_to_deserialize_from_json() {
1394            let value = serde_json::json!({
1395                "type": "search_results",
1396                "id": SearchId::MAX,
1397                "matches": [{
1398                    "type": "contents",
1399                    "path": "path",
1400                    "lines": "some lines",
1401                    "line_number": u64::MAX,
1402                    "absolute_offset": u64::MAX,
1403                    "submatches": [{
1404                        "match": "text",
1405                        "start": u64::MAX,
1406                        "end": u64::MAX,
1407                    }],
1408                }],
1409            });
1410
1411            let payload: Response = serde_json::from_value(value).unwrap();
1412            assert_eq!(
1413                payload,
1414                Response::SearchResults {
1415                    id: SearchId::MAX,
1416                    matches: vec![SearchQueryMatch::Contents(SearchQueryContentsMatch {
1417                        path: PathBuf::from("path"),
1418                        lines: "some lines".into(),
1419                        line_number: u64::MAX,
1420                        absolute_offset: u64::MAX,
1421                        submatches: vec![SearchQuerySubmatch::new("text", u64::MAX, u64::MAX)],
1422                    })],
1423                }
1424            );
1425        }
1426
1427        #[test]
1428        fn should_be_able_to_serialize_to_msgpack() {
1429            let payload = Response::SearchResults {
1430                id: SearchId::MAX,
1431                matches: vec![SearchQueryMatch::Contents(SearchQueryContentsMatch {
1432                    path: PathBuf::from("path"),
1433                    lines: "some lines".into(),
1434                    line_number: u64::MAX,
1435                    absolute_offset: u64::MAX,
1436                    submatches: vec![SearchQuerySubmatch::new("text", u64::MAX, u64::MAX)],
1437                })],
1438            };
1439
1440            // NOTE: We don't actually check the output here because it's an implementation detail
1441            // and could change as we change how serialization is results. This is merely to verify
1442            // that we can serialize since there are times when serde fails to serialize at
1443            // runtime.
1444            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
1445        }
1446
1447        #[test]
1448        fn should_be_able_to_deserialize_from_msgpack() {
1449            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
1450            // verify that we are not corrupting or causing issues when serializing on a
1451            // client/server and then trying to deserialize on the other side. This has happened
1452            // enough times with minor changes that we need tests to verify.
1453            let buf = rmp_serde::encode::to_vec_named(&Response::SearchResults {
1454                id: SearchId::MAX,
1455                matches: vec![SearchQueryMatch::Contents(SearchQueryContentsMatch {
1456                    path: PathBuf::from("path"),
1457                    lines: "some lines".into(),
1458                    line_number: u64::MAX,
1459                    absolute_offset: u64::MAX,
1460                    submatches: vec![SearchQuerySubmatch::new("text", u64::MAX, u64::MAX)],
1461                })],
1462            })
1463            .unwrap();
1464
1465            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
1466            assert_eq!(
1467                payload,
1468                Response::SearchResults {
1469                    id: SearchId::MAX,
1470                    matches: vec![SearchQueryMatch::Contents(SearchQueryContentsMatch {
1471                        path: PathBuf::from("path"),
1472                        lines: "some lines".into(),
1473                        line_number: u64::MAX,
1474                        absolute_offset: u64::MAX,
1475                        submatches: vec![SearchQuerySubmatch::new("text", u64::MAX, u64::MAX)],
1476                    })],
1477                }
1478            );
1479        }
1480    }
1481
1482    mod search_done {
1483        use super::*;
1484
1485        #[test]
1486        fn should_be_able_to_serialize_to_json() {
1487            let payload = Response::SearchDone { id: SearchId::MAX };
1488
1489            let value = serde_json::to_value(payload).unwrap();
1490            assert_eq!(
1491                value,
1492                serde_json::json!({
1493                    "type": "search_done",
1494                    "id": SearchId::MAX,
1495                })
1496            );
1497        }
1498
1499        #[test]
1500        fn should_be_able_to_deserialize_from_json() {
1501            let value = serde_json::json!({
1502                "type": "search_done",
1503                "id": SearchId::MAX,
1504            });
1505
1506            let payload: Response = serde_json::from_value(value).unwrap();
1507            assert_eq!(payload, Response::SearchDone { id: SearchId::MAX });
1508        }
1509
1510        #[test]
1511        fn should_be_able_to_serialize_to_msgpack() {
1512            let payload = Response::SearchDone { id: SearchId::MAX };
1513
1514            // NOTE: We don't actually check the output here because it's an implementation detail
1515            // and could change as we change how serialization is done. This is merely to verify
1516            // that we can serialize since there are times when serde fails to serialize at
1517            // runtime.
1518            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
1519        }
1520
1521        #[test]
1522        fn should_be_able_to_deserialize_from_msgpack() {
1523            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
1524            // verify that we are not corrupting or causing issues when serializing on a
1525            // client/server and then trying to deserialize on the other side. This has happened
1526            // enough times with minor changes that we need tests to verify.
1527            let buf = rmp_serde::encode::to_vec_named(&Response::SearchDone { id: SearchId::MAX })
1528                .unwrap();
1529
1530            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
1531            assert_eq!(payload, Response::SearchDone { id: SearchId::MAX });
1532        }
1533    }
1534
1535    mod proc_spawned {
1536        use super::*;
1537
1538        #[test]
1539        fn should_be_able_to_serialize_to_json() {
1540            let payload = Response::ProcSpawned { id: ProcessId::MAX };
1541
1542            let value = serde_json::to_value(payload).unwrap();
1543            assert_eq!(
1544                value,
1545                serde_json::json!({
1546                    "type": "proc_spawned",
1547                    "id": ProcessId::MAX,
1548                })
1549            );
1550        }
1551
1552        #[test]
1553        fn should_be_able_to_deserialize_from_json() {
1554            let value = serde_json::json!({
1555                "type": "proc_spawned",
1556                "id": ProcessId::MAX,
1557            });
1558
1559            let payload: Response = serde_json::from_value(value).unwrap();
1560            assert_eq!(payload, Response::ProcSpawned { id: ProcessId::MAX });
1561        }
1562
1563        #[test]
1564        fn should_be_able_to_serialize_to_msgpack() {
1565            let payload = Response::ProcSpawned { id: ProcessId::MAX };
1566
1567            // NOTE: We don't actually check the output here because it's an implementation detail
1568            // and could change as we change how serialization is done. This is merely to verify
1569            // that we can serialize since there are times when serde fails to serialize at
1570            // runtime.
1571            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
1572        }
1573
1574        #[test]
1575        fn should_be_able_to_deserialize_from_msgpack() {
1576            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
1577            // verify that we are not corrupting or causing issues when serializing on a
1578            // client/server and then trying to deserialize on the other side. This has happened
1579            // enough times with minor changes that we need tests to verify.
1580            let buf =
1581                rmp_serde::encode::to_vec_named(&Response::ProcSpawned { id: ProcessId::MAX })
1582                    .unwrap();
1583
1584            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
1585            assert_eq!(payload, Response::ProcSpawned { id: ProcessId::MAX });
1586        }
1587    }
1588
1589    mod proc_stdout {
1590        use super::*;
1591
1592        #[test]
1593        fn should_be_able_to_serialize_to_json() {
1594            let payload = Response::ProcStdout {
1595                id: ProcessId::MAX,
1596                data: vec![0, 1, 2, u8::MAX],
1597            };
1598
1599            let value = serde_json::to_value(payload).unwrap();
1600            assert_eq!(
1601                value,
1602                serde_json::json!({
1603                    "type": "proc_stdout",
1604                    "id": ProcessId::MAX,
1605                    "data": vec![0, 1, 2, u8::MAX],
1606                })
1607            );
1608        }
1609
1610        #[test]
1611        fn should_be_able_to_deserialize_from_json() {
1612            let value = serde_json::json!({
1613                "type": "proc_stdout",
1614                "id": ProcessId::MAX,
1615                "data": vec![0, 1, 2, u8::MAX],
1616            });
1617
1618            let payload: Response = serde_json::from_value(value).unwrap();
1619            assert_eq!(
1620                payload,
1621                Response::ProcStdout {
1622                    id: ProcessId::MAX,
1623                    data: vec![0, 1, 2, u8::MAX],
1624                }
1625            );
1626        }
1627
1628        #[test]
1629        fn should_be_able_to_serialize_to_msgpack() {
1630            let payload = Response::ProcStdout {
1631                id: ProcessId::MAX,
1632                data: vec![0, 1, 2, u8::MAX],
1633            };
1634
1635            // NOTE: We don't actually check the output here because it's an implementation detail
1636            // and could change as we change how serialization is done. This is merely to verify
1637            // that we can serialize since there are times when serde fails to serialize at
1638            // runtime.
1639            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
1640        }
1641
1642        #[test]
1643        fn should_be_able_to_deserialize_from_msgpack() {
1644            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
1645            // verify that we are not corrupting or causing issues when serializing on a
1646            // client/server and then trying to deserialize on the other side. This has happened
1647            // enough times with minor changes that we need tests to verify.
1648            let buf = rmp_serde::encode::to_vec_named(&Response::ProcStdout {
1649                id: ProcessId::MAX,
1650                data: vec![0, 1, 2, u8::MAX],
1651            })
1652            .unwrap();
1653
1654            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
1655            assert_eq!(
1656                payload,
1657                Response::ProcStdout {
1658                    id: ProcessId::MAX,
1659                    data: vec![0, 1, 2, u8::MAX],
1660                }
1661            );
1662        }
1663    }
1664
1665    mod proc_stderr {
1666        use super::*;
1667
1668        #[test]
1669        fn should_be_able_to_serialize_to_json() {
1670            let payload = Response::ProcStderr {
1671                id: ProcessId::MAX,
1672                data: vec![0, 1, 2, u8::MAX],
1673            };
1674
1675            let value = serde_json::to_value(payload).unwrap();
1676            assert_eq!(
1677                value,
1678                serde_json::json!({
1679                    "type": "proc_stderr",
1680                    "id": ProcessId::MAX,
1681                    "data": vec![0, 1, 2, u8::MAX],
1682                })
1683            );
1684        }
1685
1686        #[test]
1687        fn should_be_able_to_deserialize_from_json() {
1688            let value = serde_json::json!({
1689                "type": "proc_stderr",
1690                "id": ProcessId::MAX,
1691                "data": vec![0, 1, 2, u8::MAX],
1692            });
1693
1694            let payload: Response = serde_json::from_value(value).unwrap();
1695            assert_eq!(
1696                payload,
1697                Response::ProcStderr {
1698                    id: ProcessId::MAX,
1699                    data: vec![0, 1, 2, u8::MAX],
1700                }
1701            );
1702        }
1703
1704        #[test]
1705        fn should_be_able_to_serialize_to_msgpack() {
1706            let payload = Response::ProcStderr {
1707                id: ProcessId::MAX,
1708                data: vec![0, 1, 2, u8::MAX],
1709            };
1710
1711            // NOTE: We don't actually check the errput here because it's an implementation detail
1712            // and could change as we change how serialization is done. This is merely to verify
1713            // that we can serialize since there are times when serde fails to serialize at
1714            // runtime.
1715            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
1716        }
1717
1718        #[test]
1719        fn should_be_able_to_deserialize_from_msgpack() {
1720            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
1721            // verify that we are not corrupting or causing issues when serializing on a
1722            // client/server and then trying to deserialize on the other side. This has happened
1723            // enough times with minor changes that we need tests to verify.
1724            let buf = rmp_serde::encode::to_vec_named(&Response::ProcStderr {
1725                id: ProcessId::MAX,
1726                data: vec![0, 1, 2, u8::MAX],
1727            })
1728            .unwrap();
1729
1730            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
1731            assert_eq!(
1732                payload,
1733                Response::ProcStderr {
1734                    id: ProcessId::MAX,
1735                    data: vec![0, 1, 2, u8::MAX],
1736                }
1737            );
1738        }
1739    }
1740
1741    mod proc_done {
1742        use super::*;
1743
1744        #[test]
1745        fn should_be_able_to_serialize_minimal_payload_to_json() {
1746            let payload = Response::ProcDone {
1747                id: ProcessId::MAX,
1748                success: false,
1749                code: None,
1750            };
1751
1752            let value = serde_json::to_value(payload).unwrap();
1753            assert_eq!(
1754                value,
1755                serde_json::json!({
1756                    "type": "proc_done",
1757                    "id": ProcessId::MAX,
1758                    "success": false,
1759                })
1760            );
1761        }
1762
1763        #[test]
1764        fn should_be_able_to_serialize_full_payload_to_json() {
1765            let payload = Response::ProcDone {
1766                id: ProcessId::MAX,
1767                success: true,
1768                code: Some(i32::MAX),
1769            };
1770
1771            let value = serde_json::to_value(payload).unwrap();
1772            assert_eq!(
1773                value,
1774                serde_json::json!({
1775                    "type": "proc_done",
1776                    "id": ProcessId::MAX,
1777                    "success": true,
1778                    "code": i32::MAX,
1779                })
1780            );
1781        }
1782
1783        #[test]
1784        fn should_be_able_to_deserialize_minimal_payload_from_json() {
1785            let value = serde_json::json!({
1786                "type": "proc_done",
1787                "id": ProcessId::MAX,
1788                "success": false,
1789            });
1790
1791            let payload: Response = serde_json::from_value(value).unwrap();
1792            assert_eq!(
1793                payload,
1794                Response::ProcDone {
1795                    id: ProcessId::MAX,
1796                    success: false,
1797                    code: None,
1798                }
1799            );
1800        }
1801
1802        #[test]
1803        fn should_be_able_to_deserialize_full_payload_from_json() {
1804            let value = serde_json::json!({
1805                "type": "proc_done",
1806                "id": ProcessId::MAX,
1807                "success": true,
1808                "code": i32::MAX,
1809            });
1810
1811            let payload: Response = serde_json::from_value(value).unwrap();
1812            assert_eq!(
1813                payload,
1814                Response::ProcDone {
1815                    id: ProcessId::MAX,
1816                    success: true,
1817                    code: Some(i32::MAX),
1818                }
1819            );
1820        }
1821
1822        #[test]
1823        fn should_be_able_to_serialize_minimal_payload_to_msgpack() {
1824            let payload = Response::ProcDone {
1825                id: ProcessId::MAX,
1826                success: false,
1827                code: None,
1828            };
1829
1830            // NOTE: We don't actually check the errput here because it's an implementation detail
1831            // and could change as we change how serialization is done. This is merely to verify
1832            // that we can serialize since there are times when serde fails to serialize at
1833            // runtime.
1834            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
1835        }
1836
1837        #[test]
1838        fn should_be_able_to_serialize_full_payload_to_msgpack() {
1839            let payload = Response::ProcDone {
1840                id: ProcessId::MAX,
1841                success: true,
1842                code: Some(i32::MAX),
1843            };
1844
1845            // NOTE: We don't actually check the errput here because it's an implementation detail
1846            // and could change as we change how serialization is done. This is merely to verify
1847            // that we can serialize since there are times when serde fails to serialize at
1848            // runtime.
1849            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
1850        }
1851
1852        #[test]
1853        fn should_be_able_to_deserialize_minimal_payload_from_msgpack() {
1854            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
1855            // verify that we are not corrupting or causing issues when serializing on a
1856            // client/server and then trying to deserialize on the other side. This has happened
1857            // enough times with minor changes that we need tests to verify.
1858            let buf = rmp_serde::encode::to_vec_named(&Response::ProcDone {
1859                id: ProcessId::MAX,
1860                success: false,
1861                code: None,
1862            })
1863            .unwrap();
1864
1865            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
1866            assert_eq!(
1867                payload,
1868                Response::ProcDone {
1869                    id: ProcessId::MAX,
1870                    success: false,
1871                    code: None,
1872                }
1873            );
1874        }
1875
1876        #[test]
1877        fn should_be_able_to_deserialize_full_payload_from_msgpack() {
1878            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
1879            // verify that we are not corrupting or causing issues when serializing on a
1880            // client/server and then trying to deserialize on the other side. This has happened
1881            // enough times with minor changes that we need tests to verify.
1882            let buf = rmp_serde::encode::to_vec_named(&Response::ProcDone {
1883                id: ProcessId::MAX,
1884                success: true,
1885                code: Some(i32::MAX),
1886            })
1887            .unwrap();
1888
1889            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
1890            assert_eq!(
1891                payload,
1892                Response::ProcDone {
1893                    id: ProcessId::MAX,
1894                    success: true,
1895                    code: Some(i32::MAX),
1896                }
1897            );
1898        }
1899    }
1900
1901    mod system_info {
1902        use std::path::PathBuf;
1903
1904        use super::*;
1905
1906        #[test]
1907        fn should_be_able_to_serialize_to_json() {
1908            let payload = Response::SystemInfo(SystemInfo {
1909                family: String::from("family"),
1910                os: String::from("os"),
1911                arch: String::from("arch"),
1912                current_dir: PathBuf::from("current-dir"),
1913                main_separator: '/',
1914                username: String::from("username"),
1915                shell: String::from("shell"),
1916            });
1917
1918            let value = serde_json::to_value(payload).unwrap();
1919            assert_eq!(
1920                value,
1921                serde_json::json!({
1922                    "type": "system_info",
1923                    "family": "family",
1924                    "os": "os",
1925                    "arch": "arch",
1926                    "current_dir": "current-dir",
1927                    "main_separator": '/',
1928                    "username": "username",
1929                    "shell": "shell",
1930                })
1931            );
1932        }
1933
1934        #[test]
1935        fn should_be_able_to_deserialize_from_json() {
1936            let value = serde_json::json!({
1937                "type": "system_info",
1938                "family": "family",
1939                "os": "os",
1940                "arch": "arch",
1941                "current_dir": "current-dir",
1942                "main_separator": '/',
1943                "username": "username",
1944                "shell": "shell",
1945            });
1946
1947            let payload: Response = serde_json::from_value(value).unwrap();
1948            assert_eq!(
1949                payload,
1950                Response::SystemInfo(SystemInfo {
1951                    family: String::from("family"),
1952                    os: String::from("os"),
1953                    arch: String::from("arch"),
1954                    current_dir: PathBuf::from("current-dir"),
1955                    main_separator: '/',
1956                    username: String::from("username"),
1957                    shell: String::from("shell"),
1958                })
1959            );
1960        }
1961
1962        #[test]
1963        fn should_be_able_to_serialize_to_msgpack() {
1964            let payload = Response::SystemInfo(SystemInfo {
1965                family: String::from("family"),
1966                os: String::from("os"),
1967                arch: String::from("arch"),
1968                current_dir: PathBuf::from("current-dir"),
1969                main_separator: '/',
1970                username: String::from("username"),
1971                shell: String::from("shell"),
1972            });
1973
1974            // NOTE: We don't actually check the errput here because it's an implementation detail
1975            // and could change as we change how serialization is done. This is merely to verify
1976            // that we can serialize since there are times when serde fails to serialize at
1977            // runtime.
1978            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
1979        }
1980
1981        #[test]
1982        fn should_be_able_to_deserialize_from_msgpack() {
1983            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
1984            // verify that we are not corrupting or causing issues when serializing on a
1985            // client/server and then trying to deserialize on the other side. This has happened
1986            // enough times with minor changes that we need tests to verify.
1987            let buf = rmp_serde::encode::to_vec_named(&Response::SystemInfo(SystemInfo {
1988                family: String::from("family"),
1989                os: String::from("os"),
1990                arch: String::from("arch"),
1991                current_dir: PathBuf::from("current-dir"),
1992                main_separator: '/',
1993                username: String::from("username"),
1994                shell: String::from("shell"),
1995            }))
1996            .unwrap();
1997
1998            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
1999            assert_eq!(
2000                payload,
2001                Response::SystemInfo(SystemInfo {
2002                    family: String::from("family"),
2003                    os: String::from("os"),
2004                    arch: String::from("arch"),
2005                    current_dir: PathBuf::from("current-dir"),
2006                    main_separator: '/',
2007                    username: String::from("username"),
2008                    shell: String::from("shell"),
2009                })
2010            );
2011        }
2012    }
2013
2014    mod version {
2015        use super::*;
2016        use crate::semver::Version as SemVer;
2017
2018        #[test]
2019        fn should_be_able_to_serialize_to_json() {
2020            let payload = Response::Version(Version {
2021                server_version: "123.456.789-rc+build".parse().unwrap(),
2022                protocol_version: SemVer::new(1, 2, 3),
2023                capabilities: vec![String::from("cap")],
2024            });
2025
2026            let value = serde_json::to_value(payload).unwrap();
2027            assert_eq!(
2028                value,
2029                serde_json::json!({
2030                    "type": "version",
2031                    "server_version": "123.456.789-rc+build",
2032                    "protocol_version": "1.2.3",
2033                    "capabilities": ["cap"],
2034                })
2035            );
2036        }
2037
2038        #[test]
2039        fn should_be_able_to_deserialize_from_json() {
2040            let value = serde_json::json!({
2041                "type": "version",
2042                "server_version": "123.456.789-rc+build",
2043                "protocol_version": "1.2.3",
2044                "capabilities": ["cap"],
2045            });
2046
2047            let payload: Response = serde_json::from_value(value).unwrap();
2048            assert_eq!(
2049                payload,
2050                Response::Version(Version {
2051                    server_version: "123.456.789-rc+build".parse().unwrap(),
2052                    protocol_version: SemVer::new(1, 2, 3),
2053                    capabilities: vec![String::from("cap")],
2054                })
2055            );
2056        }
2057
2058        #[test]
2059        fn should_be_able_to_serialize_to_msgpack() {
2060            let payload = Response::Version(Version {
2061                server_version: "123.456.789-rc+build".parse().unwrap(),
2062                protocol_version: SemVer::new(1, 2, 3),
2063                capabilities: vec![String::from("cap")],
2064            });
2065
2066            // NOTE: We don't actually check the errput here because it's an implementation detail
2067            // and could change as we change how serialization is done. This is merely to verify
2068            // that we can serialize since there are times when serde fails to serialize at
2069            // runtime.
2070            let _ = rmp_serde::encode::to_vec_named(&payload).unwrap();
2071        }
2072
2073        #[test]
2074        fn should_be_able_to_deserialize_from_msgpack() {
2075            // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
2076            // verify that we are not corrupting or causing issues when serializing on a
2077            // client/server and then trying to deserialize on the other side. This has happened
2078            // enough times with minor changes that we need tests to verify.
2079            let buf = rmp_serde::encode::to_vec_named(&Response::Version(Version {
2080                server_version: "123.456.789-rc+build".parse().unwrap(),
2081                protocol_version: SemVer::new(1, 2, 3),
2082                capabilities: vec![String::from("cap")],
2083            }))
2084            .unwrap();
2085
2086            let payload: Response = rmp_serde::decode::from_slice(&buf).unwrap();
2087            assert_eq!(
2088                payload,
2089                Response::Version(Version {
2090                    server_version: "123.456.789-rc+build".parse().unwrap(),
2091                    protocol_version: SemVer::new(1, 2, 3),
2092                    capabilities: vec![String::from("cap")],
2093                })
2094            );
2095        }
2096    }
2097}