nut-shell 0.1.2

A lightweight command-line interface library for embedded systems
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
653
654
655
656
657
658
659
660
661
662
663
664
//! Tab completion for commands and paths.
//!
//! Provides smart completion with prefix matching and directory handling.
//! Uses stub function pattern - module always exists, functions return empty when disabled.

#![cfg_attr(not(feature = "completion"), allow(unused_variables))]

use crate::auth::AccessLevel;
use crate::error::CliError;
use crate::tree::Directory;

#[cfg(feature = "completion")]
use crate::tree::Node;

/// Tab completion result with type-safe variants for different match outcomes.
#[derive(Debug, Clone, PartialEq)]
pub enum CompletionResult<const MAX_MATCHES: usize> {
    /// No matches found for the input prefix
    None,

    /// Exactly one match found (auto-completable)
    Single {
        /// The completed name (with "/" appended for directories)
        // TODO: Use C::MAX_INPUT when const generics stabilize
        completion: heapless::String<128>,

        /// True if the match is a directory
        is_directory: bool,
    },

    /// Multiple matches found (show options to user)
    Multiple {
        /// Common prefix of all matches
        // TODO: Use C::MAX_INPUT when const generics stabilize
        common_prefix: heapless::String<128>,

        /// All matching node names (for display)
        // TODO: Consider using C::MAX_INPUT or a separate config constant when const generics stabilize
        all_matches: heapless::Vec<heapless::String<64>, MAX_MATCHES>,
    },
}

impl<const MAX_MATCHES: usize> CompletionResult<MAX_MATCHES> {
    /// Create empty completion result.
    pub fn empty() -> Self {
        Self::None
    }
}

// ============================================================================
// Feature-enabled implementation
// ============================================================================

/// Suggest completions for partial input using prefix matching.
/// Directories get "/" appended for single matches.
#[cfg(feature = "completion")]
pub fn suggest_completions<L: AccessLevel, const MAX_MATCHES: usize>(
    dir: &Directory<L>,
    input: &str,
    current_user: Option<&crate::auth::User<L>>,
) -> Result<CompletionResult<MAX_MATCHES>, CliError> {
    // Find all matching nodes
    let mut matches: heapless::Vec<(&str, bool), MAX_MATCHES> = heapless::Vec::new();

    for child in dir.children.iter() {
        // Check access control
        let node_level = match child {
            Node::Command(cmd) => cmd.access_level,
            Node::Directory(d) => d.access_level,
        };

        // Filter by access level
        if let Some(user) = current_user
            && user.access_level < node_level
        {
            continue; // User lacks access, skip this node
        }

        let name = child.name();
        let is_dir = child.is_directory();

        // Check prefix match
        if name.starts_with(input) {
            matches
                .push((name, is_dir))
                .map_err(|_| CliError::BufferFull)?;
        }
    }

    // No matches
    if matches.is_empty() {
        return Ok(CompletionResult::None);
    }

    // Single match - complete!
    if matches.len() == 1 {
        let (name, is_dir) = matches[0];
        let mut completion = heapless::String::new();
        completion
            .push_str(name)
            .map_err(|_| CliError::BufferFull)?;

        // Auto-append "/" for directories
        if is_dir {
            completion.push('/').map_err(|_| CliError::BufferFull)?;
        }

        return Ok(CompletionResult::Single {
            completion,
            is_directory: is_dir,
        });
    }

    // Multiple matches - find common prefix
    let common_prefix_str = find_common_prefix(&matches);

    let mut common_prefix = heapless::String::new();
    common_prefix
        .push_str(common_prefix_str)
        .map_err(|_| CliError::BufferFull)?;

    // Collect all match names for display
    // TODO: Consider using C::MAX_INPUT or a separate config constant when const generics stabilize
    let mut all_matches: heapless::Vec<heapless::String<64>, MAX_MATCHES> = heapless::Vec::new();
    for (name, _) in matches.iter() {
        let mut match_str = heapless::String::new();
        match_str.push_str(name).map_err(|_| CliError::BufferFull)?;
        all_matches
            .push(match_str)
            .map_err(|_| CliError::BufferFull)?;
    }

    Ok(CompletionResult::Multiple {
        common_prefix,
        all_matches,
    })
}

/// Find longest common prefix among multiple matches.
#[cfg(feature = "completion")]
fn find_common_prefix<'a>(matches: &[(&'a str, bool)]) -> &'a str {
    if matches.is_empty() {
        return "";
    }

    let first = matches[0].0;

    // Find shortest match length
    let min_len = matches.iter().map(|(s, _)| s.len()).min().unwrap_or(0);

    // Find common prefix length
    let mut prefix_len = 0;
    for i in 0..min_len {
        let ch = first.as_bytes()[i];
        let all_match = matches.iter().all(|(s, _)| s.as_bytes()[i] == ch);
        if all_match {
            prefix_len = i + 1;
        } else {
            break;
        }
    }

    &first[..prefix_len]
}

// ============================================================================
// Feature-disabled stub implementation
// ============================================================================

/// Stub implementation when completion feature is disabled.
#[cfg(not(feature = "completion"))]
pub fn suggest_completions<L: AccessLevel, const MAX_MATCHES: usize>(
    _dir: &Directory<L>,
    _input: &str,
    _current_user: Option<&crate::auth::User<L>>,
) -> Result<CompletionResult<MAX_MATCHES>, CliError> {
    Ok(CompletionResult::empty())
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::AccessLevel;
    use crate::tree::{CommandKind, CommandMeta, Directory, Node};

    // Test access level
    #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
    enum TestLevel {
        Guest = 0,
        User = 1,
        Admin = 2,
    }

    impl AccessLevel for TestLevel {
        fn from_str(s: &str) -> Option<Self> {
            match s {
                "Guest" => Some(Self::Guest),
                "User" => Some(Self::User),
                "Admin" => Some(Self::Admin),
                _ => None,
            }
        }

        fn as_str(&self) -> &'static str {
            match self {
                Self::Guest => "Guest",
                Self::User => "User",
                Self::Admin => "Admin",
            }
        }
    }

    // Test fixtures
    const CMD_STATUS: CommandMeta<TestLevel> = CommandMeta {
        id: "status",
        name: "status",
        description: "Show status",
        access_level: TestLevel::User,
        kind: CommandKind::Sync,
        min_args: 0,
        max_args: 0,
    };

    const CMD_START: CommandMeta<TestLevel> = CommandMeta {
        id: "start",
        name: "start",
        description: "Start service",
        access_level: TestLevel::User,
        kind: CommandKind::Sync,
        min_args: 0,
        max_args: 1,
    };

    const CMD_STOP: CommandMeta<TestLevel> = CommandMeta {
        id: "stop",
        name: "stop",
        description: "Stop service",
        access_level: TestLevel::User,
        kind: CommandKind::Sync,
        min_args: 0,
        max_args: 0,
    };

    const CMD_REBOOT: CommandMeta<TestLevel> = CommandMeta {
        id: "reboot",
        name: "reboot",
        description: "Reboot system",
        access_level: TestLevel::Admin,
        kind: CommandKind::Sync,
        min_args: 0,
        max_args: 0,
    };

    const DIR_SYSTEM: Directory<TestLevel> = Directory {
        name: "system",
        children: &[],
        access_level: TestLevel::User,
    };

    const DIR_SERVICES: Directory<TestLevel> = Directory {
        name: "services",
        children: &[],
        access_level: TestLevel::User,
    };

    const TEST_DIR: Directory<TestLevel> = Directory {
        name: "test",
        children: &[
            Node::Command(&CMD_STATUS),
            Node::Command(&CMD_START),
            Node::Command(&CMD_STOP),
            Node::Command(&CMD_REBOOT),
            Node::Directory(&DIR_SYSTEM),
            Node::Directory(&DIR_SERVICES),
        ],
        access_level: TestLevel::Guest,
    };

    #[test]
    #[cfg(feature = "completion")]
    fn test_single_match_command() {
        let result = suggest_completions::<TestLevel, 16>(&TEST_DIR, "reb", None).unwrap();

        match result {
            CompletionResult::Single {
                completion,
                is_directory,
            } => {
                assert_eq!(completion.as_str(), "reboot");
                assert!(!is_directory);
            }
            _ => panic!("Expected Single variant"),
        }
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_single_match_directory() {
        let result = suggest_completions::<TestLevel, 16>(&TEST_DIR, "syst", None).unwrap();

        match result {
            CompletionResult::Single {
                completion,
                is_directory,
            } => {
                assert_eq!(completion.as_str(), "system/");
                assert!(is_directory);
            }
            _ => panic!("Expected Single variant"),
        }
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_multiple_matches_with_common_prefix() {
        let result = suggest_completions::<TestLevel, 16>(&TEST_DIR, "st", None).unwrap();

        match result {
            CompletionResult::Multiple {
                common_prefix,
                all_matches,
            } => {
                // Common prefix is "st" for "status", "start", "stop"
                assert_eq!(common_prefix.as_str(), "st");
                assert_eq!(all_matches.len(), 3);

                // Check all matches present (verify each is in the result)
                let match_names: [&str; 3] = ["status", "start", "stop"];
                for expected in &match_names {
                    assert!(
                        all_matches.iter().any(|m| m.as_str() == *expected),
                        "Expected to find '{}' in matches",
                        expected
                    );
                }
            }
            _ => panic!("Expected Multiple variant"),
        }
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_multiple_matches_directories() {
        let result = suggest_completions::<TestLevel, 16>(&TEST_DIR, "s", None).unwrap();

        match result {
            CompletionResult::Multiple {
                common_prefix,
                all_matches,
            } => {
                // Should match: status, start, stop, system, services
                assert_eq!(common_prefix.as_str(), "s");
                assert_eq!(all_matches.len(), 5);
            }
            _ => panic!("Expected Multiple variant"),
        }
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_no_matches() {
        let result = suggest_completions::<TestLevel, 16>(&TEST_DIR, "xyz", None).unwrap();

        match result {
            CompletionResult::None => {
                // Expected - no matches
            }
            _ => panic!("Expected None variant"),
        }
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_exact_match_command() {
        let result = suggest_completions::<TestLevel, 16>(&TEST_DIR, "status", None).unwrap();

        match result {
            CompletionResult::Single {
                completion,
                is_directory,
            } => {
                assert_eq!(completion.as_str(), "status");
                assert!(!is_directory);
            }
            _ => panic!("Expected Single variant"),
        }
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_exact_match_directory() {
        let result = suggest_completions::<TestLevel, 16>(&TEST_DIR, "system", None).unwrap();

        match result {
            CompletionResult::Single {
                completion,
                is_directory,
            } => {
                assert_eq!(completion.as_str(), "system/");
                assert!(is_directory);
            }
            _ => panic!("Expected Single variant"),
        }
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_empty_input_matches_all() {
        let result = suggest_completions::<TestLevel, 16>(&TEST_DIR, "", None).unwrap();

        match result {
            CompletionResult::Multiple { all_matches, .. } => {
                // Empty input matches everything
                assert_eq!(all_matches.len(), 6); // 4 commands + 2 directories
            }
            _ => panic!("Expected Multiple variant"),
        }
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_case_sensitive_matching() {
        let result = suggest_completions::<TestLevel, 16>(&TEST_DIR, "ST", None).unwrap();

        match result {
            CompletionResult::None => {
                // Expected - no matches (case-sensitive)
            }
            _ => panic!("Expected None variant"),
        }
    }

    #[test]
    #[cfg(not(feature = "completion"))]
    fn test_stub_returns_empty() {
        let result = suggest_completions::<TestLevel, 16>(&TEST_DIR, "st", None).unwrap();

        match result {
            CompletionResult::None => {
                // Expected - stub always returns empty
            }
            _ => panic!("Expected None variant"),
        }
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_access_control_filtering() {
        use crate::auth::User;

        // Create guest user (no access to Admin commands)
        let guest_user = User {
            username: {
                let mut s = heapless::String::new();
                s.push_str("guest").unwrap();
                s
            },
            access_level: TestLevel::Guest,
            #[cfg(feature = "authentication")]
            password_hash: [0u8; 32],
            #[cfg(feature = "authentication")]
            salt: [0u8; 16],
        };

        // "r" should NOT match "reboot" (Admin only) for guest user
        let result =
            suggest_completions::<TestLevel, 16>(&TEST_DIR, "r", Some(&guest_user)).unwrap();

        match result {
            CompletionResult::None => {
                // Expected - no access to reboot command
            }
            _ => panic!("Expected None variant"),
        }

        // Create admin user
        let admin_user = User {
            username: {
                let mut s = heapless::String::new();
                s.push_str("admin").unwrap();
                s
            },
            access_level: TestLevel::Admin,
            #[cfg(feature = "authentication")]
            password_hash: [0u8; 32],
            #[cfg(feature = "authentication")]
            salt: [0u8; 16],
        };

        // "r" should match "reboot" for admin user
        let result =
            suggest_completions::<TestLevel, 16>(&TEST_DIR, "r", Some(&admin_user)).unwrap();

        match result {
            CompletionResult::Single { completion, .. } => {
                assert_eq!(completion.as_str(), "reboot");
            }
            _ => panic!("Expected Single variant"),
        }
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_common_prefix_calculation() {
        // Test internal helper
        let matches = [("start", false), ("status", false), ("stop", false)];
        let prefix = find_common_prefix(&matches);
        assert_eq!(prefix, "st");

        let matches = [("network", false), ("netscan", false)];
        let prefix = find_common_prefix(&matches);
        assert_eq!(prefix, "net");

        let matches = [("abc", false), ("xyz", false)];
        let prefix = find_common_prefix(&matches);
        assert_eq!(prefix, ""); // No common prefix
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_max_matches_exceeded() {
        // Create directory with more nodes than MAX_MATCHES
        const CMD1: CommandMeta<TestLevel> = CommandMeta {
            id: "a1",
            name: "a1",
            description: "Command 1",
            access_level: TestLevel::Guest,
            kind: CommandKind::Sync,
            min_args: 0,
            max_args: 0,
        };
        const CMD2: CommandMeta<TestLevel> = CommandMeta {
            id: "a2",
            name: "a2",
            description: "Command 2",
            access_level: TestLevel::Guest,
            kind: CommandKind::Sync,
            min_args: 0,
            max_args: 0,
        };
        const CMD3: CommandMeta<TestLevel> = CommandMeta {
            id: "a3",
            name: "a3",
            description: "Command 3",
            access_level: TestLevel::Guest,
            kind: CommandKind::Sync,
            min_args: 0,
            max_args: 0,
        };
        const CMD4: CommandMeta<TestLevel> = CommandMeta {
            id: "a4",
            name: "a4",
            description: "Command 4",
            access_level: TestLevel::Guest,
            kind: CommandKind::Sync,
            min_args: 0,
            max_args: 0,
        };

        const OVERFLOW_DIR: Directory<TestLevel> = Directory {
            name: "overflow",
            children: &[
                Node::Command(&CMD1),
                Node::Command(&CMD2),
                Node::Command(&CMD3),
                Node::Command(&CMD4),
            ],
            access_level: TestLevel::Guest,
        };

        // Use MAX_MATCHES = 2, but we have 4 matching items
        let result = suggest_completions::<TestLevel, 2>(&OVERFLOW_DIR, "a", None);

        // Should return BufferFull error
        assert!(matches!(result, Err(CliError::BufferFull)));
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_very_long_command_name() {
        // Create a command with name > 128 characters
        const LONG_CMD: CommandMeta<TestLevel> = CommandMeta {
            id: "long",
            name: "this_is_a_very_long_command_name_that_exceeds_the_maximum_buffer_size_of_128_characters_and_should_cause_a_buffer_overflow_error_when_completing",
            description: "Long command",
            access_level: TestLevel::Guest,
            kind: CommandKind::Sync,
            min_args: 0,
            max_args: 0,
        };

        const LONG_DIR: Directory<TestLevel> = Directory {
            name: "long",
            children: &[Node::Command(&LONG_CMD)],
            access_level: TestLevel::Guest,
        };

        // Try to complete - should return BufferFull error
        let result = suggest_completions::<TestLevel, 16>(&LONG_DIR, "this", None);

        assert!(matches!(result, Err(CliError::BufferFull)));
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_long_directory_name_with_slash() {
        // Create a directory with name = 128 characters (so adding "/" would overflow)
        const LONG_DIR_CHILD: Directory<TestLevel> = Directory {
            name: "this_is_exactly_one_hundred_twenty_eight_characters_long_directory_name_abcdefghijklmnopqrstuvwxyz_0123456789_more_padding_needed",
            children: &[],
            access_level: TestLevel::Guest,
        };

        const LONG_DIR: Directory<TestLevel> = Directory {
            name: "parent",
            children: &[Node::Directory(&LONG_DIR_CHILD)],
            access_level: TestLevel::Guest,
        };

        // Try to complete - should return BufferFull error when trying to append "/"
        let result = suggest_completions::<TestLevel, 16>(&LONG_DIR, "this", None);

        assert!(matches!(result, Err(CliError::BufferFull)));
    }

    #[test]
    #[cfg(feature = "completion")]
    fn test_match_name_exceeds_64_chars() {
        // Create commands with names > 64 characters (for all_matches buffer)
        const LONG1: CommandMeta<TestLevel> = CommandMeta {
            id: "m1",
            name: "match_name_that_is_longer_than_sixty_four_characters_abcdefghijklm",
            description: "Long 1",
            access_level: TestLevel::Guest,
            kind: CommandKind::Sync,
            min_args: 0,
            max_args: 0,
        };
        const LONG2: CommandMeta<TestLevel> = CommandMeta {
            id: "m2",
            name: "match_name_that_is_longer_than_sixty_four_characters_nopqrstuvwxyz",
            description: "Long 2",
            access_level: TestLevel::Guest,
            kind: CommandKind::Sync,
            min_args: 0,
            max_args: 0,
        };

        const LONG_MATCH_DIR: Directory<TestLevel> = Directory {
            name: "longmatch",
            children: &[Node::Command(&LONG1), Node::Command(&LONG2)],
            access_level: TestLevel::Guest,
        };

        // Multiple matches with long names should cause BufferFull when building all_matches
        let result = suggest_completions::<TestLevel, 16>(&LONG_MATCH_DIR, "match", None);

        assert!(matches!(result, Err(CliError::BufferFull)));
    }
}