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
//! The RunCommand plugin allows defining and executing remote commands.
use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
#[cfg(not(feature = "std"))]
use alloc::string::String;
use crate::packet::{NetworkPacket, NetworkPacketBody};
/// A value of the map in the list of commands of the [`RunCommandPacket::command_list`] field.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RunCommandValue {
/// A displayed name for the command.
pub name: String,
/// The command to execute.
pub command: String,
}
/// This packet is a list of available commands.
///
/// <https://invent.kde.org/network/kdeconnect-meta/blob/master/protocol.md#kdeconnectruncommand>
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RunCommandPacket {
/// A JSON-serialized dictionary of commands that the device offers. The key is sent in a
/// kdeconnect.runcommand.request packet to execute its corresponding command. Each value has
/// a name field and a command field, e.g
/// `{ "command-id1": { "name": "Command", "command": "/path/to/command1" } }`.
pub command_list: String,
}
/// This packet is a runcommand status update.
///
/// <https://invent.kde.org/network/kdeconnect-meta/blob/master/protocol.md#kdeconnectruncommandrequest>
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RunCommandRequestPacket {
/// If the packet body contains this field it is a request to execute the command with the ID key.
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub key: Option<String>,
/// If the packet body contains this field it is a request for the command list.
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub request_command_list: Option<bool>,
}
impl NetworkPacket {
/// Make a [`NetworkPacket`] with a [`RunCommandPacket`] payload listing all available commands.
pub fn run_command_list_response(command_list: &HashMap<String, RunCommandValue>) -> Self {
NetworkPacket::new(NetworkPacketBody::RunCommand(RunCommandPacket {
command_list: serde_json::to_string(command_list).expect("malformed command list JSON"),
}))
}
/// Make a [`NetworkPacket`] with a [`RunCommandRequestPacket`] payload ready to request the
/// list of commands.
pub fn run_command_request_list() -> Self {
NetworkPacket::new(NetworkPacketBody::RunCommandRequest(RunCommandRequestPacket {
key: None,
request_command_list: Some(true),
}))
}
/// Make a [`NetworkPacket`] with a [`RunCommandRequestPacket`] payload asking to execute the
/// provided command.
pub fn run_command_exec<T: Into<String>>(key: T) -> Self {
NetworkPacket::new(NetworkPacketBody::RunCommandRequest(RunCommandRequestPacket {
key: Some(key.into()),
request_command_list: None,
}))
}
}