Files
automation_rs/src/config.rs
Dreaded_X 3b7579878b feat: Use ActionCallback for schedule
This has two advantages:
- Each schedule entry can take either a single function or table of
  functions.
- We get a better type definition.
2025-10-19 03:40:16 +02:00

58 lines
1.4 KiB
Rust

use std::collections::HashMap;
use std::net::{Ipv4Addr, SocketAddr};
use automation_lib::action_callback::ActionCallback;
use automation_lib::device::Device;
use automation_macro::LuaDeviceConfig;
use lua_typed::Typed;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Setup {
#[serde(default = "default_entrypoint")]
pub entrypoint: String,
#[serde(default)]
pub variables: HashMap<String, String>,
#[serde(default)]
pub secrets: HashMap<String, String>,
}
fn default_entrypoint() -> String {
"./config.lua".into()
}
#[derive(Debug, Deserialize, Typed)]
pub struct FulfillmentConfig {
pub openid_url: String,
#[serde(default = "default_fulfillment_ip")]
#[typed(default)]
pub ip: Ipv4Addr,
#[serde(default = "default_fulfillment_port")]
#[typed(default)]
pub port: u16,
}
#[derive(Debug, LuaDeviceConfig, Typed)]
pub struct Config {
pub fulfillment: FulfillmentConfig,
#[device_config(from_lua, default)]
#[typed(default)]
pub devices: Vec<Box<dyn Device>>,
#[device_config(from_lua, default)]
#[typed(default)]
pub schedule: HashMap<String, ActionCallback<()>>,
}
impl From<FulfillmentConfig> for SocketAddr {
fn from(fulfillment: FulfillmentConfig) -> Self {
(fulfillment.ip, fulfillment.port).into()
}
}
fn default_fulfillment_ip() -> Ipv4Addr {
[0, 0, 0, 0].into()
}
fn default_fulfillment_port() -> u16 {
7878
}