22//! Loads and runs modules built with caryatid-sdk
33
44use anyhow:: { anyhow, Result } ;
5- use caryatid_sdk:: config:: { config_from_value, get_sub_config} ;
5+ use caryatid_sdk:: config:: { build_module_config , config_from_value, get_sub_config} ;
66use caryatid_sdk:: context:: GlobalContext ;
77use caryatid_sdk:: { Context , MessageBounds , MessageBus , Module , ModuleRegistry } ;
88use config:: Config ;
@@ -58,7 +58,7 @@ impl<M: MessageBounds> Process<M> {
5858 }
5959 } ;
6060
61- return Ok ( BusInfo { id, bus } ) ;
61+ Ok ( BusInfo { id, bus } )
6262 }
6363
6464 /// Create a process with the given config
@@ -78,7 +78,6 @@ impl<M: MessageBounds> Process<M> {
7878 Ok ( bus) => {
7979 buses. push ( Arc :: new ( bus) ) ;
8080 }
81-
8281 _ => { }
8382 }
8483 }
@@ -112,36 +111,36 @@ impl<M: MessageBounds> Process<M> {
112111 }
113112
114113 // Initialise all the modules from [module.<id>] configuration
115- if let Ok ( mod_confs) = self . config . get_table ( "module" ) {
116- for ( id, mod_conf) in mod_confs {
117- if let Ok ( modt) = mod_conf. into_table ( ) {
118- let modc = config_from_value ( modt) ;
119- let mut module_name = id. clone ( ) ; // Default
120- if let Ok ( class) = modc. get_string ( "class" ) {
121- module_name = class;
122- }
123-
124- // Look up the module
125- if let Some ( module) = self . modules . get ( & module_name) {
126- info ! ( "Initialising module {id}" ) ;
127- let message_bus = self . context . message_bus . clone ( ) ;
128- let message_bus = if let Some ( m) = & mut monitor {
129- m. spy_on_bus ( & module_name, message_bus)
130- } else {
131- message_bus
132- } ;
133- let context = Arc :: new ( Context :: new (
134- self . config . clone ( ) ,
135- message_bus,
136- self . context . startup_watch . subscribe ( ) ,
137- ) ) ;
138- module. init ( context, Arc :: new ( modc) ) . await . unwrap ( ) ;
139- } else {
140- error ! ( "Unrecognised module class: {module_name} in [module.{id}]" ) ;
141- }
142- } else {
114+ if let Ok ( module_cfgs) = self . config . get_table ( "module" ) {
115+ for ( id, module_cfg) in module_cfgs {
116+ let Ok ( module_tbl) = module_cfg. into_table ( ) else {
143117 warn ! ( "Bad configuration for module {id} ignored" ) ;
144- }
118+ continue ;
119+ } ;
120+
121+ let module_cfg = build_module_config ( & self . config , module_tbl) ;
122+ let module_name = module_cfg. get_string ( "class" ) . unwrap_or_else ( |_| id. clone ( ) ) ;
123+
124+ let Some ( module) = self . modules . get ( & module_name) else {
125+ error ! ( "Unrecognised module class: {module_name} in [module.{id}]" ) ;
126+ continue ;
127+ } ;
128+
129+ info ! ( "Initialising module {id}" ) ;
130+
131+ let message_bus = self . context . message_bus . clone ( ) ;
132+ let message_bus = match & mut monitor {
133+ Some ( m) => m. spy_on_bus ( & module_name, message_bus) ,
134+ None => message_bus,
135+ } ;
136+
137+ let context = Arc :: new ( Context :: new (
138+ self . config . clone ( ) ,
139+ message_bus,
140+ self . context . startup_watch . subscribe ( ) ,
141+ ) ) ;
142+
143+ module. init ( context, Arc :: new ( module_cfg) ) . await ?;
145144 }
146145 }
147146
@@ -151,9 +150,9 @@ impl<M: MessageBounds> Process<M> {
151150 tokio:: spawn ( monitor. monitor ( ) ) ;
152151 }
153152
154- // Send startup message if required
153+ // Send the startup message if required
155154 let _ = self . context . startup_watch . send ( true ) ;
156- if let Ok ( topic) = self . config . get_string ( "startup.topic" ) {
155+ if let Ok ( topic) = self . config . get_string ( "global. startup.topic" ) {
157156 self . context
158157 . message_bus
159158 . publish ( & topic, Arc :: new ( M :: default ( ) ) )
0 commit comments