dynamic-cli 0.2.0

A framework for building configurable CLI and REPL applications from YAML/JSON configuration files
Documentation
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
//! Command execution module
//!
//! This module provides the core functionality for executing commands in the
//! dynamic-cli framework. It defines the [`CommandHandler`] trait that all
//! command implementations must satisfy.
//!
//! # Module Organization
//!
//! - [`traits`]: Core trait definitions (`CommandHandler`)
//! - `command_executor` (future): Executor logic for running commands
//!
//! # Architecture
//!
//! The execution flow in dynamic-cli follows this pattern:
//!
//! ```text
//! User Input → Parser → Validator → Executor → Command Handler
//!//!                                  Context
//! ```
//!
//! 1. **Parser**: Converts raw input to structured arguments
//! 2. **Validator**: Checks argument types and constraints
//! 3. **Executor**: Looks up and invokes the appropriate handler
//! 4. **Handler**: Executes the command logic with access to context
//!
//! # Design Philosophy
//!
//! ## Object Safety
//!
//! The module is designed around object-safe traits to enable dynamic dispatch.
//! This allows:
//! - Runtime registration of commands
//! - Storing heterogeneous handlers in collections
//! - Plugin-style architecture where handlers are loaded dynamically
//!
//! ## Thread Safety
//!
//! All types are `Send + Sync` to support:
//! - Multi-threaded CLI applications
//! - Concurrent command execution (future enhancement)
//! - Safe shared access to the command registry
//!
//! ## Simplicity
//!
//! The API is intentionally kept simple:
//! - Arguments are passed as `HashMap<String, String>`
//! - Context is accessed through trait objects
//! - Error handling uses the framework's standard `Result` type
//!
//! # Quick Start
//!
//! ```
//! use std::collections::HashMap;
//! use dynamic_cli::error::ExecutionError;
//! use dynamic_cli::executor::CommandHandler;
//! use dynamic_cli::context::ExecutionContext;
//! use dynamic_cli::Result;
//!
//! // 1. Define your context
//! #[derive(Default)]
//! struct AppContext {
//!     counter: i32,
//! }
//!
//! impl ExecutionContext for AppContext {
//!     fn as_any(&self) -> &dyn std::any::Any { self }
//!     fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
//! }
//!
//! // 2. Implement a command handler
//! struct IncrementCommand;
//!
//! impl CommandHandler for IncrementCommand {
//!     fn execute(
//!         &self,
//!         context: &mut dyn ExecutionContext,
//!         args: &HashMap<String, String>,
//!     ) -> Result<()> {
//!         let ctx = dynamic_cli::context::downcast_mut::<AppContext>(context)
//!             .ok_or_else(|| ExecutionError::CommandFailed(anyhow::anyhow!("Wrong context type")))?;
//!         
//!         let amount: i32 = args.get("amount")
//!             .and_then(|s| s.parse().ok())
//!             .unwrap_or(1);
//!         
//!         ctx.counter += amount;
//!         println!("Counter is now: {}", ctx.counter);
//!         Ok(())
//!     }
//! }
//!
//! // 3. Use the handler
//! # fn main() -> Result<()> {
//! let handler = IncrementCommand;
//! let mut context = AppContext::default();
//! let mut args = HashMap::new();
//! args.insert("amount".to_string(), "5".to_string());
//!
//! handler.execute(&mut context, &args)?;
//! assert_eq!(context.counter, 5);
//! # Ok(())
//! # }
//! ```
//!
//! # Examples
//!
//! ## Basic Command
//!
//! ```
//! use std::collections::HashMap;
//! use dynamic_cli::executor::CommandHandler;
//! use dynamic_cli::context::ExecutionContext;
//! use dynamic_cli::Result;
//!
//! struct EchoCommand;
//!
//! impl CommandHandler for EchoCommand {
//!     fn execute(
//!         &self,
//!         _context: &mut dyn ExecutionContext,
//!         args: &HashMap<String, String>,
//!     ) -> Result<()> {
//!         if let Some(message) = args.get("message") {
//!             println!("{}", message);
//!         }
//!         Ok(())
//!     }
//! }
//! ```
//!
//! ## Command with Validation
//!
//! ```
//! use std::collections::HashMap;
//! use dynamic_cli::error::ExecutionError;
//! use dynamic_cli::executor::CommandHandler;
//! use dynamic_cli::context::ExecutionContext;
//! use dynamic_cli::DynamicCliError::Execution;
//! use dynamic_cli::Result;
//!
//! struct DivideCommand;
//!
//! impl CommandHandler for DivideCommand {
//!     fn execute(
//!         &self,
//!         _context: &mut dyn ExecutionContext,
//!         args: &HashMap<String, String>,
//!     ) -> dynamic_cli::Result<()> {
//!         let denom = args.get("denominator")
//!             .ok_or_else(|| {
//!                 ExecutionError::CommandFailed(
//!                     anyhow::anyhow!("Missing Denominator"))})?;
//!
//!         let value: f64 = denom.parse()
//!             .map_err(|_| {
//!                 ExecutionError::CommandFailed(
//!                     anyhow::anyhow!("Invalid Denominator"))})?;
//!
//!         if value == 0.0 {
//!             return Err(ExecutionError::CommandFailed(
//!                     anyhow::anyhow!("Cannot divide by zero")).into());
//!         }
//!         Ok(())
//!     }
//! }
//! ```
//!
//! ## Stateful Command
//!
//! ```
//! use std::collections::HashMap;
//! use dynamic_cli::error::ExecutionError;
//! use dynamic_cli::executor::CommandHandler;
//! use dynamic_cli::context::ExecutionContext;
//! use dynamic_cli::Result;
//!
//! #[derive(Default)]
//! struct FileContext {
//!     current_file: Option<String>,
//! }
//!
//! impl ExecutionContext for FileContext {
//!     fn as_any(&self) -> &dyn std::any::Any { self }
//!     fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
//! }
//!
//! struct OpenCommand;
//!
//! impl CommandHandler for OpenCommand {
//!     fn execute(
//!         &self,
//!         context: &mut dyn ExecutionContext,
//!         args: &HashMap<String, String>,
//!     ) -> Result<()> {
//!         let ctx = dynamic_cli::context::downcast_mut::<FileContext>(context)
//!             .ok_or_else(|| ExecutionError::CommandFailed(anyhow::anyhow!("Wrong context type")))?;
//!         
//!         let filename = args.get("file")
//!             .ok_or_else(|| { ExecutionError::CommandFailed(anyhow::anyhow!("Missing file argument"))})?;
//!         
//!         ctx.current_file = Some(filename.clone());
//!         println!("Opened: {}", filename);
//!         Ok(())
//!     }
//! }
//! ```
//!
//! # Advanced Usage
//!
//! ## Dynamic Command Registration
//!
//! Commands can be registered dynamically at runtime using trait objects:
//!
//! ```
//! use std::collections::HashMap;
//! use dynamic_cli::executor::CommandHandler;
//! # use dynamic_cli::context::ExecutionContext;
//! # use dynamic_cli::Result;
//!
//! // Store commands in a registry
//! struct CommandRegistry {
//!     handlers: HashMap<String, Box<dyn CommandHandler>>,
//! }
//!
//! impl CommandRegistry {
//!     fn new() -> Self {
//!         Self {
//!             handlers: HashMap::new(),
//!         }
//!     }
//!     
//!     fn register(&mut self, name: String, handler: Box<dyn CommandHandler>) {
//!         self.handlers.insert(name, handler);
//!     }
//!     
//!     fn get(&self, name: &str) -> Option<&Box<dyn CommandHandler>> {
//!         self.handlers.get(name)
//!     }
//! }
//! ```
//!
//! ## Error Handling Pattern
//!
//! ```
//! use std::collections::HashMap;
//! use dynamic_cli::executor::CommandHandler;
//! use dynamic_cli::context::ExecutionContext;
//! use dynamic_cli::error::ExecutionError;
//! use dynamic_cli::Result;
//!
//! struct FileCommand;
//!
//! impl CommandHandler for FileCommand {
//!     fn execute(
//!         &self,
//!         _context: &mut dyn ExecutionContext,
//!         args: &HashMap<String, String>,
//!     ) -> Result<()> {
//!         let path = args.get("path")
//!             .ok_or_else(|| { ExecutionError::CommandFailed(anyhow::anyhow!("Missing path argument"))})?;
//!         
//!         // Wrap application errors in ExecutionError
//!         std::fs::read_to_string(path)
//!             .map_err(|e| ExecutionError::CommandFailed(
//!                 anyhow::anyhow!("Failed to read file: {}", e)
//!             ))?;
//!         
//!         Ok(())
//!     }
//! }
//! ```

// Public submodules
pub mod traits;

// Public re-exports for convenience
pub use traits::CommandHandler;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::ExecutionContext;
    use crate::error::ExecutionError;
    use std::any::Any;
    use std::collections::HashMap;

    // ============================================================================
    // INTEGRATION TEST FIXTURES
    // ============================================================================

    /// Test context for integration tests
    #[derive(Default)]
    struct IntegrationContext {
        log: Vec<String>,
        state: HashMap<String, String>,
    }

    impl ExecutionContext for IntegrationContext {
        fn as_any(&self) -> &dyn Any {
            self
        }

        fn as_any_mut(&mut self) -> &mut dyn Any {
            self
        }
    }

    /// Command that logs its execution
    struct LogCommand;

    impl CommandHandler for LogCommand {
        fn execute(
            &self,
            context: &mut dyn ExecutionContext,
            args: &HashMap<String, String>,
        ) -> crate::error::Result<()> {
            let ctx =
                crate::context::downcast_mut::<IntegrationContext>(context).ok_or_else(|| {
                    ExecutionError::CommandFailed(anyhow::anyhow!("Wrong context type"))
                })?;

            let message = args
                .get("message")
                .map(|s| s.as_str())
                .unwrap_or("default message");
            ctx.log.push(message.to_string());
            Ok(())
        }
    }

    /// Command that sets state
    struct SetCommand;

    impl CommandHandler for SetCommand {
        fn execute(
            &self,
            context: &mut dyn ExecutionContext,
            args: &HashMap<String, String>,
        ) -> crate::error::Result<()> {
            let ctx =
                crate::context::downcast_mut::<IntegrationContext>(context).ok_or_else(|| {
                    ExecutionError::CommandFailed(anyhow::anyhow!("Wrong context type"))
                })?;

            if let (Some(key), Some(value)) = (args.get("key"), args.get("value")) {
                ctx.state.insert(key.clone(), value.clone());
            }
            Ok(())
        }
    }

    /// Command that reads state
    struct GetCommand;

    impl CommandHandler for GetCommand {
        fn execute(
            &self,
            context: &mut dyn ExecutionContext,
            args: &HashMap<String, String>,
        ) -> crate::error::Result<()> {
            let ctx =
                crate::context::downcast_mut::<IntegrationContext>(context).ok_or_else(|| {
                    ExecutionError::CommandFailed(anyhow::anyhow!("Wrong context type"))
                })?;

            if let Some(key) = args.get("key") {
                if let Some(value) = ctx.state.get(key) {
                    ctx.log.push(format!("{} = {}", key, value));
                } else {
                    ctx.log.push(format!("{} not found", key));
                }
            }
            Ok(())
        }
    }

    // ============================================================================
    // INTEGRATION TESTS
    // ============================================================================

    #[test]
    fn test_module_reexports() {
        // Verify that CommandHandler is accessible from module root
        fn _accepts_handler(_: &dyn CommandHandler) {}

        let handler = LogCommand;
        _accepts_handler(&handler);
    }

    #[test]
    fn test_command_sequence() {
        // Test executing multiple commands in sequence
        let mut context = IntegrationContext::default();

        // Execute first command
        let log_cmd = LogCommand;
        let mut args1 = HashMap::new();
        args1.insert("message".to_string(), "First".to_string());
        log_cmd.execute(&mut context, &args1).unwrap();

        // Execute second command
        let mut args2 = HashMap::new();
        args2.insert("message".to_string(), "Second".to_string());
        log_cmd.execute(&mut context, &args2).unwrap();

        // Verify both commands executed
        assert_eq!(context.log.len(), 2);
        assert_eq!(context.log[0], "First");
        assert_eq!(context.log[1], "Second");
    }

    #[test]
    fn test_stateful_workflow() {
        // Test a complete workflow with state management
        let mut context = IntegrationContext::default();

        // Set some values
        let set_cmd = SetCommand;
        let mut args1 = HashMap::new();
        args1.insert("key".to_string(), "name".to_string());
        args1.insert("value".to_string(), "Alice".to_string());
        set_cmd.execute(&mut context, &args1).unwrap();

        let mut args2 = HashMap::new();
        args2.insert("key".to_string(), "age".to_string());
        args2.insert("value".to_string(), "30".to_string());
        set_cmd.execute(&mut context, &args2).unwrap();

        // Retrieve values
        let get_cmd = GetCommand;
        let mut args3 = HashMap::new();
        args3.insert("key".to_string(), "name".to_string());
        get_cmd.execute(&mut context, &args3).unwrap();

        let mut args4 = HashMap::new();
        args4.insert("key".to_string(), "age".to_string());
        get_cmd.execute(&mut context, &args4).unwrap();

        // Verify workflow
        assert_eq!(context.state.len(), 2);
        assert_eq!(context.state.get("name"), Some(&"Alice".to_string()));
        assert_eq!(context.state.get("age"), Some(&"30".to_string()));
        assert_eq!(context.log.len(), 2);
        assert_eq!(context.log[0], "name = Alice");
        assert_eq!(context.log[1], "age = 30");
    }

    #[test]
    fn test_heterogeneous_handler_collection() {
        // Test storing different handler types in a collection
        let handlers: Vec<Box<dyn CommandHandler>> = vec![
            Box::new(LogCommand),
            Box::new(SetCommand),
            Box::new(GetCommand),
        ];

        // Verify we can store different handlers
        assert_eq!(handlers.len(), 3);

        // Execute each handler
        let mut context = IntegrationContext::default();

        let mut args1 = HashMap::new();
        args1.insert("message".to_string(), "test".to_string());
        handlers[0].execute(&mut context, &args1).unwrap();

        let mut args2 = HashMap::new();
        args2.insert("key".to_string(), "k".to_string());
        args2.insert("value".to_string(), "v".to_string());
        handlers[1].execute(&mut context, &args2).unwrap();

        let mut args3 = HashMap::new();
        args3.insert("key".to_string(), "k".to_string());
        handlers[2].execute(&mut context, &args3).unwrap();

        assert_eq!(context.log.len(), 2);
        assert_eq!(context.state.len(), 1);
    }

    #[test]
    fn test_context_isolation_between_commands() {
        // Verify that context is shared correctly
        let mut context = IntegrationContext::default();

        let set_cmd = SetCommand;
        let mut args = HashMap::new();
        args.insert("key".to_string(), "shared".to_string());
        args.insert("value".to_string(), "value".to_string());
        set_cmd.execute(&mut context, &args).unwrap();

        // Another command can see the state
        let get_cmd = GetCommand;
        let mut args2 = HashMap::new();
        args2.insert("key".to_string(), "shared".to_string());
        get_cmd.execute(&mut context, &args2).unwrap();

        assert!(context.log[0].contains("shared = value"));
    }

    #[test]
    fn test_error_propagation() {
        // Test that errors propagate correctly through the handler chain
        struct FailingCommand;

        impl CommandHandler for FailingCommand {
            fn execute(
                &self,
                _context: &mut dyn ExecutionContext,
                _args: &HashMap<String, String>,
            ) -> crate::error::Result<()> {
                Err(ExecutionError::CommandFailed(anyhow::anyhow!("Intentional failure")).into())
            }
        }

        let handler = FailingCommand;
        let mut context = IntegrationContext::default();
        let args = HashMap::new();

        let result = handler.execute(&mut context, &args);

        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("Intentional failure"));
    }

    #[test]
    fn test_validation_before_execution() {
        // Test the intended workflow: validate then execute
        struct ValidatingCommand;

        impl CommandHandler for ValidatingCommand {
            fn execute(
                &self,
                context: &mut dyn ExecutionContext,
                _args: &HashMap<String, String>,
            ) -> crate::error::Result<()> {
                let ctx = crate::context::downcast_mut::<IntegrationContext>(context).ok_or_else(
                    || ExecutionError::CommandFailed(anyhow::anyhow!("Wrong context type")),
                )?;
                ctx.log.push("executed".to_string());
                Ok(())
            }

            fn validate(&self, args: &HashMap<String, String>) -> crate::error::Result<()> {
                if !args.contains_key("required") {
                    return Err(ExecutionError::CommandFailed(anyhow::anyhow!(
                        "Missing required argument"
                    ))
                    .into());
                }
                Ok(())
            }
        }

        let handler = ValidatingCommand;
        let mut context = IntegrationContext::default();

        // Test validation failure
        let args_invalid = HashMap::new();
        assert!(handler.validate(&args_invalid).is_err());
        assert!(handler.execute(&mut context, &args_invalid).is_ok()); // Execute would work

        // Test validation success
        let mut args_valid = HashMap::new();
        args_valid.insert("required".to_string(), "value".to_string());
        assert!(handler.validate(&args_valid).is_ok());
        assert!(handler.execute(&mut context, &args_valid).is_ok());
    }

    #[test]
    fn test_command_handler_documentation_example() {
        // Test the example from module documentation
        #[derive(Default)]
        struct AppContext {
            counter: i32,
        }

        impl ExecutionContext for AppContext {
            fn as_any(&self) -> &dyn Any {
                self
            }
            fn as_any_mut(&mut self) -> &mut dyn Any {
                self
            }
        }

        struct IncrementCommand;

        impl CommandHandler for IncrementCommand {
            fn execute(
                &self,
                context: &mut dyn ExecutionContext,
                args: &HashMap<String, String>,
            ) -> crate::error::Result<()> {
                let ctx = crate::context::downcast_mut::<AppContext>(context).ok_or_else(|| {
                    ExecutionError::CommandFailed(anyhow::anyhow!("Wrong context type"))
                })?;

                let amount: i32 = args.get("amount").and_then(|s| s.parse().ok()).unwrap_or(1);

                ctx.counter += amount;
                Ok(())
            }
        }

        let handler = IncrementCommand;
        let mut context = AppContext::default();
        let mut args = HashMap::new();
        args.insert("amount".to_string(), "5".to_string());

        handler.execute(&mut context, &args).unwrap();
        assert_eq!(context.counter, 5);
    }

    #[test]
    fn test_complex_multi_step_workflow() {
        // Test a more complex workflow simulating real usage
        let mut context = IntegrationContext::default();

        // Step 1: Initialize some state
        let set_cmd = SetCommand;
        let mut args1 = HashMap::new();
        args1.insert("key".to_string(), "initialized".to_string());
        args1.insert("value".to_string(), "true".to_string());
        set_cmd.execute(&mut context, &args1).unwrap();

        // Step 2: Log the initialization
        let log_cmd = LogCommand;
        let mut args2 = HashMap::new();
        args2.insert("message".to_string(), "System initialized".to_string());
        log_cmd.execute(&mut context, &args2).unwrap();

        // Step 3: Set more state
        let mut args3 = HashMap::new();
        args3.insert("key".to_string(), "user".to_string());
        args3.insert("value".to_string(), "admin".to_string());
        set_cmd.execute(&mut context, &args3).unwrap();

        // Step 4: Query state
        let get_cmd = GetCommand;
        let mut args4 = HashMap::new();
        args4.insert("key".to_string(), "user".to_string());
        get_cmd.execute(&mut context, &args4).unwrap();

        // Verify the complete workflow
        assert_eq!(context.state.len(), 2);
        assert_eq!(context.log.len(), 2);
        assert_eq!(context.log[0], "System initialized");
        assert_eq!(context.log[1], "user = admin");
    }
}