switchkit 0.2.0

Vendor-neutral abstraction for smart-plug devices (Shelly, Tasmota).
Documentation
use serde::{Deserialize, Serialize};

/// The device's vendor. Non-exhaustive: new vendors are added over time and this
/// must not become a breaking change for downstream `match` expressions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Vendor {
    Tasmota,
    Shelly,
}

impl Vendor {
    pub fn as_str(&self) -> &'static str {
        match self {
            Vendor::Tasmota => "tasmota",
            Vendor::Shelly => "shelly",
        }
    }
}

/// Per-device credentials, vendor-neutral (user + password covers Tasmota WebPassword
/// and Shelly digest/basic auth). Server-side only; never rendered to a browser.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeviceCredentials {
    pub user: String,
    pub password: String,
}

/// Everything a core needs to reach one physical device.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeviceTarget {
    pub host: String,
    pub credentials: Option<DeviceCredentials>,
}

impl DeviceTarget {
    pub fn new(host: impl Into<String>) -> Self {
        DeviceTarget {
            host: host.into(),
            credentials: None,
        }
    }
    pub fn with_credentials(mut self, credentials: Option<DeviceCredentials>) -> Self {
        self.credentials = credentials;
        self
    }
}