From 0f12dabfc19fb8ecd23637b57a255c657ec000c9 Mon Sep 17 00:00:00 2001 From: fluo10 Date: Fri, 6 Jun 2025 07:49:36 +0900 Subject: [PATCH] Add async to console commands --- lazy-supplements/src/cli/console.rs | 24 ++++++++++++++++-------- lazy-supplements/src/cli/node.rs | 6 +++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lazy-supplements/src/cli/console.rs b/lazy-supplements/src/cli/console.rs index c826729..9ba268e 100644 --- a/lazy-supplements/src/cli/console.rs +++ b/lazy-supplements/src/cli/console.rs @@ -1,7 +1,7 @@ use std::{collections::HashMap, ffi::OsString, hash::Hash, time::Duration}; use clap::{Args, Parser}; -use futures::StreamExt; +use futures::{future::BoxFuture, StreamExt}; use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm}; use tokio::time::sleep; use tracing_subscriber::EnvFilter; @@ -18,22 +18,28 @@ pub trait ConsoleCommand { fn execute_line(&self, line: String) -> Result<(), Error>; } pub struct ConsoleCommands { - content: HashMap<&'static str, Box) -> Result<(), Error>>>, + content: HashMap<&'static str, Box) -> BoxFuture<'static, Result<(), Error>>>>, } impl ConsoleCommands { pub fn new() -> Self { Self{content: HashMap::new()} } - pub fn insert(&mut self,name: &'static str, f: Box) -> Result<(), Error>>) { - if let Some(_) = self.content.insert(name, f){ + pub fn insert(&mut self,name: &'static str, f: F) + where + F: Fn(Vec) -> Fut + 'static, + Fut: Future> + Send + 'static, + { + if let Some(_) = self.content.insert(name, Box::new(move |v| { + Box::pin(f(v)) + })){ unreachable!(); }; } - pub fn parse_line(&self, line: String) -> Result<(), Error>{ + pub async fn parse_and_run(&self, line: String) -> Result<(), Error>{ let args = shell_words::split(&line)?; if let Some(command_name) = args.first().map(|s| {s.clone()}) { if let Some(command) = self.content.get(command_name.as_str()) { - command(args) + command(args).await } else { println!("Invalid command: {command_name}"); self.print_commands(); @@ -53,7 +59,7 @@ impl ConsoleCommands { impl Default for ConsoleCommands { fn default() -> Self { let mut commands = Self::new(); - commands.insert("node", Box::new(parse_and_run_console_node_command)); + commands.insert("node", parse_and_run_console_node_command); commands } } @@ -74,7 +80,9 @@ impl ConsoleArgs { let mut rl = rustyline::DefaultEditor::new()?; loop { match rl.readline(">> ") { - Ok(line) => commands.parse_line(line)?, + Ok(line) => { + commands.parse_and_run(line).await? + }, Err(x) => Err(x)?, }; } diff --git a/lazy-supplements/src/cli/node.rs b/lazy-supplements/src/cli/node.rs index a985d9e..f2a4f28 100644 --- a/lazy-supplements/src/cli/node.rs +++ b/lazy-supplements/src/cli/node.rs @@ -24,15 +24,15 @@ pub struct ConsoleNodeArgs { } impl ConsoleNodeArgs { - pub fn run(self) -> Result<(), Error> { + pub async fn run(self) -> Result<(), Error> { println!("{self:?}"); Ok(()) } } -pub fn parse_and_run_console_node_command(s:Vec) -> Result<(), Error> { +pub async fn parse_and_run_console_node_command(s:Vec) -> Result<(), Error> { let args = ConsoleNodeArgs::parse_from(s); - args.run() + args.run().await } #[derive(Args, Debug)]