kdeconnect-proto 0.1.0

A pure Rust modular implementation of the KDE Connect protocol
Documentation
//! The Battery plugin allows a device to expose the status of its battery.
use serde::{Deserialize, Serialize};

use crate::packet::{NetworkPacket, NetworkPacketBody};

/// This packet is a battery status update.
///
/// <https://invent.kde.org/network/kdeconnect-meta/blob/master/protocol.md#kdeconnectbattery>
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct BatteryPacket {
    /// The current battery percentage, typically between `0` and `100`. If the value is `-1`,
    /// the device should be treated as though it has no battery.
    pub current_charge: i8,

    /// A boolean value indicating whether the battery is currently charging.
    pub is_charging: bool,

    /// Either `1` if the battery is below the threshold level, `0` otherwise.
    /// The threshold is chosen arbitrarily by the reporting device.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub threshold_event: Option<u8>,
}

impl NetworkPacket {
    /// Make a [`NetworkPacket`] with a [`BatteryPacket`] payload.
    pub fn battery(current_charge: i8, is_charging: bool) -> Self {
        NetworkPacket::new(NetworkPacketBody::Battery(BatteryPacket {
            current_charge,
            is_charging,
            threshold_event: None,
        }))
    }

    /// Make a [`NetworkPacket`] with a [`BatteryPacket`] payload and a threshold event.
    pub fn battery_with_threshold(
        current_charge: i8,
        is_charging: bool,
        threshold_event: u8,
    ) -> Self {
        NetworkPacket::new(NetworkPacketBody::Battery(BatteryPacket {
            current_charge,
            is_charging,
            threshold_event: Some(threshold_event),
        }))
    }
}