dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//! 
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//! 
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//! 
//!     http://www.apache.org/licenses/LICENSE-2.0
//! 
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.

#![allow(non_snake_case)]

//! # Application Builder
//!
//! This module provides the application builder for constructing Ri applications.
//! The `RiAppBuilder` follows the builder pattern for fluent configuration,
//! enabling developers to compose applications from various modules, configuration
//! sources, and runtime settings in a declarative manner.
//!
//! ## Builder Pattern
//!
//! The builder pattern allows step-by-step construction of complex objects.
//! Each method on `RiAppBuilder` configures a specific aspect of the application
//! and returns the builder for method chaining. This results in a fluent API
//! that is both readable and type-safe.
//!
//! ## Module Registration
//!
//! Modules are the primary extension point for Ri applications. The builder
//! supports multiple types of modules:
//!
//! - **Synchronous modules**: Implement `ServiceModule` trait for sync operations
//! - **Asynchronous modules**: Implement `AsyncServiceModule` trait for async operations
//! - **Ri modules**: Implement public `RiModule` trait (converted to async internally)
//! - **Python modules**: Modules created from Python code (with pyo3 feature)
//!
//! ## Configuration Management
//!
//! The builder supports multiple configuration sources with a defined priority order:
//!
//! 1. Configuration files (lowest priority): `dms.yaml`, `dms.yml`, `dms.toml`, `dms.json`
//! 2. Custom configuration via `with_config()` method
//! 3. Environment variables (highest priority)
//!
//! ## Usage Example
//!
//! ```rust
//! use ri::prelude::*;
//!
//! #[tokio::main]
//! async fn main() -> RiResult<()> {
//!     let app = RiAppBuilder::new()
//!         .with_config("config.yaml")?
//!         .with_module(Box::new(MySyncModule::new()))
//!         .with_async_module(Box::new(MyAsyncModule::new()))
//!         .build()?;
//!
//!     app.run(|ctx| async move {
//!         ctx.logger().info("service", "Ri service started")?;
//!         Ok(())
//!     }).await
//! }
//! ```
//!
//! ## Thread Safety
//!
//! The `RiAppBuilder` is designed to be used in a single-threaded context
//! during application construction. After calling `build()`, the resulting
//! `RiAppRuntime` is safe to use across multiple threads.
//!
//! ## Error Handling
//!
//! All builder methods that can fail return `RiResult`, enabling proper
//! error handling through the `?` operator or explicit match statements.

use crate::core::{RiResult, RiServiceContext, ServiceModule, AsyncServiceModule};
use super::module_sorter::sort_modules;
use super::module_types::{ModuleSlot, ModuleType};
use super::lifecycle::RiLifecycleObserver;
use super::analytics::RiLogAnalyticsModule;
use std::sync::Arc;

#[cfg(feature = "pyo3")]
use pyo3::PyResult;
#[cfg(feature = "pyo3")]
use crate::core::app_runtime::RiAppRuntime;

/// Public-facing application builder for Ri.
/// 
/// The `RiAppBuilder` provides a fluent API for configuring and building Ri applications.
/// It follows the builder pattern, allowing users to configure various aspects of the application
/// before building the final runtime.
/// 
/// ## Usage
/// 
/// ```rust
/// use ri::prelude::*;
/// 
/// #[tokio::main]
/// async fn main() -> RiResult<()> {
///     let app = RiAppBuilder::new()
///         .with_config("config.yaml")?
///         .with_module(Box::new(MySyncModule::new()))
///         .with_async_module(Box::new(MyAsyncModule::new()))
///         .build()?;
///     
///     app.run(|ctx| async move {
///         ctx.logger().info("service", "Ri service started")?;
///         Ok(())
///     }).await
/// }
/// ```

#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiAppBuilder {
    /// Vector of modules with their state, including both sync and async modules
    modules: Vec<ModuleSlot>, 
    /// Configuration file paths to load
    config_paths: Vec<String>, 
    /// Custom logging configuration (optional)
    logging_config: Option<crate::log::RiLogConfig>, 
    /// Custom observability configuration (optional)
    observability_config: Option<crate::observability::RiObservabilityConfig>, 
}
 
impl Default for RiAppBuilder {
    fn default() -> Self {
        Self::new()
    }
}
 
impl RiAppBuilder {
    /// Create a new empty application builder.
    /// 
    /// # Returns
    /// 
    /// A new `RiAppBuilder` instance with default settings.
    pub fn new() -> Self {
        RiAppBuilder {
            modules: Vec::new(),
            config_paths: Vec::new(),
            logging_config: None,
            observability_config: None,
        }
    }
 
    /// Add a synchronous module to the application.
    /// 
    /// # Parameters
    /// 
    /// - `module`: A boxed synchronous module implementing `ServiceModule`
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_module(mut self, module: Box<dyn ServiceModule>) -> Self {
        self.modules.push(ModuleSlot { module: ModuleType::Sync(module), failed: false });
        self
    }
    
    /// Add a Python module to the application.
    /// 
    /// This method adds a module created from Python code to the application.
    /// The module will be treated as an asynchronous Ri module.
    /// 
    /// # Parameters
    /// 
    /// - `module`: A Python module adapter implementing module configuration
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    #[cfg(feature = "pyo3")]
    pub fn with_python_module(mut self, module: crate::core::module::RiPythonModuleAdapter) -> Self {
        self.modules.push(ModuleSlot { 
            module: ModuleType::Async(Box::new(module)), 
            failed: false 
        });
        self
    }
 
    /// Add an asynchronous module to the application.
    /// 
    /// # Parameters
    /// 
    /// - `module`: A boxed asynchronous module implementing `AsyncServiceModule`
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_async_module(mut self, module: Box<dyn AsyncServiceModule>) -> Self {
        self.modules.push(ModuleSlot { module: ModuleType::Async(module), failed: false });
        self
    }
 
    /// Add a Ri module to the application.
    /// 
    /// This method adds a module implementing the public `RiModule` trait to the application.
    /// The module will be treated as an asynchronous module.
    /// 
    /// # Parameters
    /// 
    /// - `module`: A boxed module implementing `RiModule`
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_dms_module(mut self, module: Box<dyn crate::core::RiModule>) -> Self {
        // Wrap RiModule into AsyncServiceModule adapter
        struct RiModuleAdapter(Box<dyn crate::core::RiModule + Send + Sync + 'static>);
        
        #[async_trait::async_trait]
        impl AsyncServiceModule for RiModuleAdapter {
            fn name(&self) -> &str {
                self.0.name()
            }
            
            fn is_critical(&self) -> bool {
                self.0.is_critical()
            }
            
            fn priority(&self) -> i32 {
                self.0.priority()
            }
            
            fn dependencies(&self) -> Vec<&str> {
                self.0.dependencies()
            }
            
            async fn init(&mut self, ctx: &mut RiServiceContext) -> RiResult<()> {
                self.0.init(ctx).await
            }
            
            async fn before_start(&mut self, ctx: &mut RiServiceContext) -> RiResult<()> {
                self.0.before_start(ctx).await
            }
            
            async fn start(&mut self, ctx: &mut RiServiceContext) -> RiResult<()> {
                self.0.start(ctx).await
            }
            
            async fn after_start(&mut self, ctx: &mut RiServiceContext) -> RiResult<()> {
                self.0.after_start(ctx).await
            }
            
            async fn before_shutdown(&mut self, ctx: &mut RiServiceContext) -> RiResult<()> {
                self.0.before_shutdown(ctx).await
            }
            
            async fn shutdown(&mut self, ctx: &mut RiServiceContext) -> RiResult<()> {
                self.0.shutdown(ctx).await
            }
            
            async fn after_shutdown(&mut self, ctx: &mut RiServiceContext) -> RiResult<()> {
                self.0.after_shutdown(ctx).await
            }
        }
        
        self.modules.push(ModuleSlot { 
            module: ModuleType::Async(Box::new(RiModuleAdapter(module))), 
            failed: false 
        });
        self
    }
 
    /// Add multiple synchronous modules to the application.
    /// 
    /// # Parameters
    /// 
    /// - `modules`: A vector of boxed synchronous modules implementing `ServiceModule`
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_modules(mut self, modules: Vec<Box<dyn ServiceModule>>) -> Self {
        for module in modules {
            self.modules.push(ModuleSlot { module: ModuleType::Sync(module), failed: false });
        }
        self
    }
 
    /// Add multiple asynchronous modules to the application.
    /// 
    /// # Parameters
    /// 
    /// - `modules`: A vector of boxed asynchronous modules implementing `AsyncServiceModule`
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_async_modules(mut self, modules: Vec<Box<dyn AsyncServiceModule>>) -> Self {
        for module in modules {
            self.modules.push(ModuleSlot { module: ModuleType::Async(module), failed: false });
        }
        self
    }
    
    /// Add multiple Ri modules to the application.
    /// 
    /// This method adds multiple modules implementing the public `RiModule` trait to the application.
    /// Each module will be treated as an asynchronous module.
    /// 
    /// # Parameters
    /// 
    /// - `modules`: A vector of boxed modules implementing `RiModule`
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_dms_modules(mut self, modules: Vec<Box<dyn crate::core::RiModule>>) -> Self {
        for module in modules {
            self = self.with_dms_module(module);
        }
        self
    }
 
    /// Add a configuration file to the application.
    /// 
    /// # Parameters
    /// 
    /// - `config_path`: Path to the configuration file
    /// 
    /// # Returns
    /// 
    /// A `RiResult` containing the updated `RiAppBuilder` instance for method chaining.
    /// 
    /// # Errors
    /// 
    /// This method currently never returns an error, but returns `RiResult` for consistency
    /// with other builder methods and to allow for future error handling.
    pub fn with_config(mut self, config_path: impl Into<String>) -> RiResult<Self> {
        self.config_paths.push(config_path.into());
        Ok(self)
    }
 
    /// Set custom logging configuration for the application.
    /// 
    /// # Parameters
    /// 
    /// - `logging_config`: Custom logging configuration
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_logging(mut self, logging_config: crate::log::RiLogConfig) -> Self {
        self.logging_config = Some(logging_config);
        self
    }
 
    /// Set custom observability configuration for the application.
    /// 
    /// # Parameters
    /// 
    /// - `observability_config`: Custom observability configuration
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_observability(mut self, observability_config: crate::observability::RiObservabilityConfig) -> Self {
        self.observability_config = Some(observability_config);
        self
    }

    /// Add cache module with configuration.
    /// 
    /// This method adds a cache module to the application with custom configuration.
    /// The configuration is provided via a closure that receives a cache config builder.
    /// 
    /// # Parameters
    /// 
    /// - `config_fn`: Closure for configuring the cache module
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_cache_module<F>(mut self, config_fn: F) -> Self
    where
        F: FnOnce(&mut crate::cache::RiCacheConfig) -> &mut crate::cache::RiCacheConfig,
    {
        let mut config = crate::cache::RiCacheConfig::default();
        config_fn(&mut config);
        let cache_module = crate::cache::RiCacheModule::with_config(config);
        self.modules.push(ModuleSlot {
            module: ModuleType::Sync(Box::new(cache_module)),
            failed: false,
        });
        self
    }

    /// Add authentication module with configuration.
    /// 
    /// This method adds an authentication module to the application with custom configuration.
    /// 
    /// # Parameters
    /// 
    /// - `config_fn`: Closure for configuring the auth module
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_auth_module<F>(mut self, config_fn: F) -> Self
    where
        F: FnOnce(&mut crate::auth::RiAuthConfig) -> &mut crate::auth::RiAuthConfig,
    {
        let mut config = crate::auth::RiAuthConfig::default();
        config_fn(&mut config);
        let auth_module = crate::auth::RiAuthModule::with_config(config);
        self.modules.push(ModuleSlot {
            module: ModuleType::Sync(Box::new(auth_module)),
            failed: false,
        });
        self
    }

    /// Add queue module with configuration.
    /// 
    /// This method adds a message queue module to the application with custom configuration.
    /// 
    /// # Parameters
    /// 
    /// - `config_fn`: Closure for configuring the queue module
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_queue_module<F>(mut self, config_fn: F) -> Self
    where
        F: FnOnce(&mut crate::queue::RiQueueConfig) -> &mut crate::queue::RiQueueConfig,
    {
        let mut config = crate::queue::RiQueueConfig::default();
        config_fn(&mut config);
        match crate::queue::RiQueueModule::with_config(config) {
            Ok(queue_module) => {
                self.modules.push(ModuleSlot {
                    module: ModuleType::Sync(Box::new(queue_module)),
                    failed: false,
                });
            }
            Err(e) => {
                log::error!("Failed to create queue module: {}", e);
            }
        }
        self
    }

    /// Add device control module with configuration.
    /// 
    /// This method adds a device control module to the application with custom configuration.
    /// 
    /// # Parameters
    /// 
    /// - `config_fn`: Closure for configuring the device module
    /// 
    /// # Returns
    /// 
    /// The updated `RiAppBuilder` instance for method chaining.
    pub fn with_device_module<F>(mut self, config_fn: F) -> Self
    where
        F: FnOnce(&mut crate::device::RiDeviceControlConfig) -> &mut crate::device::RiDeviceControlConfig,
    {
        let mut config = crate::device::RiDeviceControlConfig::default();
        config_fn(&mut config);
        let device_module = crate::device::RiDeviceControlModule::new().with_config(config);
        self.modules.push(ModuleSlot {
            module: ModuleType::Sync(Box::new(device_module)),
            failed: false,
        });
        self
    }
 
    /// Build the application runtime.
    /// 
    /// This method performs the following steps:
    /// 1. Creates and configures the configuration manager
    /// 2. Loads configuration from specified files and environment variables
    /// 3. Creates the service context with core functionalities
    /// 4. Adds core modules (analytics and lifecycle observer)
    /// 5. Sorts modules based on dependencies and priority
    /// 6. Creates and returns the application runtime
    /// 
    /// # Returns
    /// 
    /// A `RiResult` containing the built `RiAppRuntime` instance, or an error if building fails.
    /// 
    /// # Errors
    /// 
    /// - If configuration loading fails
    /// - If service context creation fails
    /// - If module sorting fails due to circular dependencies
    pub fn build(mut self) -> RiResult<super::app_runtime::RiAppRuntime> {
        // Create config manager with specified config paths
        let mut config_manager = crate::config::RiConfigManager::new();
        
        // Add specified config files
        for path in &self.config_paths {
            config_manager.add_file_source(path);
        }
        
        // Add default config sources if no paths specified
        if self.config_paths.is_empty() {
            if let Ok(cwd) = std::env::current_dir() {
                let config_dir = cwd.join("config");
                
                // Add all supported config files in order of priority (lowest to highest)
                config_manager.add_file_source(config_dir.join("dms.yaml"));
                config_manager.add_file_source(config_dir.join("dms.yml"));
                config_manager.add_file_source(config_dir.join("dms.toml"));
                config_manager.add_file_source(config_dir.join("dms.json"));
            }
        }
        
        // Add environment variables as highest priority
        config_manager.add_environment_source();
        
        // Load configuration
        config_manager.load()?;
 
        // Create service context with custom configuration
        let ctx = self.create_service_context(config_manager)?;
        
        // Add core modules
        self.modules.push(ModuleSlot { module: ModuleType::Sync(Box::new(RiLogAnalyticsModule::new())), failed: false });
        self.modules.push(ModuleSlot { module: ModuleType::Sync(Box::new(RiLifecycleObserver::new())), failed: false });
        
        // Sort modules based on dependencies and priority
        self.modules = sort_modules(self.modules)?;
        
        let runtime = super::app_runtime::RiAppRuntime::new(ctx, self.modules);
        Ok(runtime)
    }
    
    /// Create the service context with the given configuration manager.
    /// 
    /// This method creates the service context with the following components:
    /// 1. File system accessor
    /// 2. Logger (using custom config if provided, otherwise from configuration)
    /// 3. Configuration manager
    /// 4. Hook bus for lifecycle events
    /// 
    /// # Parameters
    /// 
    /// - `config_manager`: Configuration manager with loaded configuration
    /// 
    /// # Returns
    /// 
    /// A `RiResult` containing the created `RiServiceContext` instance, or an error if creation fails.
    /// 
    /// # Errors
    /// 
    /// - If project root directory detection fails
    /// - If file system creation fails
    /// - If logger creation fails
    fn create_service_context(&self, config_manager: crate::config::RiConfigManager) -> RiResult<RiServiceContext> {
        let cfg = config_manager.config();
 
        let project_root = std::env::current_dir()
            .map_err(|e| crate::core::RiError::Other(format!("detect project root failed: {e}")))?;
        let app_data_root = if let Some(root_str) = cfg.get_str("fs.app_data_root") {
            project_root.join(root_str)
        } else {
            project_root.join(".dms")
        };
 
        let fs = crate::fs::RiFileSystem::new_with_roots(project_root, app_data_root);

        // Use custom logging config if provided, otherwise create from config
        let log_config: crate::log::RiLogConfig = if let Some(log_config) = &self.logging_config {
            log_config.clone()
        } else {
            crate::log::RiLogConfig::from_config(&cfg)
        };
        let logger = crate::log::RiLogger::new(&log_config, fs.clone());
        let hooks = crate::hooks::RiHookBus::new();
        let metrics_registry = Some(Arc::new(crate::observability::RiMetricsRegistry::new()));
        
        Ok(RiServiceContext::new_with(fs, logger, config_manager, hooks, metrics_registry))
    }
}
 
#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiAppBuilder {
    #[new]
    fn py_new() -> Self {
        Self::new()
    }

    fn py_with_config(&mut self, config_path: &str) -> PyResult<Self> {
        self.config_paths.push(config_path.to_string());
        Ok(std::mem::take(self))
    }

    fn py_with_logging(&mut self, logging_config: crate::log::RiLogConfig) -> PyResult<Self> {
        self.logging_config = Some(logging_config);
        Ok(std::mem::take(self))
    }

    fn py_with_observability(&mut self, observability_config: crate::observability::RiObservabilityConfig) -> PyResult<Self> {
        self.observability_config = Some(observability_config);
        Ok(std::mem::take(self))
    }

    fn py_build(&mut self) -> PyResult<RiAppRuntime> {
        let builder = std::mem::take(self);
        RiAppBuilder::build(builder).map_err(|e| pyo3::prelude::PyErr::from(e))
    }

    fn py_with_module(&mut self, module: super::module::RiPythonServiceModule) -> PyResult<Self> {
        self.modules.push(crate::core::module_types::ModuleSlot {
            module: crate::core::module_types::ModuleType::Sync(Box::new(module)),
            failed: false,
        });
        Ok(std::mem::take(self))
    }

    fn py_with_python_module(&mut self, module: super::module::RiPythonModuleAdapter) -> PyResult<Self> {
        self.modules.push(crate::core::module_types::ModuleSlot {
            module: crate::core::module_types::ModuleType::Async(Box::new(module)),
            failed: false,
        });
        Ok(std::mem::take(self))
    }

    fn py_with_async_module(&mut self, module: super::module::RiPythonAsyncServiceModule) -> PyResult<Self> {
        self.modules.push(crate::core::module_types::ModuleSlot {
            module: crate::core::module_types::ModuleType::Async(Box::new(module)),
            failed: false,
        });
        Ok(std::mem::take(self))
    }

    fn py_with_dms_module(&mut self, module: super::module::RiPythonModuleAdapter) -> PyResult<Self> {
        self.modules.push(crate::core::module_types::ModuleSlot {
            module: crate::core::module_types::ModuleType::Async(Box::new(module)),
            failed: false,
        });
        Ok(std::mem::take(self))
    }

    fn py_with_modules(&mut self, modules: Vec<super::module::RiPythonServiceModule>) -> PyResult<Self> {
        for module in modules {
            self.modules.push(crate::core::module_types::ModuleSlot {
                module: crate::core::module_types::ModuleType::Sync(Box::new(module)),
                failed: false,
            });
        }
        Ok(std::mem::take(self))
    }

    fn py_with_async_modules(&mut self, modules: Vec<super::module::RiPythonAsyncServiceModule>) -> PyResult<Self> {
        for module in modules {
            self.modules.push(crate::core::module_types::ModuleSlot {
                module: crate::core::module_types::ModuleType::Async(Box::new(module)),
                failed: false,
            });
        }
        Ok(std::mem::take(self))
    }

    fn py_with_dms_modules(&mut self, modules: Vec<super::module::RiPythonModuleAdapter>) -> PyResult<Self> {
        for module in modules {
            self.modules.push(crate::core::module_types::ModuleSlot {
                module: crate::core::module_types::ModuleType::Async(Box::new(module)),
                failed: false,
            });
        }
        Ok(std::mem::take(self))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_app_builder_creation() {
        let builder = RiAppBuilder::new();
        assert!(builder.modules.is_empty());
        assert!(builder.config_paths.is_empty());
        assert!(builder.logging_config.is_none());
        assert!(builder.observability_config.is_none());
    }

    #[test]
    fn test_app_builder_with_config() {
        let builder = RiAppBuilder::new()
            .with_config("config.yaml")
            .unwrap();
        assert_eq!(builder.config_paths.len(), 1);
        assert_eq!(builder.config_paths[0], "config.yaml");
    }

    #[test]
    fn test_app_builder_method_chaining() {
        let builder = RiAppBuilder::new()
            .with_config("config.yaml")
            .unwrap()
            .with_logging(crate::log::RiLogConfig::default());
        assert_eq!(builder.config_paths.len(), 1);
        assert!(builder.logging_config.is_some());
    }

    #[test]
    fn test_app_builder_with_observability() {
        let builder = RiAppBuilder::new()
            .with_observability(crate::observability::RiObservabilityConfig::default());
        assert!(builder.observability_config.is_some());
    }

    #[test]
    fn test_app_builder_with_multiple_configs() {
        let builder = RiAppBuilder::new()
            .with_config("config1.yaml")
            .unwrap()
            .with_config("config2.yaml")
            .unwrap();
        assert_eq!(builder.config_paths.len(), 2);
    }
}