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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * lib.rs is part of slaps.
 * Copyright (C) 2020 rd <slaps@megane.space>
 * License: WTFPL
 */

//! Add an interactive shell mode to your Rust command-line application using your
//! existing [`clap`]/[`structopt`] configuration.
//!
//! [`Clap`]: https://crates.io/crates/clap
//! [`structopt`]: https://crates.io/crates/structopt

// Deny unsafe code and enforce strict linting.
#![deny(unsafe_code)]
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(missing_docs)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]

mod clap;
mod config;
mod error;
#[doc(hidden)]
pub mod shlex;

pub use config::{ColorMode, CompletionType, Config};
pub use error::{ClapError, Error, ErrorKind, ExecutionError, MismatchedQuotes, ReadlineError};
use rustyline::Editor;
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};

/// Handler function for a command.
type Handler<'a> = Box<dyn 'a + Fn(&clap::ArgMatches) -> Result<(), ExecutionError> + Send + Sync>;

/// Interactive shell mode using a `clap`/`structopt` configuration.
pub struct Slaps<'a, 'b> {
    config: Config<'b>,
    editor: Editor<clap::Matcher<'a, 'b>>,
    handlers: HashMap<u64, Handler<'b>>,
}

impl<'a, 'b> Slaps<'a, 'b> {
    /// Creates a new interactive app using the default configuration.
    ///
    /// # Examples
    /// ```
    /// use slaps::Slaps;
    ///
    /// let slaps = Slaps::with_name("slaps");
    /// ```
    #[must_use]
    pub fn with_name(name: &str) -> Self {
        Self::with_name_and_config(name, Config::default())
    }

    /// Creates a new interactive app with the given `Config`.
    ///
    /// # Examples
    /// ```
    /// use slaps::{Config, ColorMode, Slaps};
    ///
    /// let slaps = Slaps::with_name_and_config("slaps", Config::default()
    ///     .color_mode(ColorMode::Disabled));
    /// ```
    #[must_use]
    pub fn with_name_and_config(name: &str, config: Config<'b>) -> Slaps<'a, 'b> {
        use ::clap::App;

        let mut editor = Editor::with_config(config.into());
        editor.set_helper(Some(clap::Matcher::with_name_and_config(name, config)));

        Slaps {
            config,
            editor,
            handlers: HashMap::new(),
        }
        .command(
            App::new("quit").alias("exit").about("Exits the program"),
            |_| Err(ExecutionError::GracefulExit),
        )
    }

    /// Registers a Clap subcommand configuration with Slaps.
    ///
    /// # Examples
    /// ```
    /// use clap::App;
    /// use slaps::Slaps;
    ///
    /// let slaps = Slaps::with_name("slaps")
    ///     .command(App::new("slap"), |matches| {println!("{:?}", matches); Ok(())} );
    /// ```
    #[must_use]
    pub fn command<T: 'b>(mut self, command: clap::App<'a, 'b>, handler: T) -> Self
    where
        T: Fn(&clap::ArgMatches) -> Result<(), ExecutionError> + Send + Sync,
    {
        self.handlers
            .insert(calculate_hash(&command.p.meta.name), Box::new(handler));
        self.editor.helper_mut().unwrap().register_command(command);
        self
    }

    /// Prompts the user for a `String` using the default prompt.
    ///
    /// If prompt is None, uses the default from the [`Config`].
    ///
    /// # Errors
    /// May return a [`ReadlineError`] from the internal readline implementation.
    pub fn prompt_line(&mut self, prompt: Option<&str>) -> Result<String, ReadlineError> {
        Ok(self.editor.readline(prompt.unwrap_or(self.config.prompt))?)
    }

    /// Prompts the user for a password.
    ///
    /// If prompt is None, uses the default from the [`Config`].
    ///
    /// # Errors
    /// May return a [`ReadlineError`] from the internal readline implementation.
    pub fn prompt_password(&self, prompt: Option<&str>) -> Result<String, ReadlineError> {
        Ok(rpassword::prompt_password_stderr(
            prompt.unwrap_or(self.config.password_prompt),
        )?)
    }

    /// Parses a string directly into a [`clap::ArgMatches`] object.
    ///
    /// # Errors
    /// - `MismatchedQuote` parsing failed due to mismatched quotation marks within the input.
    ///
    /// # Examples
    /// ```
    /// use clap::{App, Arg};
    /// use slaps::Slaps;
    ///
    /// let command = App::new("slaps").arg(Arg::with_name("that").long("that").takes_value(true));
    /// let mut slaps = Slaps::with_name("slaps").command(command, |args| Ok(()));
    /// let matches = slaps.get_matches("slaps --that arg").unwrap();
    ///
    /// assert_eq!(matches.subcommand_name(), Some("slaps"));
    /// assert_eq!(matches.subcommand_matches("slaps").unwrap().value_of("that"), Some("arg"));
    /// ```
    pub fn get_matches<T: AsRef<str>>(&mut self, input: T) -> Result<clap::ArgMatches, Error> {
        let words = shlex::split(input.as_ref())?;
        Ok(self.editor.helper_mut().unwrap().get_matches(&words)?)
    }

    /// Starts the main interactive loop, prompting the user for input repeatedly.
    ///
    /// # Errors
    /// Will return an error when it fails to read more input.
    pub fn run(mut self) -> Result<(), Error> {
        use rustyline::error::ReadlineError as RustylineError;
        loop {
            let input = match self.editor.readline(self.config.prompt) {
                Ok(i) => i,
                Err(e) => {
                    return match e {
                        RustylineError::Eof | RustylineError::Interrupted => {
                            Err(ExecutionError::GracefulExit.into())
                        }
                        _ => Err(e.into()),
                    }
                }
            };

            let fields = match shlex::split(&input) {
                Ok(f) => f,
                Err(e) => {
                    eprintln!("{}", e);
                    continue;
                }
            };

            match self.handle_matches(&fields) {
                Err(Error::ClapError(e)) => match e.kind {
                    clap::ErrorKind::Io | ErrorKind::Format => return Err(Error::ClapError(e)),
                    _ => eprintln!("{}", e),
                },
                Ok(r) => eprintln!("t {:?}", r),
                Err(e) => eprintln!("{}", e),
            };
        }
    }

    /// Matches a command by parsing command-line arguments from the given input, and executes its
    /// associated handler.
    ///
    /// Useful for handling commands from `std::env::args` before switching to interactive mode.
    ///
    /// # Errors
    /// - [`ClapError`] - the input did not specify a command or failed to parse arguments.
    /// - [`ExecutionError`] - the handler returned an error.
    ///
    /// # Examples
    /// ```
    /// use slaps::{Config, ExecutionError, Slaps, Error};
    /// use clap::{App, Arg, ErrorKind};
    ///
    /// let command = App::new("slaps").arg(
    /// Arg::with_name("this")
    ///     .long("this")
    ///     .takes_value(true)
    ///     .required(true),
    /// );
    ///
    /// let mut slaps = Slaps::with_name("slaps").command(command, |args| {
    /// # assert_eq!(args.value_of("this"), Some("arg"));
    ///     Err(ExecutionError::GracefulExit)
    /// });
    /// let args = ["prog", "slaps", "--this", "arg"];
    /// let result = slaps.handle_matches(&args[1..]);
    ///
    /// match result {
    ///     Err(Error::ClapError(ref e)) => match e.kind {
    ///         ErrorKind::Io | ErrorKind::Format => eprintln!("I/O error: {}", e),
    ///         _ => eprintln!("Error while parsing arguments: {}", e),
    ///     },
    ///     Ok(_) => eprintln!("Command succeeded."),
    ///     Err(ref e) => eprintln!("Command handler failed to execute: {}", e)
    /// }
    ///
    /// # assert!(result.is_err());
    /// # assert!(matches!(
    /// #        result,
    /// #        Err(Error::ExecutionError(ExecutionError::GracefulExit))
    /// #    ));
    /// ```
    pub fn handle_matches<A: AsRef<OsStr>>(&mut self, input: &[A]) -> Result<(), Error> {
        let matches = self.editor.helper_mut().unwrap().get_matches(input)?;
        if let Some(subcommand) = matches.subcommand_name() {
            if let (Some(handler), Some(subcommand_matches)) = (
                self.handlers.get_mut(&calculate_hash(&subcommand)),
                matches.subcommand_matches(subcommand),
            ) {
                handler(subcommand_matches)?;
            }
        }
        Ok(())
    }
}

impl Debug for Slaps<'_, '_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Slaps {{config = {:?}, handlers = {:?}}}",
            self.config,
            self.handlers.keys()
        )
    }
}

// Hack to avoid copying Strings into the handlers hashmap.
fn calculate_hash<T: Hash>(t: &T) -> u64 {
    let mut s = DefaultHasher::new();
    t.hash(&mut s);
    s.finish()
}

#[cfg(test)]
mod tests {
    use crate::config::ColorMode::Disabled;
    use crate::config::CompletionType::List;
    use crate::shlex::split;
    use crate::{Config, Slaps};

    #[test]
    fn test_sets_rustyline_config() {
        use rustyline::config::Configurer;
        use rustyline::config::OutputStreamType;
        use rustyline::ColorMode;
        use rustyline::CompletionType;

        let config = Config::default().color_mode(Disabled).completion_type(List);

        let mut slaps = Slaps::with_name_and_config("slaps", config);
        let readline_config = slaps.editor.config_mut();
        assert_eq!(readline_config.color_mode(), ColorMode::Disabled);
        assert_eq!(readline_config.completion_type(), CompletionType::List);
        assert_eq!(readline_config.auto_add_history(), config.history_auto_add);
        assert_eq!(readline_config.tab_stop(), 4);
        assert_eq!(readline_config.output_stream(), OutputStreamType::Stderr);
    }

    #[test]
    fn test_propagates_clap_errors() {
        let app = ::clap::App::new("slaps").arg(
            ::clap::Arg::with_name("this")
                .long("this")
                .takes_value(true)
                .required(true),
        );
        let config = Config::default().highlighter_underline_word_under_cursor(true);
        let mut slaps =
            Slaps::with_name_and_config("slaps", config).command(app, |_| unreachable!());
        let result = slaps.handle_matches(&split("slaps --that arg").unwrap());

        assert!(result.is_err());
        assert!(matches!(result, Err(crate::Error::ClapError(_))));
    }
}