Skip to main content

walrus_cli/cmd/
memory.rs

1//! Memory management commands: list, get.
2
3use crate::runner::gateway::GatewayRunner;
4use anyhow::Result;
5use clap::Subcommand;
6
7/// Memory management subcommands.
8#[derive(Subcommand, Debug)]
9pub enum MemoryCommand {
10    /// List all memory entries.
11    List,
12    /// Get a specific memory entry.
13    Get {
14        /// Memory key.
15        key: String,
16    },
17}
18
19impl MemoryCommand {
20    /// Dispatch memory management subcommands.
21    pub async fn run(&self, runner: &mut GatewayRunner) -> Result<()> {
22        match self {
23            Self::List => list(runner).await,
24            Self::Get { key } => get(runner, key).await,
25        }
26    }
27}
28
29async fn list(runner: &mut GatewayRunner) -> Result<()> {
30    let entries = runner.list_memory().await?;
31    if entries.is_empty() {
32        println!("No memory entries.");
33        return Ok(());
34    }
35    for (key, value) in &entries {
36        let preview = if value.len() > 80 {
37            let end = value
38                .char_indices()
39                .nth(77)
40                .map(|(i, _)| i)
41                .unwrap_or(value.len());
42            format!("{}...", &value[..end])
43        } else {
44            value.clone()
45        };
46        println!("  {key}: {preview}");
47    }
48    Ok(())
49}
50
51async fn get(runner: &mut GatewayRunner, key: &str) -> Result<()> {
52    match runner.get_memory(key).await? {
53        Some(value) => println!("{value}"),
54        None => println!("No entry for key '{key}'."),
55    }
56    Ok(())
57}