rustyrepl/lib.rs
1// Copyright (c) Sean Lawlor
2//
3// This source code is licensed under the MIT license found in the
4// LICENSE file in the root directory of this source tree.
5
6//! rustyrepl is a simple read, evaluate, print, loop processor based on [clap] and utilizing
7//! [rustyline].
8//!
9//! You simply need to define your command structure in a [clap::Parser] derived
10//! struct and the processing logic, and the Repl will handle
11//!
12//! # Purpose
13//!
14//! 1. Capturing exits/quits of the REPL interface
15//! 2. Storing and managing REPL history commands as well as an index of said commands for you
16//! 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)
17//! 4. Processing the commands as incoming
18//!
19//! # Usage
20//!
21//! First, add rustyrepl to your Cargo.toml file
22//!
23//! ```toml
24//! [dependencies]
25//! rustyrepl = "0.2"
26//! ```
27//!
28//! Next:
29//!
30//! ```rust
31//! use anyhow::Result;
32//! use clap::{Parser, Subcommand};
33//! use rustyrepl::{Repl, ReplCommandProcessor};
34//!
35//! /// The enum of sub-commands supported by the CLI
36//! #[derive(Subcommand, Clone, Debug)]
37//! pub enum Command {
38//! /// Execute a test command
39//! Test,
40//! }
41//!
42//! /// The general CLI, essentially a wrapper for the sub-commands [Command]
43//! #[derive(Parser, Clone, Debug)]
44//! pub struct Cli {
45//! #[clap(subcommand)]
46//! command: Command,
47//! }
48//!
49//! #[derive(Debug)]
50//! pub struct CliProcessor {}
51//!
52//! #[async_trait::async_trait]
53//! impl ReplCommandProcessor<Cli> for CliProcessor {
54//! fn is_quit(&self, command: &str) -> bool {
55//! matches!(command, "quit" | "exit")
56//! }
57//!
58//! async fn process_command(&self, command: Cli) -> Result<()> {
59//! match command.command {
60//! Command::Test => println!("A wild test appeared!"),
61//! }
62//! Ok(())
63//! }
64//! }
65//!
66//! // MAIN //
67//! #[tokio::main]
68//! async fn main() -> Result<()> {
69//! let processor: Box<dyn ReplCommandProcessor<Cli>> = Box::new(CliProcessor {});
70//!
71//! let mut repl = Repl::<Cli>::new(processor, None, Some(">>".to_string()))?;
72//! repl.process().await
73//! }
74//! ```
75//!
76//! This small program will startup up a REPL with the prompt ">>" which you can interact with
77//!
78//! ```text
79//! >> help
80//! The general CLI, essentially a wrapper for the sub-commands [Commands]
81//!
82//! Usage: repl-interface <COMMAND>
83//!
84//! Commands:
85//! test Execute a test command
86//! help Print this message or the help of the given subcommand(s)
87//!
88//! Options:
89//! -h, --help Print help
90//!
91//! ```
92
93mod commands;
94mod repl;
95
96#[cfg(test)]
97pub(crate) mod common_test;
98
99pub use crate::commands::ReplCommandProcessor;
100pub use crate::repl::Repl;