From 9ce500bb02546f1c7ccad3d3901a3ef26f35b16c Mon Sep 17 00:00:00 2001 From: Sean Lawlor Date: Tue, 30 Aug 2022 05:06:17 -1000 Subject: [PATCH] Updating documentation for public release (#4) * initial commit of REPL interface * Adding test utility * Initial clippy + rustfmt to cleanup build pipeline * PR CI additions * cargo test abiguity * Added async trait to Repl, for non-async usage * Crate rename to rustyrepl * Generate the process() logic via macro for better reusability and less code duplication * Adding publish pipeline * missing default flag * adding async tests * fix async test pipeline * ci syntax * docs update Co-authored-by: Sean Lawlor --- README.md | 56 ++++++++++++++++++++++++++++++++++++++++++-- rustyrepl/Cargo.toml | 2 +- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f0b370d..62e5c09 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,58 @@ -# repl +# rustyrepl The easy Rust Read-Evaluate-Print-Loop (REPL) utility crate ## About -`repl` is a simple crate to facilitate creation of Read, Evaluate, Print, Loop utilities at the command-line. +`rustyrepl` is a simple crate to facilitate creation of Read, Evaluate, Print, Loop utilities at the command-line. + +## Purpose +1. Capturing exits/quits of the REPL interface +2. Storing and managing REPL history commands as well as an index of said commands for you +3. Allowing operators to get a help menu at any point, using the full Clap supported help interface (i.e. sub-command help as well) +4. Processing the commands as incoming +# Usage +First, add rustyrepl to your Cargo.toml file +```toml +[dependencies] +rustyrepl = "0.1" +``` + +Next: +```rust +use anyhow::Result; +use clap::{Parser, Subcommand}; +use rustyrepl::{Repl, ReplCommandProcessor}; +/// The enum of sub-commands supported by the CLI +#[derive(Subcommand, Clone, Debug)] +pub enum Command { + /// Execute a test command + Test, +} +/// The general CLI, essentially a wrapper for the sub-commands [Commands] +#[derive(Parser, Clone, Debug)] +pub struct Cli { + #[clap(subcommand)] + command: Command, +} +#[derive(Debug)] +pub struct CliProcessor {} +#[async_trait::async_trait] +impl ReplCommandProcessor for CliProcessor { + fn is_quit(&self, command: &str) -> bool { + matches!(command, "quit" | "exit") + } + async fn process_command(&self, command: Cli) -> Result<()> { + match command.command { + Command::Test => println!("A wild test appeared!"), + } + Ok(()) + } +} +// MAIN // +#[tokio::main] +async fn main() -> Result<()> { + let processor: Box> = Box::new(CliProcessor {}); + let mut repl = Repl::::new(processor, None, Some(">>".to_string()))?; + repl.process().await +} +``` diff --git a/rustyrepl/Cargo.toml b/rustyrepl/Cargo.toml index 5f04829..9ad33c4 100644 --- a/rustyrepl/Cargo.toml +++ b/rustyrepl/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustyrepl" -version = "0.1.0" +version = "0.1.1" authors = ["Sean Lawlor "] description = "A Rust read, evaluate, print, loop (REPL) utility " license = "MIT"