Skip to main content

kdeconnect_proto/packet/
ping.rs

1//! The Ping plugin allows sending and receiving simple 'pings', with an optional message. Usually these are displayed as notifications.
2use serde::{Deserialize, Serialize};
3
4#[cfg(not(feature = "std"))]
5use alloc::string::String;
6
7use crate::packet::{NetworkPacket, NetworkPacketBody};
8
9/// This packet is a ping request.
10///
11/// <https://invent.kde.org/network/kdeconnect-meta/blob/master/protocol.md#kdeconnectping>
12#[derive(Serialize, Deserialize, Debug, Clone)]
13#[serde(rename_all = "camelCase")]
14pub struct PingPacket {
15    /// An optional message.
16    #[serde(skip_serializing_if = "Option::is_none")]
17    #[serde(default)]
18    pub message: Option<String>,
19}
20
21impl NetworkPacket {
22    /// Make a [`NetworkPacket`] with a [`PingPacket`] payload.
23    pub fn ping<T: Into<String>>(message: T) -> Self {
24        NetworkPacket::new(NetworkPacketBody::Ping(PingPacket {
25            message: Some(message.into()),
26        }))
27    }
28
29    /// Make a [`NetworkPacket`] with a [`PingPacket`] payload and no message.
30    pub fn ping_empty_msg() -> Self {
31        NetworkPacket::new(NetworkPacketBody::Ping(PingPacket { message: None }))
32    }
33}