From b36cf92b03fa5b844e042cdc747079846ac11103 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 26 Jun 2019 00:05:27 -0400 Subject: clippy --- Cargo.toml | 6 ++++++ README.md | 1 + src/builtins.rs | 4 ++-- src/eval.rs | 10 +++++----- src/main.rs | 10 ++++++++++ src/parser.rs | 4 +++- src/process.rs | 2 +- src/readline.rs | 22 ++++++++++++---------- src/repl.rs | 38 +++++++++++++++++--------------------- 9 files changed, 57 insertions(+), 40 deletions(-) create mode 100644 README.md diff --git a/Cargo.toml b/Cargo.toml index 7b14779..6792a5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,12 @@ name = "nbsh" version = "0.1.0" authors = ["Jesse Luehrs "] edition = "2018" +description = "experimental interactive shell" +license = "MIT" +repository = "https://git.tozt.net/nbsh" +readme = "README.md" +keywords = ["shell", "terminal"] +categories = ["command-line-utilities"] [dependencies] crossterm = "0.9" diff --git a/README.md b/README.md new file mode 100644 index 0000000..6a12f0b --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +nbsh is an experimental interactive shell. diff --git a/src/builtins.rs b/src/builtins.rs index 2c239ec..563cfe4 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -47,7 +47,7 @@ pub struct Builtin { impl Builtin { fn new(cmd: &str, args: &[String]) -> Result { match cmd { - "cd" => Ok(Builtin { + "cd" => Ok(Self { cmd: cmd.to_string(), args: args.to_vec(), done: false, @@ -90,7 +90,7 @@ fn cd(args: &[String]) -> Result<()> { TooManyParams { cmd: "cd", args, - expected: 1u32, + expected: 1_u32, } ); let dir = if let Some(dir) = args.get(0) { diff --git a/src/eval.rs b/src/eval.rs index be14463..8ce0062 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -4,13 +4,13 @@ use snafu::{ResultExt, Snafu}; #[derive(Debug, Snafu)] pub enum Error { #[snafu(display("failed to parse command line '{}': {}", line, source))] - ParserError { + Parser { line: String, source: crate::parser::Error, }, #[snafu(display("failed to find command `{}`: {}", cmd, source))] - CommandError { + Command { cmd: String, source: crate::process::Error, }, @@ -50,7 +50,7 @@ pub struct Eval { impl Eval { fn new(line: &str) -> Result { let (cmd, args) = - crate::parser::parse(line).context(ParserError { line })?; + crate::parser::parse(line).context(Parser { line })?; let builtin_stream = crate::builtins::exec(&cmd, &args); let stream: Box< dyn futures::stream::Stream @@ -69,10 +69,10 @@ impl Eval { source: e, })) } - Err(e) => return Err(e).context(CommandError { cmd }), + Err(e) => return Err(e).context(Command { cmd }), } }; - Ok(Eval { stream }) + Ok(Self { stream }) } } diff --git a/src/main.rs b/src/main.rs index 3656616..3155564 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,13 @@ +#![warn(clippy::pedantic)] +#![warn(clippy::nursery)] +// clippy::cargo seems to be broken with rls currently +// #![warn(clippy::cargo)] +#![allow(clippy::collapsible_if)] +#![allow(clippy::module_name_repetitions)] +#![allow(clippy::multiple_crate_versions)] +#![allow(clippy::single_match)] +#![allow(clippy::write_with_newline)] + mod builtins; mod eval; mod parser; diff --git a/src/parser.rs b/src/parser.rs index 9d4125f..d8d1dd6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -10,7 +10,9 @@ pub type Result = std::result::Result; pub fn parse(line: &str) -> Result<(String, Vec)> { // TODO - let mut tokens = line.split_whitespace().map(|s| s.to_string()); + let mut tokens = line + .split_whitespace() + .map(std::string::ToString::to_string); let cmd = tokens.next().context(CommandRequired)?; Ok((cmd, tokens.collect())) } diff --git a/src/process.rs b/src/process.rs index 80d5ced..b7e3ed9 100644 --- a/src/process.rs +++ b/src/process.rs @@ -66,7 +66,7 @@ impl RunningProcess { // let input = tokio::io::stdin(); let input = tokio::reactor::PollEvented2::new(EventedStdin); - Ok(RunningProcess { + Ok(Self { pty, process, input, diff --git a/src/readline.rs b/src/readline.rs index c167e56..56fe91d 100644 --- a/src/readline.rs +++ b/src/readline.rs @@ -48,7 +48,7 @@ impl Readline { let screen = crossterm::RawScreen::into_raw_mode().context(IntoRawMode)?; - Ok(Readline { + Ok(Self { reader: None, state: ReadlineState { prompt: prompt.to_string(), @@ -82,18 +82,19 @@ impl ReadlineState { ) -> std::result::Result, Error> { match event { crossterm::InputEvent::Keyboard(e) => { - return self.process_keyboard_event(e) + return self.process_keyboard_event(&e) } _ => {} } - return Ok(futures::Async::NotReady); + + Ok(futures::Async::NotReady) } fn process_keyboard_event( &mut self, - event: crossterm::KeyEvent, + event: &crossterm::KeyEvent, ) -> std::result::Result, Error> { - match event { + match *event { crossterm::KeyEvent::Char(c) => { if self.cursor != self.buffer.len() && c != '\n' { self.echo(b"\x1b[@").context(WriteToTerminal)?; @@ -191,13 +192,14 @@ impl ReadlineState { } _ => {} } - return Ok(futures::Async::NotReady); + + Ok(futures::Async::NotReady) } fn write(&self, buf: &[u8]) -> std::io::Result<()> { let stdout = std::io::stdout(); let mut stdout = stdout.lock(); - stdout.write(buf)?; + stdout.write_all(buf)?; stdout.flush() } @@ -224,7 +226,7 @@ impl ReadlineState { } fn echo_char(&self, c: char) -> std::io::Result<()> { - let mut buf = [0u8; 4]; + let mut buf = [0_u8; 4]; self.echo(c.encode_utf8(&mut buf[..]).as_bytes()) } } @@ -288,14 +290,14 @@ impl KeyReader { if newline { break; } - if let Ok(_) = quit_rx.try_recv() { + if quit_rx.try_recv().is_ok() { break; } } }) .context(TerminalInputReadingThread)?; - Ok(KeyReader { + Ok(Self { events: events_rx, quit: quit_tx, }) diff --git a/src/repl.rs b/src/repl.rs index 3901e4c..8661022 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -6,13 +6,13 @@ use std::io::Write; #[derive(Debug, Snafu)] pub enum Error { #[snafu(display("error during read: {}", source))] - ReadError { source: crate::readline::Error }, + Read { source: crate::readline::Error }, #[snafu(display("error during eval: {}", source))] - EvalError { source: crate::eval::Error }, + Eval { source: crate::eval::Error }, #[snafu(display("error during print: {}", source))] - PrintError { source: std::io::Error }, + Print { source: std::io::Error }, } pub type Result = std::result::Result; @@ -34,7 +34,7 @@ pub fn repl() { futures::future::ok(Some(format!("{}", status))) } crate::eval::CommandEvent::BuiltinExit => { - futures::future::ok(Some(format!("success"))) + futures::future::ok(Some("success".to_string())) } }) }); @@ -42,33 +42,29 @@ pub fn repl() { Some(repl.then(move |res| match res { Ok(Some(status)) => { eprint!("command exited: {}\r\n", status); - return Ok((done, false)); + Ok((done, false)) } Ok(None) => { eprint!("command exited weirdly?\r\n"); - return Ok((done, false)); + Ok((done, false)) } - Err(Error::ReadError { + Err(Error::Read { source: crate::readline::Error::EOF, - }) => { - return Ok((done, true)); - } - Err(Error::EvalError { + }) => Ok((done, true)), + Err(Error::Eval { source: - crate::eval::Error::ParserError { + crate::eval::Error::Parser { source: crate::parser::Error::CommandRequired, - line: _, + .. }, - }) => { - return Ok((done, false)); - } + }) => Ok((done, false)), Err(e) => { let stderr = std::io::stderr(); let mut stderr = stderr.lock(); // panics seem fine for errors during error handling write!(stderr, "{}\r\n", e).unwrap(); stderr.flush().unwrap(); - return Ok((done, false)); + Ok((done, false)) } })) }); @@ -79,7 +75,7 @@ fn read() -> impl futures::future::Future { crate::readline::readline("$ ", true) .into_future() .flatten() - .map_err(|e| Error::ReadError { source: e }) + .map_err(|e| Error::Read { source: e }) } fn eval( @@ -89,12 +85,12 @@ fn eval( crate::eval::eval(line) .into_future() .flatten_stream() - .map_err(|e| Error::EvalError { source: e }) + .map_err(|e| Error::Eval { source: e }) } fn print(out: &[u8]) -> Result<()> { let stdout = std::io::stdout(); let mut stdout = stdout.lock(); - stdout.write(out).context(PrintError)?; - stdout.flush().context(PrintError) + stdout.write(out).context(Print)?; + stdout.flush().context(Print) } -- cgit v1.2.3-54-g00ecf