Add async to console commands
This commit is contained in:
parent
3e939e8214
commit
0f12dabfc1
2 changed files with 19 additions and 11 deletions
|
@ -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<dyn Fn(Vec<String>) -> Result<(), Error>>>,
|
||||
content: HashMap<&'static str, Box<dyn Fn(Vec<String>) -> BoxFuture<'static, Result<(), Error>>>>,
|
||||
}
|
||||
impl ConsoleCommands {
|
||||
pub fn new() -> Self {
|
||||
Self{content: HashMap::new()}
|
||||
}
|
||||
pub fn insert(&mut self,name: &'static str, f: Box<dyn Fn(Vec<String>) -> Result<(), Error>>) {
|
||||
if let Some(_) = self.content.insert(name, f){
|
||||
pub fn insert<F, Fut>(&mut self,name: &'static str, f: F)
|
||||
where
|
||||
F: Fn(Vec<String>) -> Fut + 'static,
|
||||
Fut: Future<Output = Result<(), Error>> + 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)?,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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<String>) -> Result<(), Error> {
|
||||
pub async fn parse_and_run_console_node_command(s:Vec<String>) -> Result<(), Error> {
|
||||
let args = ConsoleNodeArgs::parse_from(s);
|
||||
args.run()
|
||||
args.run().await
|
||||
}
|
||||
|
||||
#[derive(Args, Debug)]
|
||||
|
|
Loading…
Add table
Reference in a new issue