diff --git a/protocol/src/light.rs b/protocol/src/light.rs index 0e795c8..f9ac785 100644 --- a/protocol/src/light.rs +++ b/protocol/src/light.rs @@ -1,7 +1,6 @@ use std::{error::Error, future::Future}; use deranged::RangedU16; -use palette::Oklch; use snafu::{ResultExt, Snafu}; #[derive( @@ -43,14 +42,14 @@ pub trait GetState { fn get_state(&self) -> impl Future> + Send; } -#[ext_trait::extension(trait IsOff)] +#[ext_trait::extension(pub trait IsOff)] impl T { async fn is_off(&self) -> Result { Ok(self.get_state().await?.is_off()) } } -#[ext_trait::extension(trait IsOn)] +#[ext_trait::extension(pub trait IsOn)] impl T { async fn is_on(&self) -> Result { Ok(self.get_state().await?.is_on()) @@ -62,36 +61,39 @@ pub trait SetState { fn set_state(&mut self, state: State) -> impl Future> + Send; } -#[ext_trait::extension(trait TurnOff)] +#[ext_trait::extension(pub trait TurnOff)] impl T { async fn turn_off(&mut self) -> Result<(), T::Error> { self.set_state(State::Off).await } } -#[ext_trait::extension(trait TurnOn)] +#[ext_trait::extension(pub trait TurnOn)] impl T { async fn turn_on(&mut self) -> Result<(), T::Error> { self.set_state(State::On).await } } +pub trait Toggle { + type Error: Error; + fn toggle(&mut self) -> impl Future> + Send; +} + #[derive(Debug, Clone, Snafu)] -enum InvertToToggleError { +pub enum InvertToToggleError { GetStateError { source: GetStateError }, SetStateError { source: SetStateError }, } -#[ext_trait::extension(trait InvertToToggle)] -impl T +impl Toggle for T where ::Error: 'static, ::Error: 'static, { + type Error = InvertToToggleError<::Error, ::Error>; /// Toggle the light by setting it to the inverse of its current state - async fn toggle( - &mut self, - ) -> Result<(), InvertToToggleError<::Error, ::Error>> { + async fn toggle(&mut self) -> Result<(), Self::Error> { let state = self.get_state().await.context(GetStateSnafu)?; self.set_state(state.invert()) .await @@ -101,26 +103,22 @@ where } } -pub trait Toggle { - type Error: Error; - fn toggle(&mut self, state: State) -> impl Future> + Send; -} - -#[derive(Debug, Clone, Copy, derive_more::From, derive_more::Into)] -pub struct Kelvin(pub RangedU16<2000, 10000>); +pub type Kelvin = RangedU16<2000, 10000>; pub trait TurnToTemperature { - type TurnToTemperatureError: Error; + type Error: Error; fn turn_to_temperature( &mut self, temperature: Kelvin, - ) -> impl Future> + Send; + ) -> impl Future> + Send; } +pub type Oklch = palette::Oklch; + pub trait TurnToColor { - type TurnToColorError: Error; + type Error: Error; fn turn_to_color( &mut self, - color: Oklch, - ) -> impl Future> + Send; + color: Oklch, + ) -> impl Future> + Send; }