From 45485fca37b85d54316715b4e326dd6a4f8cb383 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Fri, 10 Oct 2025 03:50:47 +0200 Subject: [PATCH] feat: Add proper type definition for devices Depending on the implemented traits the lua class will inherit from the associated interface class. It also specifies the constructor function for each of the devices. --- automation_macro/src/device.rs | 25 ++++++++++++++++++++ definitions/interfaces.lua | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 definitions/interfaces.lua diff --git a/automation_macro/src/device.rs b/automation_macro/src/device.rs index 6ccbdc7..48675f6 100644 --- a/automation_macro/src/device.rs +++ b/automation_macro/src/device.rs @@ -138,6 +138,12 @@ impl quote::ToTokens for Implementation { add_methods, } = &self; + let interfaces: String = traits + .0 + .iter() + .map(|tr| format!(", Interface{tr}")) + .collect(); + tokens.extend(quote! { impl mlua::UserData for #name { fn add_methods>(methods: &mut M) { @@ -168,6 +174,25 @@ impl quote::ToTokens for Implementation { fn type_name() -> String { stringify!(#name).into() } + + fn generate_header() -> std::option::Option<::std::string::String> { + let type_name = ::type_name(); + Some(format!("---@class {type_name}: InterfaceDevice{}\nlocal {type_name}\n", #interfaces)) + } + + fn generate_members() -> Option { + let mut output = String::new(); + + let type_name = ::type_name(); + output += &format!("devices.{type_name} = {{}}\n"); + let config_name = <::Config as ::lua_typed::Typed>::type_name(); + output += &format!("---@param config {config_name}\n"); + output += &format!("---@return {type_name}\n"); + output += &format!("function devices.{type_name}.new(config) end\n"); + + + Some(output) + } } }); } diff --git a/definitions/interfaces.lua b/definitions/interfaces.lua new file mode 100644 index 0000000..222cc4d --- /dev/null +++ b/definitions/interfaces.lua @@ -0,0 +1,42 @@ +--- @meta + +---@class InterfaceDevice +local InterfaceDevice +---@return string +function InterfaceDevice:get_id() end + +---@class InterfaceOnOff: InterfaceDevice +local InterfaceOnOff +---@async +---@param on boolean +function InterfaceOnOff:set_on(on) end +---@async +---@return boolean +function InterfaceOnOff:on() end + +---@class InterfaceBrightness: InterfaceDevice +local InterfaceBrightness +---@async +---@param brightness integer +function InterfaceBrightness:set_brightness(brightness) end +---@async +---@return integer +function InterfaceBrightness:brightness() end + +---@class InterfaceColorSetting: InterfaceDevice +local InterfaceColorSetting +---@async +---@param temperature integer +function InterfaceColorSetting:set_color_temperature(temperature) end +---@async +---@return integer +function InterfaceColorSetting:color_temperature() end + +---@class InterfaceOpenClose: InterfaceDevice +local InterfaceOpenClose +---@async +---@param open_percent integer +function InterfaceOpenClose:set_open_percent(open_percent) end +---@async +---@return integer +function InterfaceOpenClose:open_percent() end