1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
use std::{fmt, io};
/// Interface for creating new commands
///
/// Defines io::Stdout as the default generic type.
/// Returning CommandResult::Break instructs the Cmd.run() loop to break.
///
/// # Examples
///
/// ```rust
/// // CommandHandler that prints out help message
/// use std::io;
/// use std::io::Write;
/// use rusty_cmd::command_handler::{CommandHandler, CommandResult};
///
/// #[derive(Debug, Default)]
/// pub struct Help;
///
/// impl CommandHandler for Help {
/// fn execute(&self, _stdout: &mut io::Stdout, _args: &[&str]) -> CommandResult {
/// writeln!(_stdout, "Help message").unwrap();
/// CommandResult::Continue
/// }
/// }
///
/// /// CommandHandler that prints out a greeting
/// #[derive(Debug, Default)]
/// pub struct Greet;
///
/// impl<W: io::Write> CommandHandler<W> for Greet {
/// fn execute(&self, _stdout: &mut W, _args: &[&str]) -> CommandResult {
/// let joined_args = _args.join(", ");
/// match _args.len() {
/// 0 => _stdout.write(format!("Hello, {}!", joined_args).as_bytes()).unwrap(),
/// _ => _stdout.write(b"Hello!").unwrap(),
/// };
/// CommandResult::Continue
/// }
/// }
/// ```
pub trait CommandHandler<W = io::Stdout>: fmt::Debug {
/// Required method to execute a command
fn execute(&self, _stdout: &mut W, _args: &[&str]) -> CommandResult;
}
pub enum CommandResult {
Continue,
Break,
}