From 4b95734faffb4608d9c1b8f2b338a9f17c78433e Mon Sep 17 00:00:00 2001 From: Simon Johnston Date: Tue, 18 Aug 2020 12:32:02 -0700 Subject: [PATCH] Removed the `DeviceIdentifier` trait, and `Device` now returns a String. --- Cargo.toml | 2 +- README.md | 12 ++++++++++-- src/lib.rs | 16 +++++++--------- src/usb_hid.rs | 29 ++++++----------------------- src/webhook.rs | 47 +++++++++++------------------------------------ 5 files changed, 35 insertions(+), 71 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1003b77..e22ccb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "luxafor" description = "Library, and CLI, for Luxafor lights via either USB or webhooks." -version = "0.2.0" +version = "0.2.1" authors = ["Simon Johnston "] repository = "https://github.com/johnstonskj/rust-luxafor" documentation = "https://docs.rs/luxafor/0.1.0/luxafor/" diff --git a/README.md b/README.md index 11ee1d3..38182af 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,12 @@ Library, and CLI, for [Luxafor](https://luxafor.com/products/) lights via either ![MIT License](https://img.shields.io/badge/license-mit-118811.svg) [![GitHub stars](https://img.shields.io/github/stars/johnstonskj/rust-luxafor.svg)](https://github.com/johnstonskj/rust-luxafor/stargazers) -This has been tested with the USB connected [flag](https://luxafor.com/flag-usb-busylight-availability-indicator/) -as well as the [Bluetooth](https://luxafor.com/bluetooth-busy-light-availability-indicator/) lights. +The main entry point for clients is the trait `Device` that has implementations for USB connected devices such as the +[flag](https://luxafor.com/flag-usb-busylight-availability-indicator/) as well as webhooks for both the flag and +[bluetooth](https://luxafor.com/bluetooth-busy-light-availability-indicator/) lights. + +Each connection has its own discovery or connection methods but will provide a `Device` implementation +for the manipulation of the light state. ## API Examples @@ -76,6 +80,10 @@ The following shows the command line tool turning the light off. ## Changes +**Version 0.2.1** + +* Removed the `DeviceIdentifier` trait, and `Device` now returns a String. + **Version 0.2.0** * Refactored to provide a new `Device` trait diff --git a/src/lib.rs b/src/lib.rs index f2f2704..477116f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,13 @@ /*! Library, and CLI, for [Luxafor](https://luxafor.com/products/) lights via either USB or webhooks. -This has been -tested with the USB connected [flag](https://luxafor.com/flag-usb-busylight-availability-indicator/) -as well as the [Bluetooth](https://luxafor.com/bluetooth-busy-light-availability-indicator/) lights. +The main entry point for clients is the trait [Device](trait.Device.html) that has implementations +for USB connected devices such as the [flag](https://luxafor.com/flag-usb-busylight-availability-indicator/) +as well as webhooks for both the flag and [bluetooth](https://luxafor.com/bluetooth-busy-light-availability-indicator/) +lights. +Each connection has its own discovery or connection methods but will provide a `Device` implementation +for the manipulation of the light state. # API Examples @@ -160,11 +163,6 @@ pub enum Pattern { Synthetic, } -/// -/// Trait describing a device identifier, basically you just need to be able to `to_string()` it. -/// -pub trait DeviceIdentifier: Display {} - /// /// A trait implemented by different access methods to control a light. /// @@ -172,7 +170,7 @@ pub trait Device { /// /// Return the identifier for the device. /// - fn id(&self) -> &dyn DeviceIdentifier; + fn id(&self) -> String; /// /// Turn the light off. diff --git a/src/usb_hid.rs b/src/usb_hid.rs index 2fe9718..36ad900 100644 --- a/src/usb_hid.rs +++ b/src/usb_hid.rs @@ -2,9 +2,8 @@ Implementation of the Device trait for USB connected lights. */ -use crate::{Device, DeviceIdentifier, Pattern, SolidColor}; +use crate::{Device, Pattern, SolidColor}; use hidapi::{HidApi, HidDevice}; -use std::fmt::{Display, Formatter}; // ------------------------------------------------------------------------------------------------ // Public Types @@ -18,19 +17,13 @@ pub struct USBDeviceDiscovery { hid_api: HidApi, } -/// -/// The device identifier for a USB connected light. -/// -#[derive(Clone, Debug)] -pub struct USBDeviceID(String); - /// /// The device implementation for a USB connected light. /// #[allow(missing_debug_implementations)] pub struct USBDevice<'a> { hid_device: HidDevice<'a>, - id: USBDeviceID, + id: String, } // ------------------------------------------------------------------------------------------------ @@ -83,16 +76,6 @@ const PATTERN_RAINBOW_WAVE: u8 = 8; // Implementations // ------------------------------------------------------------------------------------------------ -impl Display for USBDeviceID { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} - -impl DeviceIdentifier for USBDeviceID {} - -// ------------------------------------------------------------------------------------------------ - impl USBDeviceDiscovery { /// /// Construct a new discovery object, this initializes the USB HID interface and thus can fail. @@ -120,8 +103,8 @@ impl USBDeviceDiscovery { // ------------------------------------------------------------------------------------------------ impl<'a> Device for USBDevice<'a> { - fn id(&self) -> &dyn DeviceIdentifier { - &self.id + fn id(&self) -> String { + self.id.clone() } fn turn_off(&self) -> crate::error::Result<()> { @@ -189,7 +172,7 @@ impl<'a> Device for USBDevice<'a> { impl<'a> USBDevice<'a> { fn new(hid_device: HidDevice<'a>) -> crate::error::Result> { - let id = USBDeviceID(format!( + let id = format!( "{}::{}::{}", hid_device .get_manufacturer_string() @@ -200,7 +183,7 @@ impl<'a> USBDevice<'a> { hid_device .get_serial_number_string() .unwrap_or("".to_string()) - )); + ); Ok(Self { hid_device, id }) } } diff --git a/src/webhook.rs b/src/webhook.rs index 5443eee..2ede868 100644 --- a/src/webhook.rs +++ b/src/webhook.rs @@ -3,27 +3,19 @@ Implementation of the Device trait for webhook connected lights. */ -use crate::{Device, DeviceIdentifier, Pattern, SolidColor}; +use crate::{Device, Pattern, SolidColor}; use reqwest::blocking::Client; -use std::fmt::{Display, Formatter}; -use std::str::FromStr; // ------------------------------------------------------------------------------------------------ // Public Types // ------------------------------------------------------------------------------------------------ -/// -/// The device identifier for a webhook connected light. -/// -#[derive(Clone, Debug)] -pub struct WebhookDeviceID(String); - /// /// The device implementation for a webhook connected light. /// #[derive(Clone, Debug)] pub struct WebhookDevice { - id: WebhookDeviceID, + id: String, } // ------------------------------------------------------------------------------------------------ @@ -34,8 +26,13 @@ pub struct WebhookDevice { /// Return a device implementation for a webhook connected light. /// pub fn new_device_for(device_id: &str) -> crate::error::Result { - let device_id = WebhookDeviceID::from_str(device_id)?; - Ok(WebhookDevice { id: device_id }) + if !device_id.is_empty() && device_id.chars().all(|c| c.is_ascii_hexdigit()) { + Ok(WebhookDevice { + id: device_id.to_string(), + }) + } else { + Err(crate::error::ErrorKind::InvalidDeviceID.into()) + } } // ------------------------------------------------------------------------------------------------ @@ -48,31 +45,9 @@ const API_V1: &str = "https://api.luxafor.com/webhook/v1/actions"; // Implementations // ------------------------------------------------------------------------------------------------ -impl Display for WebhookDeviceID { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} - -impl DeviceIdentifier for WebhookDeviceID {} - -impl FromStr for WebhookDeviceID { - type Err = crate::error::Error; - - fn from_str(s: &str) -> Result { - if !s.is_empty() && s.chars().all(|c| c.is_ascii_hexdigit()) { - Ok(Self(s.to_string())) - } else { - Err(crate::error::ErrorKind::InvalidDeviceID.into()) - } - } -} - -// ------------------------------------------------------------------------------------------------ - impl Device for WebhookDevice { - fn id(&self) -> &dyn DeviceIdentifier { - &self.id + fn id(&self) -> String { + self.id.clone() } fn turn_off(&self) -> crate::error::Result<()> {