Improved Lua macro situation
This commit is contained in:
parent
006320be18
commit
98ab265fed
|
@ -1,19 +1,10 @@
|
||||||
#![feature(let_chains)]
|
#![feature(let_chains)]
|
||||||
#![feature(iter_intersperse)]
|
#![feature(iter_intersperse)]
|
||||||
mod lua_device;
|
|
||||||
mod lua_device_config;
|
mod lua_device_config;
|
||||||
|
|
||||||
use lua_device::impl_lua_device_macro;
|
|
||||||
use lua_device_config::impl_lua_device_config_macro;
|
use lua_device_config::impl_lua_device_config_macro;
|
||||||
use syn::{parse_macro_input, DeriveInput};
|
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))]
|
#[proc_macro_derive(LuaDeviceConfig, attributes(device_config))]
|
||||||
pub fn lua_device_config_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
pub fn lua_device_config_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
let ast = parse_macro_input!(input as DeriveInput);
|
let ast = parse_macro_input!(input as DeriveInput);
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -1,5 +1,5 @@
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use google_home::device::Name;
|
use google_home::device::Name;
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
use google_home::traits::{
|
use google_home::traits::{
|
||||||
|
@ -27,7 +27,7 @@ pub struct AirFilterConfig {
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct AirFilter {
|
pub struct AirFilter {
|
||||||
config: AirFilterConfig,
|
config: AirFilterConfig,
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use google_home::traits::OnOff;
|
use google_home::traits::OnOff;
|
||||||
use tracing::{debug, error, trace, warn};
|
use tracing::{debug, error, trace, warn};
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ pub struct AudioSetupConfig {
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct AudioSetup {
|
pub struct AudioSetup {
|
||||||
config: AudioSetupConfig,
|
config: AudioSetupConfig,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use google_home::traits::OnOff;
|
use google_home::traits::OnOff;
|
||||||
use mlua::FromLua;
|
use mlua::FromLua;
|
||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
|
@ -62,7 +62,7 @@ pub struct ContactSensorConfig {
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct ContactSensor {
|
pub struct ContactSensor {
|
||||||
config: ContactSensorConfig,
|
config: ContactSensorConfig,
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use tracing::{trace, warn};
|
use tracing::{trace, warn};
|
||||||
|
|
||||||
use super::LuaDeviceCreate;
|
use super::LuaDeviceCreate;
|
||||||
|
@ -20,7 +20,7 @@ pub struct DebugBridgeConfig {
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct DebugBridge {
|
pub struct DebugBridge {
|
||||||
config: DebugBridgeConfig,
|
config: DebugBridgeConfig,
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::convert::Infallible;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tracing::{error, trace, warn};
|
use tracing::{error, trace, warn};
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ pub struct HueBridgeConfig {
|
||||||
pub flags: FlagIDs,
|
pub flags: FlagIDs,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct HueBridge {
|
pub struct HueBridge {
|
||||||
config: HueBridgeConfig,
|
config: HueBridgeConfig,
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
use google_home::traits::OnOff;
|
use google_home::traits::OnOff;
|
||||||
use rumqttc::{Publish, SubscribeFilter};
|
use rumqttc::{Publish, SubscribeFilter};
|
||||||
|
@ -31,7 +31,7 @@ pub struct HueGroupConfig {
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct HueGroup {
|
pub struct HueGroup {
|
||||||
config: HueGroupConfig,
|
config: HueGroupConfig,
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use google_home::device;
|
use google_home::device;
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
use google_home::traits::{self, OnOff};
|
use google_home::traits::{self, OnOff};
|
||||||
|
@ -45,7 +45,7 @@ pub struct IkeaOutletConfig {
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct IkeaOutlet {
|
pub struct IkeaOutlet {
|
||||||
config: IkeaOutletConfig,
|
config: IkeaOutletConfig,
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::net::SocketAddr;
|
||||||
use std::str::Utf8Error;
|
use std::str::Utf8Error;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use bytes::{Buf, BufMut};
|
use bytes::{Buf, BufMut};
|
||||||
use google_home::errors::{self, DeviceError};
|
use google_home::errors::{self, DeviceError};
|
||||||
use google_home::traits;
|
use google_home::traits;
|
||||||
|
@ -22,7 +22,7 @@ pub struct KasaOutletConfig {
|
||||||
pub addr: SocketAddr,
|
pub addr: SocketAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct KasaOutlet {
|
pub struct KasaOutlet {
|
||||||
config: KasaOutletConfig,
|
config: KasaOutletConfig,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use rumqttc::Publish;
|
use rumqttc::Publish;
|
||||||
use tracing::{debug, trace, warn};
|
use tracing::{debug, trace, warn};
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ pub struct LightSensorConfig {
|
||||||
|
|
||||||
const DEFAULT: bool = false;
|
const DEFAULT: bool = false;
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct LightSensor {
|
pub struct LightSensor {
|
||||||
config: LightSensorConfig,
|
config: LightSensorConfig,
|
||||||
|
|
||||||
|
|
|
@ -44,20 +44,42 @@ pub trait LuaDeviceCreate {
|
||||||
Self: Sized;
|
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<()> {
|
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
|
||||||
AirFilter::register_with_lua(lua)?;
|
register_device!(lua, AirFilter);
|
||||||
AudioSetup::register_with_lua(lua)?;
|
register_device!(lua, AudioSetup);
|
||||||
ContactSensor::register_with_lua(lua)?;
|
register_device!(lua, ContactSensor);
|
||||||
DebugBridge::register_with_lua(lua)?;
|
register_device!(lua, DebugBridge);
|
||||||
HueBridge::register_with_lua(lua)?;
|
register_device!(lua, HueBridge);
|
||||||
HueGroup::register_with_lua(lua)?;
|
register_device!(lua, HueGroup);
|
||||||
IkeaOutlet::register_with_lua(lua)?;
|
register_device!(lua, IkeaOutlet);
|
||||||
KasaOutlet::register_with_lua(lua)?;
|
register_device!(lua, KasaOutlet);
|
||||||
LightSensor::register_with_lua(lua)?;
|
register_device!(lua, LightSensor);
|
||||||
Ntfy::register_with_lua(lua)?;
|
register_device!(lua, Ntfy);
|
||||||
Presence::register_with_lua(lua)?;
|
register_device!(lua, Presence);
|
||||||
WakeOnLAN::register_with_lua(lua)?;
|
register_device!(lua, WakeOnLAN);
|
||||||
Washer::register_with_lua(lua)?;
|
register_device!(lua, Washer);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_repr::*;
|
use serde_repr::*;
|
||||||
use tracing::{error, trace, warn};
|
use tracing::{error, trace, warn};
|
||||||
|
@ -120,7 +120,7 @@ pub struct NtfyConfig {
|
||||||
pub tx: event::Sender,
|
pub tx: event::Sender,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct Ntfy {
|
pub struct Ntfy {
|
||||||
config: NtfyConfig,
|
config: NtfyConfig,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use rumqttc::Publish;
|
use rumqttc::Publish;
|
||||||
use tracing::{debug, trace, warn};
|
use tracing::{debug, trace, warn};
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ pub struct PresenceConfig {
|
||||||
|
|
||||||
pub const DEFAULT_PRESENCE: bool = false;
|
pub const DEFAULT_PRESENCE: bool = false;
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct Presence {
|
pub struct Presence {
|
||||||
config: PresenceConfig,
|
config: PresenceConfig,
|
||||||
devices: HashMap<String, bool>,
|
devices: HashMap<String, bool>,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::net::Ipv4Addr;
|
use std::net::Ipv4Addr;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use eui48::MacAddress;
|
use eui48::MacAddress;
|
||||||
use google_home::device;
|
use google_home::device;
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
|
@ -29,7 +29,7 @@ pub struct WakeOnLANConfig {
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct WakeOnLAN {
|
pub struct WakeOnLAN {
|
||||||
config: WakeOnLANConfig,
|
config: WakeOnLANConfig,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::LuaDeviceConfig;
|
||||||
use rumqttc::Publish;
|
use rumqttc::Publish;
|
||||||
use tracing::{debug, error, trace, warn};
|
use tracing::{debug, error, trace, warn};
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ pub struct WasherConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add google home integration
|
// TODO: Add google home integration
|
||||||
#[derive(Debug, LuaDevice)]
|
#[derive(Debug)]
|
||||||
pub struct Washer {
|
pub struct Washer {
|
||||||
config: WasherConfig,
|
config: WasherConfig,
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user