dynamic_cli/parser/
mod.rs

1//! Command-line and REPL parsing
2//!
3//! This module provides comprehensive parsing functionality for both
4//! traditional command-line interfaces (CLI) and interactive REPL mode.
5//!
6//! # Module Structure
7//!
8//! The parser module consists of three main components:
9//!
10//! - [`type_parser`]: Type conversion functions (string → typed values)
11//! - [`cli_parser`]: CLI argument parser (Unix-style options)
12//! - [`repl_parser`]: REPL line parser (interactive mode)
13//!
14//! # Architecture
15//!
16//! ```text
17//! ┌─────────────────────────────────────────┐
18//! │         User Input                      │
19//! │   "process file.txt --verbose"          │
20//! └──────────────┬──────────────────────────┘
21//!                │
22//!                ▼
23//! ┌─────────────────────────────────────────┐
24//! │       ReplParser (REPL mode)            │
25//! │   - Tokenize line                       │
26//! │   - Resolve command name via Registry   │
27//! │   - Delegate to CliParser               │
28//! └──────────────┬──────────────────────────┘
29//!                │
30//!                ▼
31//! ┌─────────────────────────────────────────┐
32//! │       CliParser (CLI mode)              │
33//! │   - Parse positional arguments          │
34//! │   - Parse options (-v, --verbose)       │
35//! │   - Apply defaults                      │
36//! │   - Use TypeParser for conversion       │
37//! └──────────────┬──────────────────────────┘
38//!                │
39//!                ▼
40//! ┌─────────────────────────────────────────┐
41//! │       TypeParser                        │
42//! │   - Convert strings to typed values     │
43//! │   - Validate type constraints           │
44//! └──────────────┬──────────────────────────┘
45//!                │
46//!                ▼
47//! ┌─────────────────────────────────────────┐
48//! │   HashMap<String, String>               │
49//! │   {"input": "file.txt",                 │
50//! │    "verbose": "true"}                   │
51//! └─────────────────────────────────────────┘
52//! ```
53//!
54//! # Design Principles
55//!
56//! ## 1. Separation of Concerns
57//!
58//! Each parser has a specific responsibility:
59//! - **TypeParser**: Handles type conversion only
60//! - **CliParser**: Handles CLI syntax (options, arguments)
61//! - **ReplParser**: Handles REPL-specific concerns (tokenization, command resolution)
62//!
63//! ## 2. Composability
64//!
65//! Parsers compose naturally:
66//! - ReplParser uses CliParser for argument parsing
67//! - CliParser uses TypeParser for type conversion
68//! - Each can be used independently when needed
69//!
70//! ## 3. Error Clarity
71//!
72//! All parsers provide detailed error messages with:
73//! - Clear descriptions of what went wrong
74//! - Suggestions for typos (via Levenshtein distance)
75//! - Hints for correct usage
76//!
77//! # Usage Examples
78//!
79//! ## CLI Mode (Direct Argument Parsing)
80//!
81//! ```
82//! use dynamic_cli::parser::cli_parser::CliParser;
83//! use dynamic_cli::config::schema::{CommandDefinition, ArgumentDefinition, ArgumentType};
84//!
85//! let definition = CommandDefinition {
86//!     name: "process".to_string(),
87//!     aliases: vec![],
88//!     description: "Process files".to_string(),
89//!     required: false,
90//!     arguments: vec![
91//!         ArgumentDefinition {
92//!             name: "input".to_string(),
93//!             arg_type: ArgumentType::Path,
94//!             required: true,
95//!             description: "Input file".to_string(),
96//!             validation: vec![],
97//!         }
98//!     ],
99//!     options: vec![],
100//!     implementation: "handler".to_string(),
101//! };
102//!
103//! let parser = CliParser::new(&definition);
104//! let args = vec!["input.txt".to_string()];
105//! let parsed = parser.parse(&args).unwrap();
106//!
107//! assert_eq!(parsed.get("input"), Some(&"input.txt".to_string()));
108//! ```
109//!
110//! ## REPL Mode (Interactive Parsing)
111//!
112//! ```no_run
113//! use dynamic_cli::parser::repl_parser::ReplParser;
114//! use dynamic_cli::registry::CommandRegistry;
115//!
116//! let registry = CommandRegistry::new();
117//! // ... register commands ...
118//!
119//! let parser = ReplParser::new(&registry);
120//!
121//! // Parse user input
122//! let line = "process input.txt --verbose";
123//! let parsed = parser.parse_line(line).unwrap();
124//!
125//! println!("Command: {}", parsed.command_name);
126//! println!("Arguments: {:?}", parsed.arguments);
127//! ```
128//!
129//! ## Type Parsing (Low-Level)
130//!
131//! ```
132//! use dynamic_cli::parser::type_parser::{parse_integer, parse_bool};
133//!
134//! let number = parse_integer("42").unwrap();
135//! assert_eq!(number, 42);
136//!
137//! let flag = parse_bool("yes").unwrap();
138//! assert_eq!(flag, true);
139//! ```
140//!
141//! # Error Handling
142//!
143//! All parsing functions return [`Result<T>`] where errors are instances
144//! of [`ParseError`]. Common error scenarios:
145//!
146//! - **Unknown command**: User typed a non-existent command
147//!   ```text
148//!   Error: Unknown command: 'simulat'
149//!   ? Did you mean:
150//!     • simulate
151//!     • validation
152//!   ```
153//!
154//! - **Type mismatch**: Value cannot be converted to expected type
155//!   ```text
156//!   Error: Failed to parse count as integer: 'abc'
157//!   ```
158//!
159//! - **Missing argument**: Required argument not provided
160//!   ```text
161//!   Error: Missing required argument: input for command 'process'
162//!   ```
163//!
164//! # Performance Considerations
165//!
166//! - **Type parsing**: O(1) for most types, O(n) for string length
167//! - **CLI parsing**: O(n) where n = number of arguments
168//! - **REPL parsing**: O(m + n) where m = line length (tokenization), n = arguments
169//! - **Command resolution**: O(1) via HashMap lookup in registry
170//!
171//! # Thread Safety
172//!
173//! All parsers are:
174//! - **Stateless**: Can be used concurrently from multiple threads
175//! - **Borrowing**: Use references to definitions/registry (no ownership)
176//! - **Reusable**: Can parse multiple commands with the same parser instance
177//!
178//! # Future Extensions
179//!
180//! Potential enhancements for future versions:
181//! - Support for subcommands (e.g., `git commit`)
182//! - Environment variable expansion
183//! - Glob pattern matching for paths
184//! - Command history and auto-completion hints
185//! - Streaming parser for very large inputs
186
187use crate::error::Result;
188
189// Public submodules
190pub mod cli_parser;
191pub mod repl_parser;
192pub mod type_parser;
193
194// Re-export commonly used types
195pub use cli_parser::CliParser;
196pub use repl_parser::{ParsedCommand, ReplParser};
197
198#[cfg(test)]
199mod tests {
200    use super::*;
201    use crate::config::schema::{
202        ArgumentDefinition, ArgumentType, CommandDefinition, OptionDefinition,
203    };
204    use crate::context::ExecutionContext;
205    use crate::executor::CommandHandler;
206    use crate::registry::CommandRegistry;
207    use std::collections::HashMap;
208
209    // Dummy handler for integration tests
210    struct IntegrationTestHandler;
211
212    impl CommandHandler for IntegrationTestHandler {
213        fn execute(
214            &self,
215            _context: &mut dyn ExecutionContext,
216            _args: &HashMap<String, String>,
217        ) -> Result<()> {
218            Ok(())
219        }
220    }
221
222    /// Helper to create a comprehensive test command
223    fn create_comprehensive_command() -> CommandDefinition {
224        CommandDefinition {
225            name: "analyze".to_string(),
226            aliases: vec!["analyse".to_string(), "check".to_string()],
227            description: "Analyze data files".to_string(),
228            required: false,
229            arguments: vec![
230                ArgumentDefinition {
231                    name: "input".to_string(),
232                    arg_type: ArgumentType::Path,
233                    required: true,
234                    description: "Input data file".to_string(),
235                    validation: vec![],
236                },
237                ArgumentDefinition {
238                    name: "output".to_string(),
239                    arg_type: ArgumentType::Path,
240                    required: false,
241                    description: "Output report file".to_string(),
242                    validation: vec![],
243                },
244            ],
245            options: vec![
246                OptionDefinition {
247                    name: "verbose".to_string(),
248                    short: Some("v".to_string()),
249                    long: Some("verbose".to_string()),
250                    option_type: ArgumentType::Bool,
251                    required: false,
252                    default: Some("false".to_string()),
253                    description: "Enable verbose output".to_string(),
254                    choices: vec![],
255                },
256                OptionDefinition {
257                    name: "iterations".to_string(),
258                    short: Some("i".to_string()),
259                    long: Some("iterations".to_string()),
260                    option_type: ArgumentType::Integer,
261                    required: false,
262                    default: Some("100".to_string()),
263                    description: "Number of iterations".to_string(),
264                    choices: vec![],
265                },
266                OptionDefinition {
267                    name: "threshold".to_string(),
268                    short: Some("t".to_string()),
269                    long: Some("threshold".to_string()),
270                    option_type: ArgumentType::Float,
271                    required: false,
272                    default: Some("0.5".to_string()),
273                    description: "Analysis threshold".to_string(),
274                    choices: vec![],
275                },
276            ],
277            implementation: "analyze_handler".to_string(),
278        }
279    }
280
281    // ========================================================================
282    // Integration tests: CLI Parser
283    // ========================================================================
284
285    #[test]
286    fn test_cli_parser_integration_minimal() {
287        let definition = create_comprehensive_command();
288        let parser = CliParser::new(&definition);
289
290        let args = vec!["data.csv".to_string()];
291        let result = parser.parse(&args).unwrap();
292
293        // Required argument
294        assert_eq!(result.get("input"), Some(&"data.csv".to_string()));
295
296        // Defaults should be applied
297        assert_eq!(result.get("verbose"), Some(&"false".to_string()));
298        assert_eq!(result.get("iterations"), Some(&"100".to_string()));
299        assert_eq!(result.get("threshold"), Some(&"0.5".to_string()));
300    }
301
302    #[test]
303    fn test_cli_parser_integration_full() {
304        let definition = create_comprehensive_command();
305        let parser = CliParser::new(&definition);
306
307        let args = vec![
308            "data.csv".to_string(),
309            "report.txt".to_string(),
310            "--verbose".to_string(),
311            "--iterations=200".to_string(),
312            "-t".to_string(),
313            "0.75".to_string(),
314        ];
315        let result = parser.parse(&args).unwrap();
316
317        assert_eq!(result.get("input"), Some(&"data.csv".to_string()));
318        assert_eq!(result.get("output"), Some(&"report.txt".to_string()));
319        assert_eq!(result.get("verbose"), Some(&"true".to_string()));
320        assert_eq!(result.get("iterations"), Some(&"200".to_string()));
321        assert_eq!(result.get("threshold"), Some(&"0.75".to_string()));
322    }
323
324    #[test]
325    fn test_cli_parser_integration_mixed_options() {
326        let definition = create_comprehensive_command();
327        let parser = CliParser::new(&definition);
328
329        // Options can be interspersed with positional arguments
330        let args = vec![
331            "--verbose".to_string(),
332            "data.csv".to_string(),
333            "-i200".to_string(),
334            "report.txt".to_string(),
335            "--threshold".to_string(),
336            "0.9".to_string(),
337        ];
338        let result = parser.parse(&args).unwrap();
339
340        assert_eq!(result.get("input"), Some(&"data.csv".to_string()));
341        assert_eq!(result.get("output"), Some(&"report.txt".to_string()));
342        assert_eq!(result.get("verbose"), Some(&"true".to_string()));
343        assert_eq!(result.get("iterations"), Some(&"200".to_string()));
344        assert_eq!(result.get("threshold"), Some(&"0.9".to_string()));
345    }
346
347    // ========================================================================
348    // Integration tests: REPL Parser
349    // ========================================================================
350
351    #[test]
352    fn test_repl_parser_integration_simple() {
353        let mut registry = CommandRegistry::new();
354        let definition = create_comprehensive_command();
355        registry
356            .register(definition, Box::new(IntegrationTestHandler))
357            .unwrap();
358
359        let parser = ReplParser::new(&registry);
360
361        let parsed = parser.parse_line("analyze data.csv").unwrap();
362        assert_eq!(parsed.command_name, "analyze");
363        assert_eq!(parsed.arguments.get("input"), Some(&"data.csv".to_string()));
364    }
365
366    #[test]
367    fn test_repl_parser_integration_alias() {
368        let mut registry = CommandRegistry::new();
369        let definition = create_comprehensive_command();
370        registry
371            .register(definition, Box::new(IntegrationTestHandler))
372            .unwrap();
373
374        let parser = ReplParser::new(&registry);
375
376        // Use alias instead of command name
377        let parsed = parser.parse_line("check data.csv --verbose").unwrap();
378        assert_eq!(parsed.command_name, "analyze"); // Resolves to canonical name
379        assert_eq!(parsed.arguments.get("input"), Some(&"data.csv".to_string()));
380        assert_eq!(parsed.arguments.get("verbose"), Some(&"true".to_string()));
381    }
382
383    #[test]
384    fn test_repl_parser_integration_quoted_paths() {
385        let mut registry = CommandRegistry::new();
386        let definition = create_comprehensive_command();
387        registry
388            .register(definition, Box::new(IntegrationTestHandler))
389            .unwrap();
390
391        let parser = ReplParser::new(&registry);
392
393        let parsed = parser
394            .parse_line(r#"analyze "/path/with spaces/data.csv" "output report.txt""#)
395            .unwrap();
396
397        assert_eq!(
398            parsed.arguments.get("input"),
399            Some(&"/path/with spaces/data.csv".to_string())
400        );
401        assert_eq!(
402            parsed.arguments.get("output"),
403            Some(&"output report.txt".to_string())
404        );
405    }
406
407    #[test]
408    fn test_repl_parser_integration_complex() {
409        let mut registry = CommandRegistry::new();
410        let definition = create_comprehensive_command();
411        registry
412            .register(definition, Box::new(IntegrationTestHandler))
413            .unwrap();
414
415        let parser = ReplParser::new(&registry);
416
417        let parsed = parser
418            .parse_line(r#"analyse "data file.csv" report.txt -v --iterations=500 -t 0.95"#)
419            .unwrap();
420
421        assert_eq!(parsed.command_name, "analyze");
422        assert_eq!(
423            parsed.arguments.get("input"),
424            Some(&"data file.csv".to_string())
425        );
426        assert_eq!(
427            parsed.arguments.get("output"),
428            Some(&"report.txt".to_string())
429        );
430        assert_eq!(parsed.arguments.get("verbose"), Some(&"true".to_string()));
431        assert_eq!(parsed.arguments.get("iterations"), Some(&"500".to_string()));
432        assert_eq!(parsed.arguments.get("threshold"), Some(&"0.95".to_string()));
433    }
434
435    // ========================================================================
436    // Integration tests: Type Parser
437    // ========================================================================
438
439    #[test]
440    fn test_type_parser_integration_all_types() {
441        use type_parser::parse_value;
442
443        // Test all argument types
444        assert!(parse_value("hello", ArgumentType::String).is_ok());
445        assert!(parse_value("42", ArgumentType::Integer).is_ok());
446        assert!(parse_value("3.14", ArgumentType::Float).is_ok());
447        assert!(parse_value("true", ArgumentType::Bool).is_ok());
448        assert!(parse_value("/path/to/file", ArgumentType::Path).is_ok());
449    }
450
451    #[test]
452    fn test_type_parser_integration_error_propagation() {
453        let definition = create_comprehensive_command();
454        let parser = CliParser::new(&definition);
455
456        // Invalid integer should fail
457        let args = vec![
458            "data.csv".to_string(),
459            "--iterations".to_string(),
460            "not_a_number".to_string(),
461        ];
462
463        let result = parser.parse(&args);
464        assert!(result.is_err());
465    }
466
467    // ========================================================================
468    // Integration tests: End-to-End Workflows
469    // ========================================================================
470
471    #[test]
472    fn test_workflow_cli_to_execution() {
473        // Simulate: User provides CLI args → Parser → Handler could execute
474
475        let definition = create_comprehensive_command();
476        let parser = CliParser::new(&definition);
477
478        let args = vec!["data.csv".to_string(), "-v".to_string()];
479        let parsed = parser.parse(&args).unwrap();
480
481        // Verify parsed data is ready for execution
482        assert!(parsed.contains_key("input"));
483        assert!(parsed.contains_key("verbose"));
484        assert_eq!(parsed.get("verbose"), Some(&"true".to_string()));
485    }
486
487    #[test]
488    fn test_workflow_repl_to_execution() {
489        // Simulate: User types in REPL → Parser → Handler could execute
490
491        let mut registry = CommandRegistry::new();
492        let definition = create_comprehensive_command();
493        registry
494            .register(definition, Box::new(IntegrationTestHandler))
495            .unwrap();
496
497        let parser = ReplParser::new(&registry);
498
499        let line = "analyze data.csv --verbose --iterations=1000";
500        let parsed = parser.parse_line(line).unwrap();
501
502        // Verify parsed command is ready for execution
503        assert_eq!(parsed.command_name, "analyze");
504        assert!(parsed.arguments.contains_key("input"));
505        assert_eq!(parsed.arguments.get("verbose"), Some(&"true".to_string()));
506        assert_eq!(
507            parsed.arguments.get("iterations"),
508            Some(&"1000".to_string())
509        );
510    }
511
512    #[test]
513    fn test_workflow_typo_suggestions() {
514        let mut registry = CommandRegistry::new();
515        let definition = create_comprehensive_command();
516        registry
517            .register(definition, Box::new(IntegrationTestHandler))
518            .unwrap();
519
520        let parser = ReplParser::new(&registry);
521
522        // User makes a typo
523        let result = parser.parse_line("analyz data.csv");
524
525        assert!(result.is_err());
526
527        // Error should contain suggestions
528        let error = result.unwrap_err();
529        let error_msg = format!("{}", error);
530        assert!(error_msg.contains("Unknown command"));
531    }
532
533    // ========================================================================
534    // Re-export verification tests
535    // ========================================================================
536
537    #[test]
538    fn test_reexports_accessible() {
539        // Verify that re-exported types are accessible from module root
540
541        let definition = create_comprehensive_command();
542
543        // CliParser should be accessible
544        let _cli_parser = CliParser::new(&definition);
545
546        // ReplParser should be accessible (needs registry)
547        let registry = CommandRegistry::new();
548        let _repl_parser = ReplParser::new(&registry);
549
550        // ParsedCommand should be accessible
551        let _parsed = ParsedCommand {
552            command_name: "test".to_string(),
553            arguments: HashMap::new(),
554        };
555    }
556}