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
use super::{ChannelId, ConnectionId, Destination};
use crate::{DistantMsg, DistantRequestData, Map};
use derive_more::IsVariant;
use distant_net::Request;
use serde::{Deserialize, Serialize};
use strum::{AsRefStr, EnumDiscriminants, EnumIter, EnumMessage, EnumString};

#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, EnumDiscriminants, Serialize, Deserialize)]
#[cfg_attr(feature = "clap", derive(clap::Subcommand))]
#[strum_discriminants(derive(
    AsRefStr,
    strum::Display,
    EnumIter,
    EnumMessage,
    EnumString,
    Hash,
    PartialOrd,
    Ord,
    IsVariant,
    Serialize,
    Deserialize
))]
#[cfg_attr(
    feature = "schemars",
    strum_discriminants(derive(schemars::JsonSchema))
)]
#[strum_discriminants(name(ManagerCapabilityKind))]
#[strum_discriminants(strum(serialize_all = "snake_case"))]
#[serde(rename_all = "snake_case", deny_unknown_fields, tag = "type")]
pub enum ManagerRequest {
    /// Retrieve information about the server's capabilities
    #[strum_discriminants(strum(message = "Supports retrieving capabilities"))]
    Capabilities,

    /// Launch a server using the manager
    #[strum_discriminants(strum(message = "Supports launching distant on remote servers"))]
    Launch {
        // NOTE: Boxed per clippy's large_enum_variant warning
        destination: Box<Destination>,

        /// Additional options specific to the connection
        #[cfg_attr(feature = "clap", clap(short, long, action = clap::ArgAction::Append))]
        options: Map,
    },

    /// Initiate a connection through the manager
    #[strum_discriminants(strum(message = "Supports connecting to remote servers"))]
    Connect {
        // NOTE: Boxed per clippy's large_enum_variant warning
        destination: Box<Destination>,

        /// Additional options specific to the connection
        #[cfg_attr(feature = "clap", clap(short, long, action = clap::ArgAction::Append))]
        options: Map,
    },

    /// Opens a channel for communication with a server
    #[cfg_attr(feature = "clap", clap(skip))]
    #[strum_discriminants(strum(message = "Supports opening a channel with a remote server"))]
    OpenChannel {
        /// Id of the connection
        id: ConnectionId,
    },

    /// Sends data through channel
    #[cfg_attr(feature = "clap", clap(skip))]
    #[strum_discriminants(strum(
        message = "Supports sending data through a channel with a remote server"
    ))]
    Channel {
        /// Id of the channel
        id: ChannelId,

        /// Request to send to through the channel
        #[cfg_attr(feature = "clap", clap(skip = skipped_request()))]
        request: Request<DistantMsg<DistantRequestData>>,
    },

    /// Closes an open channel
    #[cfg_attr(feature = "clap", clap(skip))]
    #[strum_discriminants(strum(message = "Supports closing a channel with a remote server"))]
    CloseChannel {
        /// Id of the channel to close
        id: ChannelId,
    },

    /// Retrieve information about a specific connection
    #[strum_discriminants(strum(message = "Supports retrieving connection-specific information"))]
    Info { id: ConnectionId },

    /// Kill a specific connection
    #[strum_discriminants(strum(message = "Supports killing a remote connection"))]
    Kill { id: ConnectionId },

    /// Retrieve list of connections being managed
    #[strum_discriminants(strum(message = "Supports retrieving a list of managed connections"))]
    List,

    /// Signals the manager to shutdown
    #[strum_discriminants(strum(message = "Supports being shut down on demand"))]
    Shutdown,
}

/// Produces some default request, purely to satisfy clap
#[cfg(feature = "clap")]
fn skipped_request() -> Request<DistantMsg<DistantRequestData>> {
    Request::new(DistantMsg::Single(DistantRequestData::SystemInfo {}))
}