in_memory_cache/
cli.rs

1//! Command-line interface definitions.
2//!
3//! This module defines the CLI structure for the cache client using clap.
4
5use clap::{Parser, Subcommand};
6
7/// In-memory cache client.
8///
9/// A CLI tool for interacting with the in-memory cache server.
10#[derive(Parser, Debug)]
11#[command(name = "cache-client")]
12#[command(author, version, about, long_about = None)]
13pub struct Cli {
14    /// The command to execute.
15    #[clap(subcommand)]
16    pub command: ClientCommand,
17}
18
19/// Available client commands.
20#[derive(Subcommand, Debug)]
21pub enum ClientCommand {
22    /// Get a value by key.
23    ///
24    /// Retrieves the value stored at the given key.
25    /// Returns nothing if the key doesn't exist.
26    Get {
27        /// The key to look up.
28        key: String,
29    },
30
31    /// Set a key-value pair.
32    ///
33    /// Stores the value at the given key. If the key already
34    /// exists, its value is updated.
35    Set {
36        /// The key to store the value under.
37        key: String,
38        /// The value to store.
39        value: String,
40    },
41
42    /// Delete a key.
43    ///
44    /// Removes the key and its value from the cache.
45    Delete {
46        /// The key to delete.
47        key: String,
48    },
49
50    /// Ping the server.
51    ///
52    /// Checks if the server is running and responsive.
53    Ping,
54
55    /// Get server statistics.
56    ///
57    /// Shows cache hits, misses, size, and hit rate.
58    Stats,
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_parse_get() {
67        let cli = Cli::parse_from(["test", "get", "mykey"]);
68        match cli.command {
69            ClientCommand::Get { key } => assert_eq!(key, "mykey"),
70            _ => panic!("Expected Get command"),
71        }
72    }
73
74    #[test]
75    fn test_parse_set() {
76        let cli = Cli::parse_from(["test", "set", "mykey", "myvalue"]);
77        match cli.command {
78            ClientCommand::Set { key, value } => {
79                assert_eq!(key, "mykey");
80                assert_eq!(value, "myvalue");
81            }
82            _ => panic!("Expected Set command"),
83        }
84    }
85
86    #[test]
87    fn test_parse_delete() {
88        let cli = Cli::parse_from(["test", "delete", "mykey"]);
89        match cli.command {
90            ClientCommand::Delete { key } => assert_eq!(key, "mykey"),
91            _ => panic!("Expected Delete command"),
92        }
93    }
94
95    #[test]
96    fn test_parse_ping() {
97        let cli = Cli::parse_from(["test", "ping"]);
98        assert!(matches!(cli.command, ClientCommand::Ping));
99    }
100
101    #[test]
102    fn test_parse_stats() {
103        let cli = Cli::parse_from(["test", "stats"]);
104        assert!(matches!(cli.command, ClientCommand::Stats));
105    }
106}