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
//! Configuration schema definitions
//!
//! This module defines all data structures for representing
//! CLI/REPL configurations loaded from YAML or JSON files.
//!
//! # Main Components
//!
//! - [`CommandsConfig`]: Root configuration structure
//! - [`CommandDefinition`]: Individual command specification
//! - [`ArgumentType`]: Supported argument types
//! - [`ValidationRule`]: Validation constraints

use serde::{Deserialize, Serialize};

/// Complete configuration for CLI/REPL commands
///
/// This is the root structure deserialized from YAML/JSON files.
/// It contains metadata about the interface and all command definitions.
///
/// # Example YAML
///
/// ```yaml
/// metadata:
///   version: "1.0.0"
///   prompt: "myapp"
///   prompt_suffix: " > "
/// commands:
///   - name: hello
///     description: "Say hello"
///     # ... more fields
/// global_options: []
/// ```
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct CommandsConfig {
    /// Metadata about the application interface
    pub metadata: Metadata,

    /// List of all available commands
    pub commands: Vec<CommandDefinition>,

    /// Global options available to all commands
    #[serde(default)]
    pub global_options: Vec<OptionDefinition>,
}

/// Metadata for the CLI/REPL interface
///
/// Contains information about the application version
/// and prompt customization for REPL mode.
///
/// # Fields
///
/// - `version`: Application version string
/// - `prompt`: Command prompt prefix (e.g., "myapp")
/// - `prompt_suffix`: Suffix after prompt (e.g., " > ")
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct Metadata {
    /// Application version (e.g., "1.0.0")
    pub version: String,

    /// Prompt prefix displayed in REPL mode
    ///
    /// Example: "chrom-rs" will display as "chrom-rs > "
    pub prompt: String,

    /// Prompt suffix (typically " > " or ": ")
    #[serde(default = "default_prompt_suffix")]
    pub prompt_suffix: String,
}

/// Default prompt suffix
fn default_prompt_suffix() -> String {
    " > ".to_string()
}

/// Definition of a single command
///
/// Describes a command with its arguments, options, and validation rules.
/// Each command must have a corresponding handler implementation.
///
/// # Example
///
/// ```yaml
/// name: simulate
/// aliases: [sim, run]
/// description: "Run a simulation"
/// required: true
/// arguments:
///   - name: input_file
///     arg_type: path
///     required: true
///     description: "Input configuration file"
///     validation:
///       - must_exist: true
///       - extensions: [yaml, json]
/// options: []
/// implementation: "simulate_handler"
/// ```
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct CommandDefinition {
    /// Command name (used for invocation)
    pub name: String,

    /// Alternative names for the command
    #[serde(default)]
    pub aliases: Vec<String>,

    /// Human-readable description for help text
    pub description: String,

    /// Whether this command is required to be implemented
    ///
    /// If true, the application will fail to start if no handler is registered.
    #[serde(default)]
    pub required: bool,

    /// Positional arguments
    #[serde(default)]
    pub arguments: Vec<ArgumentDefinition>,

    /// Named options (flags)
    #[serde(default)]
    pub options: Vec<OptionDefinition>,

    /// Name of the handler implementation
    ///
    /// This string is used to match the command with its
    /// registered handler in the CommandRegistry.
    pub implementation: String,
}

/// Definition of a positional argument
///
/// Positional arguments are required in order and don't have
/// a flag prefix (unlike options).
///
/// # Example
///
/// ```yaml
/// name: input_file
/// arg_type: path
/// required: true
/// description: "Path to input file"
/// validation:
///   - must_exist: true
///   - extensions: [yaml, yml]
/// ```
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct ArgumentDefinition {
    /// Argument name (used in error messages and documentation)
    pub name: String,

    /// Expected type of the argument
    pub arg_type: ArgumentType,

    /// Whether the argument is mandatory
    pub required: bool,

    /// Human-readable description
    pub description: String,

    /// Validation rules to apply
    #[serde(default)]
    pub validation: Vec<ValidationRule>,
}

/// Definition of a named option (flag)
///
/// Options are optional (by default) and can be specified
/// with short (`-o`) or long (`--option`) forms.
///
/// # Example
///
/// ```yaml
/// name: output
/// short: o
/// long: output
/// option_type: path
/// required: false
/// default: "output.txt"
/// description: "Output file path"
/// choices: []
/// ```
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct OptionDefinition {
    /// Option name (internal identifier)
    pub name: String,

    /// Short form (single character, e.g., "o" for -o)
    pub short: Option<String>,

    /// Long form (e.g., "output" for --output)
    pub long: Option<String>,

    /// Expected type of the option value
    pub option_type: ArgumentType,

    /// Whether this option is mandatory
    #[serde(default)]
    pub required: bool,

    /// Default value if not specified
    pub default: Option<String>,

    /// Human-readable description
    pub description: String,

    /// Restricted set of allowed values
    ///
    /// If non-empty, the value must be one of these choices.
    #[serde(default)]
    pub choices: Vec<String>,
}

/// Supported argument and option types
///
/// These types are used for automatic parsing and validation
/// of user input.
///
/// # Serialization
///
/// Types are serialized as lowercase strings in YAML/JSON:
/// - `String` → "string"
/// - `Integer` → "integer"
/// - `Float` → "float"
/// - `Bool` → "bool"
/// - `Path` → "path"
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ArgumentType {
    /// UTF-8 string
    String,

    /// Signed integer (i64)
    Integer,

    /// Floating-point number (f64)
    Float,

    /// Boolean value (true/false, yes/no, 1/0)
    Bool,

    /// File system path
    ///
    /// Represents a path that may or may not exist,
    /// depending on validation rules.
    Path,
}

impl ArgumentType {
    /// Get the type name as a string for error messages
    ///
    /// # Example
    ///
    /// ```
    /// use dynamic_cli::config::schema::ArgumentType;
    ///
    /// assert_eq!(ArgumentType::Integer.as_str(), "integer");
    /// assert_eq!(ArgumentType::Path.as_str(), "path");
    /// ```
    pub fn as_str(&self) -> &'static str {
        match self {
            ArgumentType::String => "string",
            ArgumentType::Integer => "integer",
            ArgumentType::Float => "float",
            ArgumentType::Bool => "bool",
            ArgumentType::Path => "path",
        }
    }
}

/// Validation rules for arguments and options
///
/// These rules are applied after type parsing to enforce
/// additional constraints on values.
///
/// # Variants
///
/// - `MustExist`: For paths, require that the file/directory exists
/// - `Extensions`: For paths, restrict to specific file extensions
/// - `Range`: For numbers, enforce min/max bounds
///
/// # Serialization
///
/// Rules use untagged enum serialization:
///
/// ```yaml
/// # MustExist
/// - must_exist: true
///
/// # Extensions
/// - extensions: [yaml, yml, json]
///
/// # Range
/// - min: 0.0
///   max: 100.0
/// ```
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub enum ValidationRule {
    /// Require that a path exists on the file system
    MustExist { must_exist: bool },

    /// Restrict file extensions (for path arguments)
    ///
    /// Extensions should be specified without the leading dot.
    /// Example: `["yaml", "yml"]` matches "config.yaml" and "data.yml"
    Extensions { extensions: Vec<String> },

    /// Enforce numeric range constraints
    ///
    /// Either or both bounds can be specified:
    /// - `min: Some(0.0), max: None` → x ≥ 0
    /// - `min: None, max: Some(100.0)` → x ≤ 100
    /// - `min: Some(0.0), max: Some(100.0)` → 0 ≤ x ≤ 100
    Range { min: Option<f64>, max: Option<f64> },
}

impl CommandsConfig {
    /// Create a minimal valid configuration for testing
    ///
    /// This is useful for unit tests and examples.
    ///
    /// # Example
    ///
    /// ```
    /// use dynamic_cli::config::schema::CommandsConfig;
    ///
    /// let config = CommandsConfig::minimal();
    /// assert_eq!(config.metadata.version, "0.1.0");
    /// assert!(config.commands.is_empty());
    /// ```
    #[cfg(test)]
    pub fn minimal() -> Self {
        Self {
            metadata: Metadata {
                version: "0.1.0".to_string(),
                prompt: "test".to_string(),
                prompt_suffix: " > ".to_string(),
            },
            commands: vec![],
            global_options: vec![],
        }
    }
}

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

    #[test]
    fn test_argument_type_as_str() {
        assert_eq!(ArgumentType::String.as_str(), "string");
        assert_eq!(ArgumentType::Integer.as_str(), "integer");
        assert_eq!(ArgumentType::Float.as_str(), "float");
        assert_eq!(ArgumentType::Bool.as_str(), "bool");
        assert_eq!(ArgumentType::Path.as_str(), "path");
    }

    #[test]
    fn test_default_prompt_suffix() {
        assert_eq!(default_prompt_suffix(), " > ");
    }

    #[test]
    fn test_minimal_config() {
        let config = CommandsConfig::minimal();

        assert_eq!(config.metadata.version, "0.1.0");
        assert_eq!(config.metadata.prompt, "test");
        assert_eq!(config.metadata.prompt_suffix, " > ");
        assert!(config.commands.is_empty());
        assert!(config.global_options.is_empty());
    }

    #[test]
    fn test_deserialize_argument_type() {
        // Test YAML deserialization of ArgumentType
        let yaml = r#"
            type: string
        "#;

        #[derive(Deserialize)]
        struct TestStruct {
            #[serde(rename = "type")]
            type_field: ArgumentType,
        }

        let result: TestStruct = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(result.type_field, ArgumentType::String);
    }

    #[test]
    fn test_deserialize_metadata() {
        let yaml = r#"
            version: "1.0.0"
            prompt: "myapp"
            prompt_suffix: " $ "
        "#;

        let metadata: Metadata = serde_yaml::from_str(yaml).unwrap();

        assert_eq!(metadata.version, "1.0.0");
        assert_eq!(metadata.prompt, "myapp");
        assert_eq!(metadata.prompt_suffix, " $ ");
    }

    #[test]
    fn test_deserialize_metadata_with_default() {
        // Test that prompt_suffix gets default value if not specified
        let yaml = r#"
            version: "1.0.0"
            prompt: "myapp"
        "#;

        let metadata: Metadata = serde_yaml::from_str(yaml).unwrap();

        assert_eq!(metadata.prompt_suffix, " > ");
    }

    #[test]
    fn test_deserialize_command_definition() {
        let yaml = r#"
            name: test_cmd
            aliases: [tc, test]
            description: "A test command"
            required: true
            arguments: []
            options: []
            implementation: "test_handler"
        "#;

        let cmd: CommandDefinition = serde_yaml::from_str(yaml).unwrap();

        assert_eq!(cmd.name, "test_cmd");
        assert_eq!(cmd.aliases, vec!["tc", "test"]);
        assert_eq!(cmd.description, "A test command");
        assert!(cmd.required);
        assert_eq!(cmd.implementation, "test_handler");
    }

    #[test]
    fn test_deserialize_argument_definition() {
        let yaml = r#"
            name: input_file
            arg_type: path
            required: true
            description: "Input file"
            validation:
              - must_exist: true
              - extensions: [yaml, yml]
        "#;

        let arg: ArgumentDefinition = serde_yaml::from_str(yaml).unwrap();

        assert_eq!(arg.name, "input_file");
        assert_eq!(arg.arg_type, ArgumentType::Path);
        assert!(arg.required);
        assert_eq!(arg.description, "Input file");
        assert_eq!(arg.validation.len(), 2);
    }

    #[test]
    fn test_deserialize_option_definition() {
        let yaml = r#"
            name: output
            short: o
            long: output
            option_type: path
            required: false
            default: "out.txt"
            description: "Output file"
            choices: []
        "#;

        let opt: OptionDefinition = serde_yaml::from_str(yaml).unwrap();

        assert_eq!(opt.name, "output");
        assert_eq!(opt.short, Some("o".to_string()));
        assert_eq!(opt.long, Some("output".to_string()));
        assert_eq!(opt.option_type, ArgumentType::Path);
        assert!(!opt.required);
        assert_eq!(opt.default, Some("out.txt".to_string()));
    }

    #[test]
    fn test_deserialize_validation_rule_must_exist() {
        let yaml = r#"
            must_exist: true
        "#;

        let rule: ValidationRule = serde_yaml::from_str(yaml).unwrap();

        assert_eq!(rule, ValidationRule::MustExist { must_exist: true });
    }

    #[test]
    fn test_deserialize_validation_rule_extensions() {
        let yaml = r#"
            extensions: [yaml, yml, json]
        "#;

        let rule: ValidationRule = serde_yaml::from_str(yaml).unwrap();

        match rule {
            ValidationRule::Extensions { extensions } => {
                assert_eq!(extensions, vec!["yaml", "yml", "json"]);
            }
            _ => panic!("Wrong variant"),
        }
    }

    #[test]
    fn test_deserialize_validation_rule_range() {
        let yaml = r#"
            min: 0.0
            max: 100.0
        "#;

        let rule: ValidationRule = serde_yaml::from_str(yaml).unwrap();

        match rule {
            ValidationRule::Range { min, max } => {
                assert_eq!(min, Some(0.0));
                assert_eq!(max, Some(100.0));
            }
            _ => panic!("Wrong variant"),
        }
    }

    #[test]
    fn test_deserialize_full_config() {
        let yaml = r#"
            metadata:
              version: "1.0.0"
              prompt: "test"
              prompt_suffix: " > "
            commands:
              - name: hello
                aliases: []
                description: "Say hello"
                required: false
                arguments: []
                options: []
                implementation: "hello_handler"
            global_options: []
        "#;

        let config: CommandsConfig = serde_yaml::from_str(yaml).unwrap();

        assert_eq!(config.metadata.version, "1.0.0");
        assert_eq!(config.commands.len(), 1);
        assert_eq!(config.commands[0].name, "hello");
    }

    #[test]
    fn test_serialize_and_deserialize_roundtrip() {
        let original = CommandsConfig {
            metadata: Metadata {
                version: "1.0.0".to_string(),
                prompt: "test".to_string(),
                prompt_suffix: " > ".to_string(),
            },
            commands: vec![CommandDefinition {
                name: "cmd1".to_string(),
                aliases: vec!["c1".to_string()],
                description: "Test command".to_string(),
                required: true,
                arguments: vec![],
                options: vec![],
                implementation: "handler1".to_string(),
            }],
            global_options: vec![],
        };

        // Serialize to YAML
        let yaml = serde_yaml::to_string(&original).unwrap();

        // Deserialize back
        let deserialized: CommandsConfig = serde_yaml::from_str(&yaml).unwrap();

        assert_eq!(original, deserialized);
    }

    #[test]
    fn test_json_deserialization() {
        let json = r#"
        {
            "metadata": {
                "version": "1.0.0",
                "prompt": "test",
                "prompt_suffix": " > "
            },
            "commands": [],
            "global_options": []
        }
        "#;

        let config: CommandsConfig = serde_json::from_str(json).unwrap();

        assert_eq!(config.metadata.version, "1.0.0");
        assert_eq!(config.commands.len(), 0);
    }
}