nut_shell/
config.rs

1//! Configuration traits and implementations for buffer sizing.
2//!
3//! The `ShellConfig` trait allows compile-time configuration of buffer sizes
4//! and capacity limits without runtime overhead.
5
6/// Shell configuration trait defining buffer sizes and capacity limits.
7///
8/// All values are const (zero runtime cost). Due to Rust's const generics limitations
9/// (const trait bounds not yet stable), internal buffers use hardcoded sizes from
10/// `DefaultConfig` rather than `C::MAX_INPUT`, etc. The trait establishes the API
11/// contract for when const generics stabilize.
12///
13/// **Currently customizable:** `MSG_*` strings only.
14/// **Not yet customizable:** Buffer size constants (hardcoded to `DefaultConfig` values).
15pub trait ShellConfig {
16    /// Maximum input buffer size (default: 128)
17    const MAX_INPUT: usize;
18
19    /// Maximum path depth (default: 8)
20    const MAX_PATH_DEPTH: usize;
21
22    /// Maximum number of command arguments (default: 16)
23    const MAX_ARGS: usize;
24
25    /// Maximum prompt length (default: 64)
26    const MAX_PROMPT: usize;
27
28    /// Maximum response message length (default: 256)
29    const MAX_RESPONSE: usize;
30
31    /// Command history size (default: 10)
32    const HISTORY_SIZE: usize;
33
34    // Message constants for user-visible strings
35    // All stored in ROM, zero runtime cost
36
37    /// Welcome message shown on activation
38    const MSG_WELCOME: &'static str;
39
40    /// Login prompt
41    const MSG_LOGIN_PROMPT: &'static str;
42
43    /// Login success message
44    const MSG_LOGIN_SUCCESS: &'static str;
45
46    /// Login failed message
47    const MSG_LOGIN_FAILED: &'static str;
48
49    /// Logout message
50    const MSG_LOGOUT: &'static str;
51
52    /// Invalid login format message
53    const MSG_INVALID_LOGIN_FORMAT: &'static str;
54}
55
56/// Default configuration for typical embedded systems.
57#[derive(Debug, Copy, Clone, PartialEq, Eq)]
58pub struct DefaultConfig;
59
60impl ShellConfig for DefaultConfig {
61    const MAX_INPUT: usize = 128;
62    const MAX_PATH_DEPTH: usize = 8;
63    const MAX_ARGS: usize = 16;
64    const MAX_PROMPT: usize = 64;
65    const MAX_RESPONSE: usize = 256;
66
67    #[cfg(feature = "history")]
68    const HISTORY_SIZE: usize = 10;
69
70    #[cfg(not(feature = "history"))]
71    const HISTORY_SIZE: usize = 0;
72
73    #[cfg(feature = "authentication")]
74    const MSG_WELCOME: &'static str = "Welcome to nut-shell! Please login.";
75    #[cfg(not(feature = "authentication"))]
76    const MSG_WELCOME: &'static str = "Welcome to nut-shell! Type '?' for help.";
77
78    const MSG_LOGIN_PROMPT: &'static str = "Login> ";
79    const MSG_INVALID_LOGIN_FORMAT: &'static str = "Invalid login format. Use: <name>:<password>";
80    const MSG_LOGIN_FAILED: &'static str = "Login failed. Try again.";
81    const MSG_LOGIN_SUCCESS: &'static str = "Logged in. Type '?' for help.";
82    const MSG_LOGOUT: &'static str = "Logged out.";
83}
84
85/// Minimal configuration for resource-constrained systems.
86#[derive(Debug, Copy, Clone, PartialEq, Eq)]
87pub struct MinimalConfig;
88
89impl ShellConfig for MinimalConfig {
90    const MAX_INPUT: usize = 64;
91    const MAX_PATH_DEPTH: usize = 4;
92    const MAX_ARGS: usize = 8;
93    const MAX_PROMPT: usize = 32;
94    const MAX_RESPONSE: usize = 128;
95
96    #[cfg(feature = "history")]
97    const HISTORY_SIZE: usize = 4;
98
99    #[cfg(not(feature = "history"))]
100    const HISTORY_SIZE: usize = 0;
101
102    const MSG_WELCOME: &'static str = "Welcome";
103
104    const MSG_LOGIN_PROMPT: &'static str = "Login> ";
105    const MSG_INVALID_LOGIN_FORMAT: &'static str = "Invalid format. Use name:pass";
106    const MSG_LOGIN_FAILED: &'static str = "Login failed";
107    const MSG_LOGIN_SUCCESS: &'static str = "Logged in";
108    const MSG_LOGOUT: &'static str = "Logged out";
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    #[test]
116    fn test_messages_are_const() {
117        // Verify that messages are compile-time constants (can be used in const context)
118        const _WELCOME: &str = DefaultConfig::MSG_WELCOME;
119        const _LOGIN: &str = DefaultConfig::MSG_LOGIN_PROMPT;
120        const _SUCCESS: &str = DefaultConfig::MSG_LOGIN_SUCCESS;
121        const _FAILED: &str = DefaultConfig::MSG_LOGIN_FAILED;
122        const _LOGOUT: &str = DefaultConfig::MSG_LOGOUT;
123        const _FORMAT: &str = DefaultConfig::MSG_INVALID_LOGIN_FORMAT;
124    }
125}