Improved Lua macro situation
All checks were successful
Build and deploy / Build application (push) Successful in 6m20s
Check / Run checks (push) Successful in 2m19s
Build and deploy / Build container (push) Successful in 1m16s
Build and deploy / Deploy container (push) Has been skipped

This commit is contained in:
Dreaded_X 2024-07-25 00:49:10 +02:00
parent 006320be18
commit 98ab265fed
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
16 changed files with 61 additions and 76 deletions

View File

@ -1,19 +1,10 @@
#![feature(let_chains)]
#![feature(iter_intersperse)]
mod lua_device;
mod lua_device_config;
use lua_device::impl_lua_device_macro;
use lua_device_config::impl_lua_device_config_macro;
use syn::{parse_macro_input, DeriveInput};
#[proc_macro_derive(LuaDevice, attributes(config))]
pub fn lua_device_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
impl_lua_device_macro(&ast).into()
}
#[proc_macro_derive(LuaDeviceConfig, attributes(device_config))]
pub fn lua_device_config_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast = parse_macro_input!(input as DeriveInput);

View File

@ -1,28 +0,0 @@
use proc_macro2::TokenStream;
use quote::quote;
use syn::DeriveInput;
pub fn impl_lua_device_macro(ast: &DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl #name {
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
lua.globals().set(stringify!(#name), lua.create_proxy::<#name>()?)
}
}
impl mlua::UserData for #name {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_function("new", |lua, config: mlua::Value| async {
let config = mlua::FromLua::from_lua(config, lua)?;
// TODO: Using crate:: could cause issues
let device: #name = crate::devices::LuaDeviceCreate::create(config).await.map_err(mlua::ExternalError::into_lua_err)?;
Ok(crate::device_manager::WrappedDevice::new(Box::new(device)))
});
}
}
};
gen
}

View File

@ -1,5 +1,5 @@
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use google_home::device::Name;
use google_home::errors::ErrorCode;
use google_home::traits::{
@ -27,7 +27,7 @@ pub struct AirFilterConfig {
pub client: WrappedAsyncClient,
}
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct AirFilter {
config: AirFilterConfig,

View File

@ -1,5 +1,5 @@
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use google_home::traits::OnOff;
use tracing::{debug, error, trace, warn};
@ -24,7 +24,7 @@ pub struct AudioSetupConfig {
pub client: WrappedAsyncClient,
}
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct AudioSetup {
config: AudioSetupConfig,
}

View File

@ -1,7 +1,7 @@
use std::time::Duration;
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use google_home::traits::OnOff;
use mlua::FromLua;
use tokio::task::JoinHandle;
@ -62,7 +62,7 @@ pub struct ContactSensorConfig {
pub client: WrappedAsyncClient,
}
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct ContactSensor {
config: ContactSensorConfig,

View File

@ -1,7 +1,7 @@
use std::convert::Infallible;
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use tracing::{trace, warn};
use super::LuaDeviceCreate;
@ -20,7 +20,7 @@ pub struct DebugBridgeConfig {
pub client: WrappedAsyncClient,
}
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct DebugBridge {
config: DebugBridgeConfig,
}

View File

@ -2,7 +2,7 @@ use std::convert::Infallible;
use std::net::SocketAddr;
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use serde::{Deserialize, Serialize};
use tracing::{error, trace, warn};
@ -31,7 +31,7 @@ pub struct HueBridgeConfig {
pub flags: FlagIDs,
}
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct HueBridge {
config: HueBridgeConfig,
}

View File

@ -3,7 +3,7 @@ use std::time::Duration;
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use google_home::errors::ErrorCode;
use google_home::traits::OnOff;
use rumqttc::{Publish, SubscribeFilter};
@ -31,7 +31,7 @@ pub struct HueGroupConfig {
pub client: WrappedAsyncClient,
}
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct HueGroup {
config: HueGroupConfig,
}

View File

@ -2,7 +2,7 @@ use std::time::Duration;
use anyhow::Result;
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use google_home::device;
use google_home::errors::ErrorCode;
use google_home::traits::{self, OnOff};
@ -45,7 +45,7 @@ pub struct IkeaOutletConfig {
pub client: WrappedAsyncClient,
}
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct IkeaOutlet {
config: IkeaOutletConfig,

View File

@ -3,7 +3,7 @@ use std::net::SocketAddr;
use std::str::Utf8Error;
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use bytes::{Buf, BufMut};
use google_home::errors::{self, DeviceError};
use google_home::traits;
@ -22,7 +22,7 @@ pub struct KasaOutletConfig {
pub addr: SocketAddr,
}
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct KasaOutlet {
config: KasaOutletConfig,
}

View File

@ -1,5 +1,5 @@
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use rumqttc::Publish;
use tracing::{debug, trace, warn};
@ -25,7 +25,7 @@ pub struct LightSensorConfig {
const DEFAULT: bool = false;
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct LightSensor {
config: LightSensorConfig,

View File

@ -44,20 +44,42 @@ pub trait LuaDeviceCreate {
Self: Sized;
}
macro_rules! register_device {
($lua:expr, $device:ty) => {
impl mlua::UserData for $device {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_function("new", |lua, config: mlua::Value| async {
let config = mlua::FromLua::from_lua(config, lua)?;
// TODO: Using crate:: could cause issues
let device: $device = crate::devices::LuaDeviceCreate::create(config)
.await
.map_err(mlua::ExternalError::into_lua_err)?;
Ok(crate::device_manager::WrappedDevice::new(Box::new(device)))
});
}
}
$lua.globals()
.set(stringify!($device), $lua.create_proxy::<$device>()?)?;
};
}
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
AirFilter::register_with_lua(lua)?;
AudioSetup::register_with_lua(lua)?;
ContactSensor::register_with_lua(lua)?;
DebugBridge::register_with_lua(lua)?;
HueBridge::register_with_lua(lua)?;
HueGroup::register_with_lua(lua)?;
IkeaOutlet::register_with_lua(lua)?;
KasaOutlet::register_with_lua(lua)?;
LightSensor::register_with_lua(lua)?;
Ntfy::register_with_lua(lua)?;
Presence::register_with_lua(lua)?;
WakeOnLAN::register_with_lua(lua)?;
Washer::register_with_lua(lua)?;
register_device!(lua, AirFilter);
register_device!(lua, AudioSetup);
register_device!(lua, ContactSensor);
register_device!(lua, DebugBridge);
register_device!(lua, HueBridge);
register_device!(lua, HueGroup);
register_device!(lua, IkeaOutlet);
register_device!(lua, KasaOutlet);
register_device!(lua, LightSensor);
register_device!(lua, Ntfy);
register_device!(lua, Presence);
register_device!(lua, WakeOnLAN);
register_device!(lua, Washer);
Ok(())
}

View File

@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::convert::Infallible;
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use serde::Serialize;
use serde_repr::*;
use tracing::{error, trace, warn};
@ -120,7 +120,7 @@ pub struct NtfyConfig {
pub tx: event::Sender,
}
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct Ntfy {
config: NtfyConfig,
}

View File

@ -1,7 +1,7 @@
use std::collections::HashMap;
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use rumqttc::Publish;
use tracing::{debug, trace, warn};
@ -24,7 +24,7 @@ pub struct PresenceConfig {
pub const DEFAULT_PRESENCE: bool = false;
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct Presence {
config: PresenceConfig,
devices: HashMap<String, bool>,

View File

@ -1,7 +1,7 @@
use std::net::Ipv4Addr;
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use eui48::MacAddress;
use google_home::device;
use google_home::errors::ErrorCode;
@ -29,7 +29,7 @@ pub struct WakeOnLANConfig {
pub client: WrappedAsyncClient,
}
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct WakeOnLAN {
config: WakeOnLANConfig,
}

View File

@ -1,5 +1,5 @@
use async_trait::async_trait;
use automation_macro::{LuaDevice, LuaDeviceConfig};
use automation_macro::LuaDeviceConfig;
use rumqttc::Publish;
use tracing::{debug, error, trace, warn};
@ -24,7 +24,7 @@ pub struct WasherConfig {
}
// TODO: Add google home integration
#[derive(Debug, LuaDevice)]
#[derive(Debug)]
pub struct Washer {
config: WasherConfig,