repl-rs 0.2.11

Library to generate a REPL for your application
Documentation
extern crate repl_rs;

use repl_rs::{Command, Parameter, Result, Value};
use repl_rs::{Convert, Repl};
use std::collections::HashMap;
use tokio::runtime::Runtime;

// Async function being called. Silly example, but you get the point
async fn add_async(first: i32, second: i32) -> Result<i32> {
    Ok(first + second)
}

// Add two numbers. Calls the async fn using Runtime::block_on()
fn add<T>(args: HashMap<String, Value>, _context: &mut T) -> Result<Option<String>> {
    let first: i32 = args["first"].convert()?;
    let second: i32 = args["second"].convert()?;

    // If you're calling multiple async functions, you should
    // probably share the runtime globally
    let runtime = Runtime::new().unwrap();

    // Call block_on() on the runtime to run the async code,
    // wrapping it in an async block
    runtime.block_on(async move { add_async(first, second).await.map(|i| Some(i.to_string())) })
}

fn main() -> Result<()> {
    let mut repl = Repl::new(())
        .with_name("MyApp")
        .with_version("v0.1.0")
        .with_description("My very cool app")
        .add_command(
            Command::new("add", add)
                .with_parameter(Parameter::new("first").set_required(true)?)?
                .with_parameter(Parameter::new("second").set_required(true)?)?
                .with_help("Add two numbers together"),
        );
    repl.run()
}