rmux-proto 0.1.0

RMUX detached IPC protocol DTOs, framing, and wire-safe error types.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
//! Detached response contracts.

use serde::{Deserialize, Serialize};

use crate::{ControlModeResponse, HandshakeResponse, LayoutName, RmuxError, SdkWaitId};

#[path = "response/session.rs"]
mod session;
pub use session::{
    HasSessionResponse, KillSessionResponse, ListSessionsResponse, NewSessionResponse,
    RenameSessionResponse,
};

#[path = "response/server.rs"]
mod server;
pub use server::{
    KillServerResponse, LockClientResponse, LockServerResponse, LockSessionResponse,
    ServerAccessResponse,
};

#[path = "response/target.rs"]
mod target;
pub use target::ResolveTargetResponse;

#[path = "response/window.rs"]
mod window;
pub use window::{
    KillWindowResponse, LastWindowResponse, LinkWindowResponse, ListWindowsResponse,
    MoveWindowResponse, NewWindowResponse, NextWindowResponse, PreviousWindowResponse,
    RenameWindowResponse, ResizeWindowResponse, RespawnWindowResponse, RotateWindowResponse,
    SelectWindowResponse, SwapWindowResponse, UnlinkWindowResponse, WindowListEntry,
};

#[path = "response/pane.rs"]
mod pane;
pub use pane::{
    BreakPaneResponse, DisplayPanesResponse, JoinPaneResponse, KillPaneResponse, LastPaneResponse,
    ListPanesResponse, MovePaneResponse, PaneOutputCursor, PaneOutputCursorResponse,
    PaneOutputEvent, PaneOutputLagNotice, PaneOutputLagResponse, PaneRecentOutput,
    PaneSnapshotCell, PaneSnapshotCursor, PaneSnapshotResponse, PipePaneResponse,
    ResizePaneResponse, RespawnPaneResponse, SelectPaneResponse, SendKeysResponse,
    SplitWindowResponse, SubscribePaneOutputResponse, SwapPaneResponse,
    UnsubscribePaneOutputResponse,
};

#[path = "response/client.rs"]
mod client;
pub use client::{
    AttachSessionResponse, DetachClientResponse, ListClientsResponse, RefreshClientResponse,
    SuspendClientResponse, SwitchClientResponse,
};

#[path = "response/keys.rs"]
mod keys;
pub use keys::{
    BindKeyResponse, ClockModeResponse, CopyModeResponse, ListKeysResponse, SendPrefixResponse,
    UnbindKeyResponse,
};

#[path = "response/options.rs"]
mod options;
pub use options::{
    SetEnvironmentResponse, SetHookResponse, SetOptionByNameResponse, SetOptionResponse,
    ShowEnvironmentResponse, ShowHooksResponse, ShowOptionsResponse,
};

/// All detached responses supported by the wire protocol.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Response {
    /// Success payload for `new-session`.
    NewSession(NewSessionResponse),
    /// Success payload for `has-session`.
    HasSession(HasSessionResponse),
    /// Success payload for `kill-session`.
    KillSession(KillSessionResponse),
    /// Success payload for `new-window`.
    NewWindow(NewWindowResponse),
    /// Success payload for `kill-window`.
    KillWindow(KillWindowResponse),
    /// Success payload for `select-window`.
    SelectWindow(SelectWindowResponse),
    /// Success payload for `rename-window`.
    RenameWindow(RenameWindowResponse),
    /// Success payload for `next-window`.
    NextWindow(NextWindowResponse),
    /// Success payload for `previous-window`.
    PreviousWindow(PreviousWindowResponse),
    /// Success payload for `last-window`.
    LastWindow(LastWindowResponse),
    /// Success payload for `list-windows`.
    ListWindows(ListWindowsResponse),
    /// Success payload for `move-window`.
    MoveWindow(MoveWindowResponse),
    /// Success payload for `swap-window`.
    SwapWindow(SwapWindowResponse),
    /// Success payload for `rotate-window`.
    RotateWindow(RotateWindowResponse),
    /// Success payload for `split-window`.
    SplitWindow(SplitWindowResponse),
    /// Success payload for `swap-pane`.
    SwapPane(SwapPaneResponse),
    /// Success payload for `last-pane`.
    LastPane(LastPaneResponse),
    /// Success payload for `join-pane`.
    JoinPane(JoinPaneResponse),
    /// Success payload for `break-pane`.
    BreakPane(BreakPaneResponse),
    /// Success payload for `kill-pane`.
    KillPane(KillPaneResponse),
    /// Success payload for `select-layout`.
    SelectLayout(SelectLayoutResponse),
    /// Success payload for `resize-pane`.
    ResizePane(ResizePaneResponse),
    /// Success payload for `display-panes`.
    DisplayPanes(DisplayPanesResponse),
    /// Success payload for `select-pane`.
    SelectPane(SelectPaneResponse),
    /// Success payload for `send-keys`.
    SendKeys(SendKeysResponse),
    /// Success payload for `attach-session`.
    AttachSession(AttachSessionResponse),
    /// Success payload for `switch-client`.
    SwitchClient(SwitchClientResponse),
    /// Success payload for `detach-client`.
    DetachClient(DetachClientResponse),
    /// Success payload for `set-option`.
    SetOption(SetOptionResponse),
    /// Success payload for `set-environment`.
    SetEnvironment(SetEnvironmentResponse),
    /// Success payload for `set-hook`.
    SetHook(SetHookResponse),
    /// Error payload surfaced as exit code `1` and stderr by clients.
    Error(ErrorResponse),
    /// Success payload for `next-layout`.
    NextLayout(NextLayoutResponse),
    /// Success payload for `previous-layout`.
    PreviousLayout(PreviousLayoutResponse),
    /// Success payload for `show-options`.
    ShowOptions(ShowOptionsResponse),
    /// Success payload for `show-environment`.
    ShowEnvironment(ShowEnvironmentResponse),
    /// Success payload for `set-buffer`.
    SetBuffer(SetBufferResponse),
    /// Success payload for `show-buffer`.
    ShowBuffer(ShowBufferResponse),
    /// Success payload for `paste-buffer`.
    PasteBuffer(PasteBufferResponse),
    /// Success payload for `list-buffers`.
    ListBuffers(ListBuffersResponse),
    /// Success payload for `delete-buffer`.
    DeleteBuffer(DeleteBufferResponse),
    /// Success payload for `load-buffer`.
    LoadBuffer(LoadBufferResponse),
    /// Success payload for `save-buffer`.
    SaveBuffer(SaveBufferResponse),
    /// Success payload for `capture-pane`.
    CapturePane(CapturePaneResponse),
    /// Success payload for `display-message`.
    DisplayMessage(DisplayMessageResponse),
    /// Success payload for `run-shell`.
    RunShell(RunShellResponse),
    /// Success payload for `if-shell`.
    IfShell(IfShellResponse),
    /// Success payload for `wait-for`.
    WaitFor(WaitForResponse),
    /// Success payload for `rename-session`.
    RenameSession(RenameSessionResponse),
    /// Success payload for `list-sessions`.
    ListSessions(ListSessionsResponse),
    /// Success payload for `list-panes`.
    ListPanes(ListPanesResponse),
    /// Success payload for `source-file`.
    SourceFile(SourceFileResponse),
    /// Success payload for string-based `set-option`.
    SetOptionByName(SetOptionByNameResponse),
    /// Success payload for `show-hooks`.
    ShowHooks(ShowHooksResponse),
    /// Success payload for `bind-key`.
    BindKey(BindKeyResponse),
    /// Success payload for `unbind-key`.
    UnbindKey(UnbindKeyResponse),
    /// Success payload for `list-keys`.
    ListKeys(ListKeysResponse),
    /// Success payload for `send-prefix`.
    SendPrefix(SendPrefixResponse),
    /// Success payload for `clear-history`.
    ClearHistory(ClearHistoryResponse),
    /// Success payload for `copy-mode`.
    CopyMode(CopyModeResponse),
    /// Success payload for detached control-mode upgrade.
    ControlMode(ControlModeResponse),
    /// Success payload for `clock-mode`.
    ClockMode(ClockModeResponse),
    /// Success payload for `show-messages`.
    ShowMessages(ShowMessagesResponse),
    /// Success payload for `kill-server`.
    KillServer(KillServerResponse),
    /// Success payload for `lock-server`.
    LockServer(LockServerResponse),
    /// Success payload for `lock-session`.
    LockSession(LockSessionResponse),
    /// Success payload for `lock-client`.
    LockClient(LockClientResponse),
    /// Success payload for `server-access`.
    ServerAccess(ServerAccessResponse),
    /// Success payload for `refresh-client`.
    RefreshClient(RefreshClientResponse),
    /// Success payload for `list-clients`.
    ListClients(ListClientsResponse),
    /// Success payload for `suspend-client`.
    SuspendClient(SuspendClientResponse),
    /// Success payload for `resize-window`.
    ResizeWindow(ResizeWindowResponse),
    /// Success payload for `respawn-window`.
    RespawnWindow(RespawnWindowResponse),
    /// Success payload for `move-pane`.
    MovePane(MovePaneResponse),
    /// Success payload for `pipe-pane`.
    PipePane(PipePaneResponse),
    /// Success payload for `respawn-pane`.
    RespawnPane(RespawnPaneResponse),
    /// Success payload for `link-window`.
    LinkWindow(LinkWindowResponse),
    /// Success payload for `unlink-window`.
    UnlinkWindow(UnlinkWindowResponse),
    /// Success payload for internal detached target resolution.
    ResolveTarget(ResolveTargetResponse),
    /// Success payload for SDK/daemon version and capability negotiation.
    Handshake(HandshakeResponse),
    /// Success payload for the daemon-backed pane snapshot endpoint.
    PaneSnapshot(PaneSnapshotResponse),
    /// Success payload for the daemon-backed pane output subscription endpoint.
    SubscribePaneOutput(SubscribePaneOutputResponse),
    /// Success payload for the daemon-backed pane output unsubscription endpoint.
    UnsubscribePaneOutput(UnsubscribePaneOutputResponse),
    /// Success payload for daemon-backed pane output cursor polling.
    PaneOutputCursor(PaneOutputCursorResponse),
    /// Lag notice for daemon-backed pane output cursor polling.
    PaneOutputLag(PaneOutputLagResponse),
    /// Success payload for a daemon-backed SDK byte wait.
    SdkWaitForOutput(SdkWaitForOutputResponse),
    /// Success payload for daemon-backed SDK wait cancellation.
    CancelSdkWait(CancelSdkWaitResponse),
}

impl Response {
    /// Returns the stable routing name for the response variant.
    ///
    /// `Error` is not tied to one command on the current wire and therefore
    /// reports the generic `error` tag.
    #[must_use]
    pub const fn command_name(&self) -> &'static str {
        match self {
            Self::NewSession(_) => "new-session",
            Self::HasSession(_) => "has-session",
            Self::KillSession(_) => "kill-session",
            Self::NewWindow(_) => "new-window",
            Self::KillWindow(_) => "kill-window",
            Self::SelectWindow(_) => "select-window",
            Self::RenameWindow(_) => "rename-window",
            Self::NextWindow(_) => "next-window",
            Self::PreviousWindow(_) => "previous-window",
            Self::LastWindow(_) => "last-window",
            Self::ListWindows(_) => "list-windows",
            Self::MoveWindow(_) => "move-window",
            Self::SwapWindow(_) => "swap-window",
            Self::RotateWindow(_) => "rotate-window",
            Self::SplitWindow(_) => "split-window",
            Self::SwapPane(_) => "swap-pane",
            Self::LastPane(_) => "last-pane",
            Self::JoinPane(_) => "join-pane",
            Self::BreakPane(_) => "break-pane",
            Self::KillPane(_) => "kill-pane",
            Self::SelectLayout(_) => "select-layout",
            Self::ResizePane(_) => "resize-pane",
            Self::DisplayPanes(_) => "display-panes",
            Self::SelectPane(_) => "select-pane",
            Self::SendKeys(_) => "send-keys",
            Self::AttachSession(_) => "attach-session",
            Self::SwitchClient(_) => "switch-client",
            Self::DetachClient(_) => "detach-client",
            Self::SetOption(_) | Self::SetOptionByName(_) => "set-option",
            Self::SetEnvironment(_) => "set-environment",
            Self::SetHook(_) => "set-hook",
            Self::Error(_) => "error",
            Self::NextLayout(_) => "next-layout",
            Self::PreviousLayout(_) => "previous-layout",
            Self::ShowOptions(_) => "show-options",
            Self::ShowEnvironment(_) => "show-environment",
            Self::SetBuffer(_) => "set-buffer",
            Self::ShowBuffer(_) => "show-buffer",
            Self::PasteBuffer(_) => "paste-buffer",
            Self::ListBuffers(_) => "list-buffers",
            Self::DeleteBuffer(_) => "delete-buffer",
            Self::LoadBuffer(_) => "load-buffer",
            Self::SaveBuffer(_) => "save-buffer",
            Self::CapturePane(_) => "capture-pane",
            Self::DisplayMessage(_) => "display-message",
            Self::RunShell(_) => "run-shell",
            Self::IfShell(_) => "if-shell",
            Self::WaitFor(_) => "wait-for",
            Self::RenameSession(_) => "rename-session",
            Self::ListSessions(_) => "list-sessions",
            Self::ListPanes(_) => "list-panes",
            Self::SourceFile(_) => "source-file",
            Self::ShowHooks(_) => "show-hooks",
            Self::BindKey(_) => "bind-key",
            Self::UnbindKey(_) => "unbind-key",
            Self::ListKeys(_) => "list-keys",
            Self::SendPrefix(_) => "send-prefix",
            Self::ClearHistory(_) => "clear-history",
            Self::CopyMode(_) => "copy-mode",
            Self::ControlMode(_) => "control-mode",
            Self::ClockMode(_) => "clock-mode",
            Self::ShowMessages(_) => "show-messages",
            Self::KillServer(_) => "kill-server",
            Self::LockServer(_) => "lock-server",
            Self::LockSession(_) => "lock-session",
            Self::LockClient(_) => "lock-client",
            Self::ServerAccess(_) => "server-access",
            Self::RefreshClient(_) => "refresh-client",
            Self::ListClients(_) => "list-clients",
            Self::SuspendClient(_) => "suspend-client",
            Self::ResizeWindow(_) => "resize-window",
            Self::RespawnWindow(_) => "respawn-window",
            Self::MovePane(_) => "move-pane",
            Self::PipePane(_) => "pipe-pane",
            Self::RespawnPane(_) => "respawn-pane",
            Self::PaneSnapshot(_) => "pane-snapshot",
            Self::SubscribePaneOutput(_) => "subscribe-pane-output",
            Self::UnsubscribePaneOutput(_) => "unsubscribe-pane-output",
            Self::PaneOutputCursor(_) => "pane-output-cursor",
            Self::PaneOutputLag(_) => "pane-output-lag",
            Self::SdkWaitForOutput(_) => "sdk-wait-output",
            Self::CancelSdkWait(_) => "cancel-sdk-wait",
            Self::LinkWindow(_) => "link-window",
            Self::UnlinkWindow(_) => "unlink-window",
            Self::ResolveTarget(_) => "resolve-target",
            Self::Handshake(_) => "handshake",
        }
    }

    /// Returns `true` when this response carries an error payload.
    #[must_use]
    pub const fn is_error(&self) -> bool {
        matches!(self, Self::Error(_))
    }

    /// Returns the shared stdout contract when this response carries printable output.
    #[must_use]
    pub fn command_output(&self) -> Option<&CommandOutput> {
        match self {
            Self::NewSession(response) => response.command_output(),
            Self::ListWindows(response) => Some(response.command_output()),
            Self::ShowOptions(response) => Some(response.command_output()),
            Self::ShowEnvironment(response) => Some(response.command_output()),
            Self::ShowHooks(response) => Some(response.command_output()),
            Self::ShowBuffer(response) => Some(response.command_output()),
            Self::ListBuffers(response) => Some(response.command_output()),
            Self::CapturePane(response) => response.command_output(),
            Self::DisplayMessage(response) => response.command_output(),
            Self::ResolveTarget(_) => None,
            Self::RunShell(response) => response.command_output(),
            Self::IfShell(response) => response.command_output(),
            Self::ListSessions(response) => Some(response.command_output()),
            Self::ListPanes(response) => Some(response.command_output()),
            Self::SourceFile(response) => response.command_output(),
            Self::SetOptionByName(_) => None,
            Self::ListKeys(response) => Some(response.command_output()),
            Self::ControlMode(_) => None,
            Self::ClockMode(_) => None,
            Self::ShowMessages(response) => Some(response.command_output()),
            Self::ServerAccess(response) => Some(response.command_output()),
            Self::ListClients(response) => Some(response.command_output()),
            Self::BreakPane(response) => response.command_output(),
            Self::Handshake(_) => None,
            Self::SubscribePaneOutput(_)
            | Self::UnsubscribePaneOutput(_)
            | Self::PaneOutputCursor(_)
            | Self::PaneOutputLag(_)
            | Self::SdkWaitForOutput(_)
            | Self::CancelSdkWait(_) => None,
            _ => None,
        }
    }
}

/// Reusable stdout payload for commands that produce printable output.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommandOutput {
    /// The exact bytes the client should write to stdout.
    pub stdout: Vec<u8>,
}

impl CommandOutput {
    /// Creates a stdout payload from raw bytes.
    #[must_use]
    pub fn from_stdout(stdout: impl Into<Vec<u8>>) -> Self {
        Self {
            stdout: stdout.into(),
        }
    }

    /// Returns the raw stdout bytes.
    #[must_use]
    pub fn stdout(&self) -> &[u8] {
        &self.stdout
    }
}

/// Response payload for `select-layout`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct SelectLayoutResponse {
    /// The applied layout.
    pub layout: LayoutName,
}

/// Response payload for `next-layout`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct NextLayoutResponse {
    /// The applied layout.
    pub layout: LayoutName,
}

/// Response payload for `previous-layout`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct PreviousLayoutResponse {
    /// The applied layout.
    pub layout: LayoutName,
}

/// Response payload for `set-buffer`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SetBufferResponse {
    /// The name of the created or replaced buffer.
    pub buffer_name: String,
}

/// Response payload for `show-buffer`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ShowBufferResponse {
    /// The pre-rendered stdout bytes for the CLI.
    pub output: CommandOutput,
}

impl ShowBufferResponse {
    /// Returns the reusable stdout payload.
    #[must_use]
    pub fn command_output(&self) -> &CommandOutput {
        &self.output
    }
}

/// Response payload for `paste-buffer`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PasteBufferResponse {
    /// The buffer name that was pasted.
    pub buffer_name: String,
}

/// Response payload for `list-buffers`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ListBuffersResponse {
    /// The pre-rendered stdout bytes for the CLI.
    pub output: CommandOutput,
}

impl ListBuffersResponse {
    /// Returns the reusable stdout payload.
    #[must_use]
    pub fn command_output(&self) -> &CommandOutput {
        &self.output
    }
}

/// Response payload for `delete-buffer`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeleteBufferResponse {
    /// The name of the deleted buffer.
    pub buffer_name: String,
}

/// Response payload for `load-buffer`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LoadBufferResponse {
    /// The name of the created or replaced buffer.
    pub buffer_name: String,
}

/// Response payload for `save-buffer`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SaveBufferResponse {
    /// The name of the saved buffer.
    pub buffer_name: String,
}

/// Response payload for `capture-pane`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CapturePaneResponse {
    /// The created/replaced buffer name for non-printing captures.
    pub buffer_name: Option<String>,
    /// Captured stdout for `capture-pane -p`.
    pub output: Option<CommandOutput>,
}

impl CapturePaneResponse {
    /// Builds a non-printing capture response.
    #[must_use]
    pub fn from_buffer(buffer_name: String) -> Self {
        Self {
            buffer_name: Some(buffer_name),
            output: None,
        }
    }

    /// Builds a printing capture response.
    #[must_use]
    pub fn from_output(output: CommandOutput) -> Self {
        Self {
            buffer_name: None,
            output: Some(output),
        }
    }

    /// Returns the reusable stdout payload when this was a printing capture.
    #[must_use]
    pub fn command_output(&self) -> Option<&CommandOutput> {
        self.output.as_ref()
    }
}

/// Response payload for `clear-history`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClearHistoryResponse;

/// Response payload for `show-messages`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ShowMessagesResponse {
    /// Expanded stdout for `show-messages`.
    pub output: CommandOutput,
}

impl ShowMessagesResponse {
    /// Builds a response from reusable command output bytes.
    #[must_use]
    pub const fn from_output(output: CommandOutput) -> Self {
        Self { output }
    }

    /// Returns the stdout payload for `show-messages`.
    #[must_use]
    pub const fn command_output(&self) -> &CommandOutput {
        &self.output
    }
}

/// Response payload for `display-message`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DisplayMessageResponse {
    /// Expanded stdout for `display-message -p`.
    pub output: Option<CommandOutput>,
}

impl DisplayMessageResponse {
    /// Builds a non-printing display response.
    #[must_use]
    pub const fn no_output() -> Self {
        Self { output: None }
    }

    /// Builds a printing display response.
    #[must_use]
    pub fn from_output(output: CommandOutput) -> Self {
        Self {
            output: Some(output),
        }
    }

    /// Returns the reusable stdout payload when this was a printing display.
    #[must_use]
    pub fn command_output(&self) -> Option<&CommandOutput> {
        self.output.as_ref()
    }
}

/// Response payload for `run-shell`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RunShellResponse {
    /// Captured stdout for foreground `run-shell`.
    pub output: Option<CommandOutput>,
}

impl RunShellResponse {
    /// Builds a background `run-shell` response.
    #[must_use]
    pub const fn background() -> Self {
        Self { output: None }
    }

    /// Builds a response with no command output.
    #[must_use]
    pub const fn no_output() -> Self {
        Self::background()
    }

    /// Builds a foreground `run-shell` response.
    #[must_use]
    pub fn from_output(output: CommandOutput) -> Self {
        Self {
            output: Some(output),
        }
    }

    /// Returns the reusable stdout payload for foreground `run-shell`.
    #[must_use]
    pub fn command_output(&self) -> Option<&CommandOutput> {
        self.output.as_ref()
    }
}

/// Response payload for `if-shell`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct IfShellResponse {
    /// Captured stdout from the selected nested command when it produced output.
    pub output: Option<CommandOutput>,
}

impl IfShellResponse {
    /// Builds a response with no nested command output.
    #[must_use]
    pub const fn no_output() -> Self {
        Self { output: None }
    }

    /// Builds a response with nested command output.
    #[must_use]
    pub fn from_output(output: CommandOutput) -> Self {
        Self {
            output: Some(output),
        }
    }

    /// Returns the reusable stdout payload when the selected nested command printed output.
    #[must_use]
    pub fn command_output(&self) -> Option<&CommandOutput> {
        self.output.as_ref()
    }
}

/// Response payload for `source-file`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SourceFileResponse {
    /// Verbose parsed-command output, when requested.
    pub output: Option<CommandOutput>,
}

impl SourceFileResponse {
    /// Builds a response without command output.
    #[must_use]
    pub const fn no_output() -> Self {
        Self { output: None }
    }

    /// Builds a response with command output.
    #[must_use]
    pub fn from_output(output: CommandOutput) -> Self {
        Self {
            output: Some(output),
        }
    }

    /// Returns the reusable stdout payload when `source-file -v` printed output.
    #[must_use]
    pub fn command_output(&self) -> Option<&CommandOutput> {
        self.output.as_ref()
    }
}

/// Response payload for `wait-for`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct WaitForResponse;

/// Terminal state for a daemon-backed SDK byte wait.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SdkWaitOutcome {
    /// The requested byte sequence was observed.
    Matched,
    /// The wait was cancelled by a best-effort SDK cancel request or
    /// connection cleanup.
    Cancelled,
}

/// Response payload for a daemon-backed SDK byte wait.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct SdkWaitForOutputResponse {
    /// Wait ID that completed.
    pub wait_id: SdkWaitId,
    /// Terminal wait outcome.
    pub outcome: SdkWaitOutcome,
}

/// Response payload for best-effort daemon-backed SDK wait cancellation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct CancelSdkWaitResponse {
    /// Wait ID named by the cancellation request.
    pub wait_id: SdkWaitId,
    /// Whether a live wait was removed by this request.
    pub removed: bool,
}

/// Error response payload for detached RPC failures.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ErrorResponse {
    /// The shared wire-safe error value.
    pub error: RmuxError,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{OptionScopeSelector, SetOptionMode};

    #[test]
    fn response_command_names_cover_base_aliases_and_error_tag() {
        assert_eq!(
            Response::HasSession(HasSessionResponse { exists: true }).command_name(),
            "has-session"
        );
        assert_eq!(
            Response::SetOptionByName(SetOptionByNameResponse {
                scope: OptionScopeSelector::ServerGlobal,
                name: "status".to_owned(),
                mode: SetOptionMode::Replace,
            })
            .command_name(),
            "set-option"
        );
        assert_eq!(
            Response::KillServer(KillServerResponse).command_name(),
            "kill-server"
        );
        assert_eq!(
            Response::Error(ErrorResponse {
                error: RmuxError::Server("failed".to_owned()),
            })
            .command_name(),
            "error"
        );
        assert_eq!(
            Response::Handshake(HandshakeResponse::current()).command_name(),
            "handshake"
        );
        assert_eq!(
            Response::SdkWaitForOutput(SdkWaitForOutputResponse {
                wait_id: SdkWaitId::new(1),
                outcome: SdkWaitOutcome::Matched,
            })
            .command_name(),
            "sdk-wait-output"
        );
        assert_eq!(
            Response::CancelSdkWait(CancelSdkWaitResponse {
                wait_id: SdkWaitId::new(1),
                removed: false,
            })
            .command_name(),
            "cancel-sdk-wait"
        );
        assert_eq!(
            Response::WaitFor(WaitForResponse).command_name(),
            "wait-for"
        );
    }
}