feat: Use PartialUserData on proxy type to add trait methods

This commit is contained in:
2025-10-11 02:05:35 +02:00
parent 745a1025bb
commit 006a561307
2 changed files with 45 additions and 49 deletions

View File

@@ -2,21 +2,28 @@ use std::ops::Deref;
// TODO: Enable and disable functions based on query_only and command_only // TODO: Enable and disable functions based on query_only and command_only
pub trait Device { pub trait PartialUserData<T> {
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M);
where }
Self: Sized + crate::device::Device + 'static,
{ pub struct Device;
impl<T> PartialUserData<T> for Device
where
T: crate::device::Device + 'static,
{
fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M) {
methods.add_async_method("get_id", async |_lua, this, _: ()| Ok(this.get_id())); methods.add_async_method("get_id", async |_lua, this, _: ()| Ok(this.get_id()));
} }
} }
impl<T> Device for T where T: crate::device::Device {}
pub trait OnOff { pub struct OnOff;
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
where impl<T> PartialUserData<T> for OnOff
Self: Sized + google_home::traits::OnOff + 'static, where
{ T: google_home::traits::OnOff + 'static,
{
fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M) {
methods.add_async_method("set_on", async |_lua, this, on: bool| { methods.add_async_method("set_on", async |_lua, this, on: bool| {
this.deref().set_on(on).await.unwrap(); this.deref().set_on(on).await.unwrap();
@@ -28,13 +35,14 @@ pub trait OnOff {
}); });
} }
} }
impl<T> OnOff for T where T: google_home::traits::OnOff {}
pub trait Brightness { pub struct Brightness;
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
where impl<T> PartialUserData<T> for Brightness
Self: Sized + google_home::traits::Brightness + 'static, where
{ T: google_home::traits::Brightness + 'static,
{
fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M) {
methods.add_async_method("set_brightness", async |_lua, this, brightness: u8| { methods.add_async_method("set_brightness", async |_lua, this, brightness: u8| {
this.set_brightness(brightness).await.unwrap(); this.set_brightness(brightness).await.unwrap();
@@ -46,13 +54,14 @@ pub trait Brightness {
}); });
} }
} }
impl<T> Brightness for T where T: google_home::traits::Brightness {}
pub trait ColorSetting { pub struct ColorSetting;
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
where impl<T> PartialUserData<T> for ColorSetting
Self: Sized + google_home::traits::ColorSetting + 'static, where
{ T: google_home::traits::ColorSetting + 'static,
{
fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M) {
methods.add_async_method( methods.add_async_method(
"set_color_temperature", "set_color_temperature",
async |_lua, this, temperature: u32| { async |_lua, this, temperature: u32| {
@@ -69,13 +78,14 @@ pub trait ColorSetting {
}); });
} }
} }
impl<T> ColorSetting for T where T: google_home::traits::ColorSetting {}
pub trait OpenClose { pub struct OpenClose;
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
where impl<T> PartialUserData<T> for OpenClose
Self: Sized + google_home::traits::OpenClose + 'static, where
{ T: google_home::traits::OpenClose + 'static,
{
fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M) {
methods.add_async_method("set_open_percent", async |_lua, this, open_percent: u8| { methods.add_async_method("set_open_percent", async |_lua, this, open_percent: u8| {
this.set_open_percent(open_percent).await.unwrap(); this.set_open_percent(open_percent).await.unwrap();
@@ -87,4 +97,3 @@ pub trait OpenClose {
}); });
} }
} }
impl<T> OpenClose for T where T: google_home::traits::OpenClose {}

View File

@@ -66,18 +66,6 @@ impl Parse for Traits {
} }
} }
impl ToTokens for Traits {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let Self(traits) = &self;
tokens.extend(quote! {
#(
::automation_lib::lua::traits::#traits::add_methods(methods);
)*
});
}
}
#[derive(Default)] #[derive(Default)]
struct Aliases(Vec<syn::Ident>); struct Aliases(Vec<syn::Ident>);
@@ -138,12 +126,9 @@ impl quote::ToTokens for Implementation {
traits, traits,
add_methods, add_methods,
} = &self; } = &self;
let Traits(traits) = traits;
let interfaces: String = traits let interfaces: String = traits.iter().map(|tr| format!(", Interface{tr}")).collect();
.0
.iter()
.map(|tr| format!(", Interface{tr}"))
.collect();
tokens.extend(quote! { tokens.extend(quote! {
impl mlua::UserData for #name { impl mlua::UserData for #name {
@@ -161,9 +146,11 @@ impl quote::ToTokens for Implementation {
Ok(b) Ok(b)
}); });
::automation_lib::lua::traits::Device::add_methods(methods); <::automation_lib::lua::traits::Device as ::automation_lib::lua::traits::PartialUserData<#name>>::add_methods(methods);
#traits #(
<::automation_lib::lua::traits::#traits as ::automation_lib::lua::traits::PartialUserData<#name>>::add_methods(methods);
)*
#( #(
#add_methods(methods); #add_methods(methods);