diff --git a/src/devhost/sdwirec.rs b/src/devhost/sdwirec.rs index ddbf7eb..945d076 100644 --- a/src/devhost/sdwirec.rs +++ b/src/devhost/sdwirec.rs @@ -74,11 +74,7 @@ impl Sdwirec { cmd += &self.format_device(chooser); cmd += "-u"; - let res = self.try_run(&cmd); - if let Err(e) = res { - return Err(e); - } - let res = res.unwrap(); + let res = self.try_run(&cmd)?; if !res.status.success() { return Err(Box::new(std::io::Error::new( std::io::ErrorKind::Other, @@ -96,14 +92,14 @@ impl Sdwirec { */ let res = String::from_utf8(res.stdout).unwrap(); if res.contains("SD connected to: TS") { - return Ok(SdwirecStat::TS); + Ok(SdwirecStat::TS) } else if res.contains("SD connected to: DUT") { - return Ok(SdwirecStat::DUT); + Ok(SdwirecStat::DUT) } else { - return Err(Box::new(std::io::Error::new( + Err(Box::new(std::io::Error::new( std::io::ErrorKind::Other, format!("Failed to get status of device. Reason: {}", res), - ))); + ))) } } @@ -116,11 +112,7 @@ impl Sdwirec { cmd += &self.format_device(chooser); cmd += "-ts"; - let res = self.try_run(&cmd); - if let Err(e) = res { - return Err(e); - } - let res = res.unwrap(); + let res = self.try_run(&cmd)?; if !res.status.success() { return Err(Box::new(std::io::Error::new( std::io::ErrorKind::Other, @@ -143,11 +135,7 @@ impl Sdwirec { cmd += &self.format_device(chooser); cmd += "-d"; - let res = self.try_run(&cmd); - if let Err(e) = res { - return Err(e); - } - let res = res.unwrap(); + let res = self.try_run(&cmd)?; if !res.status.success() { return Err(Box::new(std::io::Error::new( std::io::ErrorKind::Other, diff --git a/src/exec/cli_exec.rs b/src/exec/cli_exec.rs index 4261e74..a9c5b89 100644 --- a/src/exec/cli_exec.rs +++ b/src/exec/cli_exec.rs @@ -24,11 +24,7 @@ impl CliTester { impl CliTester { fn run_command(&mut self, command: &String) -> Result<(), Box> { info!("Write to shell: {}", command); - let res = self.inner.write(command.as_bytes()); - if let Err(e) = res { - return Err(e); - } - Ok(()) + self.inner.write(command.as_bytes()) } } @@ -81,12 +77,8 @@ impl CliTestApi for CliTester { info!("Waiting for string {{{}}}", expected); loop { sleep(Duration::from_millis(DURATION)); - let res = self.inner.read(); - if let Err(e) = res { - return Err(e); - } - let line = res.unwrap(); - buf.extend_from_slice(&line); + let res = self.inner.read()?; + buf.extend_from_slice(&res); let content = String::from_utf8(buf.clone()).unwrap_or_default(); if content.contains(expected) { info!("Matched string {{{}}}", expected); @@ -112,10 +104,7 @@ impl CliTestApi for CliTester { cmd += &echo_content_rand; cmd += " \n"; - let res = self.run_command(&cmd); - if let Err(e) = res { - return Err(e); - } + self.run_command(&cmd)?; let res = self.wait_serial(&echo_content_rand, timeout); if let Err(e) = res { @@ -124,7 +113,7 @@ impl CliTestApi for CliTester { } return Err(e); } - res + Ok(()) } fn assert_script_run(&mut self, script: &str, timeout: u32) -> Result<(), Box> { let mut cmd = script.to_owned(); @@ -134,13 +123,10 @@ impl CliTestApi for CliTester { cmd += &echo_content_rand; cmd += " \n"; - let res = self.run_command(&cmd); - if let Err(e) = res { - return Err(e); - } + self.run_command(&cmd)?; - let res = self.wait_serial(&echo_content_rand, timeout); - res + self.wait_serial(&echo_content_rand, timeout)?; + Ok(()) } fn background_script_run(&mut self, script: &str) -> Result<(), Box> { let mut cmd = script.to_owned(); diff --git a/src/pythonapi/pyexec.rs b/src/pythonapi/pyexec.rs index 9fc457a..0302f54 100644 --- a/src/pythonapi/pyexec.rs +++ b/src/pythonapi/pyexec.rs @@ -65,12 +65,12 @@ impl PyExec { let timeout = timeout.unwrap_or(30); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .script_run(script, timeout) .map_err(|e| PyRuntimeError::new_err(e.to_string()))?; - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .script_run(script, timeout) @@ -95,12 +95,12 @@ impl PyExec { let timeout = timeout.unwrap_or(30); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .assert_script_run(script, timeout) .map_err(|e| PyRuntimeError::new_err(e.to_string()))?; - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .assert_script_run(script, timeout) @@ -118,12 +118,12 @@ impl PyExec { let inner = self_.inner.get_mut()?; let inner = inner.as_any_mut(); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .background_script_run(script) .map_err(|e| PyRuntimeError::new_err(e.to_string()))?; - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .background_script_run(script) @@ -141,12 +141,12 @@ impl PyExec { let inner = self_.inner.get_mut()?; let inner = inner.as_any_mut(); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .writeln(script) .map_err(|e| PyRuntimeError::new_err(e.to_string()))?; - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .writeln(script) @@ -171,12 +171,12 @@ impl PyExec { let timeout = timeout.unwrap_or(30); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .wait_serial(expected, timeout) .map_err(|e| PyRuntimeError::new_err(e.to_string()))?; - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .wait_serial(expected, timeout) @@ -201,7 +201,7 @@ impl PyExec { let timeout = timeout.unwrap_or(30); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .script_sudo(script, timeout) @@ -226,7 +226,7 @@ impl PyExec { let timeout = timeout.unwrap_or(30); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .assert_script_sudo(script, timeout) diff --git a/src/pythonapi/pyhook.rs b/src/pythonapi/pyhook.rs index bd472b3..34695ed 100644 --- a/src/pythonapi/pyhook.rs +++ b/src/pythonapi/pyhook.rs @@ -18,10 +18,9 @@ pub fn build_ttyhook(inner: Py) -> PyTty { let inner = PyTtyWrapper { tty: heap_raw(inner) }; - let res = PyTty { + PyTty { inner - }; - res + } } pub struct PyTtyHook { diff --git a/src/pythonapi/shell_like.rs b/src/pythonapi/shell_like.rs index 0ac0020..5a9c07c 100644 --- a/src/pythonapi/shell_like.rs +++ b/src/pythonapi/shell_like.rs @@ -231,7 +231,7 @@ impl PyTty { let inner = Box::into_inner(inner); let inner = inner.into_any(); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast::().unwrap(); let inner = inner.exit(); Ok(PyTty { @@ -239,7 +239,7 @@ impl PyTty { tty: heap_raw(inner), }, }) - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast::().unwrap(); let inner = inner.exit(); Ok(PyTty { @@ -247,7 +247,7 @@ impl PyTty { tty: heap_raw(inner), }, }) - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast::().unwrap(); let inner = inner.exit(); Ok(PyTty { @@ -255,7 +255,7 @@ impl PyTty { tty: heap_raw(inner), }, }) - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast::().unwrap(); let inner = inner.exit(); Ok(PyTty { @@ -263,7 +263,7 @@ impl PyTty { tty: heap_raw(inner), }, }) - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast::().unwrap(); let inner = inner.exit(); Ok(PyTty { @@ -284,12 +284,12 @@ impl PyTty { let inner = self.inner.get_mut()?; let inner = inner.as_any_mut(); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .begin() .map_err(|e| PyRuntimeError::new_err(e.to_string())) - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .begin() @@ -305,12 +305,12 @@ impl PyTty { let inner = self.inner.get_mut()?; let inner = inner.as_any_mut(); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .end() .map_err(|e| PyRuntimeError::new_err(e.to_string())) - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .end() @@ -326,12 +326,12 @@ impl PyTty { let inner = self.inner.get_mut()?; let inner = inner.as_any_mut(); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .start() .map_err(|e| PyRuntimeError::new_err(e.to_string())) - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .start() @@ -347,12 +347,12 @@ impl PyTty { let inner = self.inner.get_mut()?; let inner = inner.as_any_mut(); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .pause() .map_err(|e| PyRuntimeError::new_err(e.to_string())) - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); inner .pause() @@ -368,7 +368,7 @@ impl PyTty { let inner = self.inner.get_mut()?; let inner = inner.as_any_mut(); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); let target = other.inner.safe_take()?; let target = Box::into_inner(target); @@ -379,7 +379,7 @@ impl PyTty { let target = target.unwrap(); other.inner.tty = heap_raw(target); Ok(()) - } else if let Some(_) = inner.downcast_ref::() { + } else if inner.downcast_ref::().is_some() { let inner = inner.downcast_mut::().unwrap(); let target = other.inner.safe_take()?; let target = Box::into_inner(target); @@ -403,7 +403,7 @@ impl PyTty { let inner = Box::into_inner(inner); let inner = inner.into_any(); - if let Some(_) = inner.downcast_ref::() { + if inner.downcast_ref::().is_some() { let inner = inner.downcast::().unwrap(); let res = inner.inner; Ok(res) diff --git a/src/term/asciicast.rs b/src/term/asciicast.rs index 1abbef7..7e9c1d7 100644 --- a/src/term/asciicast.rs +++ b/src/term/asciicast.rs @@ -71,7 +71,7 @@ impl Asciicast { } let inner = inner.as_mut().unwrap(); let new_data = inner.read(); - if let Err(_) = new_data { + if new_data.is_err() { return; } let new_data = new_data.unwrap(); @@ -118,14 +118,14 @@ impl AnyBase for Asciicast { impl Tty for Asciicast { fn read(&mut self) -> Result, Box> { let data = self.data.lock(); - if let Err(_) = data { + if data.is_err() { return Err(Box::::from("Read from Asciicast failed.")); } let mut data = data.unwrap(); let res = data.clone(); data.clear(); - return Ok(res); + Ok(res) } fn read_line(&mut self) -> Result, Box> { let mut res = Vec::new(); @@ -141,11 +141,11 @@ impl Tty for Asciicast { break; } } - return Ok(res); + Ok(res) } fn write(&mut self, data: &[u8]) -> Result<(), Box> { // let begin = self.begin.lock(); - // if let Err(_) = begin { + // if begin.is_err() { // return Err(Box::::from("Recorder not started.")); // } // let begin = begin.unwrap(); @@ -172,9 +172,7 @@ impl Tty for Asciicast { return Err(Box::::from("You've already exited.")); } let inner = inner.as_mut().unwrap(); - let res = inner.write(data); - - res + inner.write(data) } } @@ -182,15 +180,14 @@ impl WrapperTty for Asciicast { fn exit(self) -> DynTty { let inner = self.inner.clone(); let mut inner = inner.lock().unwrap(); - let inner = inner.take().unwrap(); - inner + inner.take().unwrap() } } impl Recorder for Asciicast { fn begin(&mut self) -> Result<(), Box> { let logged = self.logged.lock(); - if let Err(_) = logged { + if logged.is_err() { return Err(Box::::from("Recorder not started.")); } let mut logged = logged.unwrap(); @@ -198,14 +195,14 @@ impl Recorder for Asciicast { let time = SystemTime::now(); let begin_time = self.begin_time.lock(); - if let Err(_) = begin_time { + if begin_time.is_err() { return Err(Box::::from("Recorder not started.")); } let mut begin_time = begin_time.unwrap(); *begin_time = time; let begin = self.begin.lock(); - if let Err(_) = begin { + if begin.is_err() { return Err(Box::::from("Recorder not started.")); } let mut begin = begin.unwrap(); @@ -218,7 +215,7 @@ impl Recorder for Asciicast { fn end(&mut self) -> Result> { let begin = self.begin.lock(); - if let Err(_) = begin { + if begin.is_err() { return Err(Box::::from("Recorder not started.")); } let mut begin = begin.unwrap(); @@ -228,7 +225,7 @@ impl Recorder for Asciicast { *begin = false; let mut res = String::new(); let logged = self.logged.lock(); - if let Err(_) = logged { + if logged.is_err() { return Err(Box::::from("Recorder not started.")); } let logged = logged.unwrap(); diff --git a/src/term/recorder.rs b/src/term/recorder.rs index 3e6a684..b73082c 100644 --- a/src/term/recorder.rs +++ b/src/term/recorder.rs @@ -1,6 +1,6 @@ use std::{any::Any, error::Error, mem::replace}; -use crate::{consts::SHELL_PROMPT, info, term::tty::Tty, util::anybase::AnyBase}; +use crate::{info, term::tty::Tty, util::anybase::AnyBase}; use super::tty::{DynTty, WrapperTty}; @@ -47,45 +47,27 @@ impl AnyBase for SimpleRecorder { impl Tty for SimpleRecorder { fn read(&mut self) -> Result, Box> { - let data = self.inner.read(); - if let Err(e) = data { - return Err(e); - } - let data = data.unwrap(); + let data = self.inner.read()?; if self.begin { self.logged.extend(data.clone()); } - return Ok(data); + Ok(data) } fn read_line(&mut self) -> Result, Box> { - let data = self.inner.read_line(); - if let Err(e) = data { - return Err(e); - } - let data = data.unwrap(); + let data = self.inner.read_line()?; if self.begin { self.logged.extend(data.clone()); } - return Ok(data); + Ok(data) } fn write(&mut self, data: &[u8]) -> Result<(), Box> { - let res = self.inner.write(data); - if let Err(e) = res { - return Err(e); - } - - if self.begin { - // For echo back: - let line = SHELL_PROMPT.as_bytes(); - self.logged.extend(line); - self.logged.extend(data); - } + self.inner.write(data)?; - return Ok(()); + Ok(()) } } @@ -103,7 +85,7 @@ impl Recorder for SimpleRecorder info!("Recorder begin to record."); - return Ok(()); + Ok(()) } fn end(&mut self) -> Result> { @@ -118,20 +100,20 @@ impl Recorder for SimpleRecorder info!("Recorder end to record."); - return Ok(String::from_utf8(logged).unwrap()); + Ok(String::from_utf8(logged).unwrap()) } fn pause(&mut self) -> Result<(), Box> { self.begin = false; info!("Recorder pause for recording..."); - return Ok(()); + Ok(()) } fn start(&mut self) -> Result<(), Box> { self.begin = true; info!("Recorder continue for recording..."); - return Ok(()); + Ok(()) } fn swap(&mut self, target: DynTty) -> Result> { let inner = replace(&mut self.inner, target); - return Ok(inner); + Ok(inner) } } diff --git a/src/term/serial.rs b/src/term/serial.rs index 1259616..503619e 100644 --- a/src/term/serial.rs +++ b/src/term/serial.rs @@ -28,9 +28,9 @@ impl Serial { info!("Serial port opened: {} at baud rate {}", port, baud); - return Ok(Serial { + Ok(Serial { inner: inner.unwrap(), - }); + }) } } @@ -109,6 +109,6 @@ impl Tty for Serial { } } - return Ok(()); + Ok(()) } } diff --git a/src/term/shell.rs b/src/term/shell.rs index 7769335..1e8f262 100644 --- a/src/term/shell.rs +++ b/src/term/shell.rs @@ -1,5 +1,5 @@ use ptyprocess::{stream::Stream, PtyProcess}; -use std::io::{BufRead, BufReader, Read}; +use std::io::BufReader; use std::ops::DerefMut; use std::os::fd::AsRawFd; use std::{ @@ -26,20 +26,13 @@ pub struct Shell { } impl Shell { - /** - * This implementation method is DEFINITELY needs to be changed in the future, - * at least need to use a stty to let shell HAPPY. - * But for now... Well, it works. - * I've already spent too much time trying to make this thing work... Just move on. - * For now. - */ pub fn build(shell: Option<&str>) -> Result> { let shell = shell.unwrap_or("/bin/sh"); info!("Spawn shell process: {}", shell); let mut inner = Command::new(shell); - inner.args(&["-i"]); + inner.args(["-i"]); let inner = PtyProcess::spawn(inner); if let Err(e) = inner { err!("Failed to spawn shell process. Reason: {}", e); @@ -144,7 +137,7 @@ impl Tty for Shell { if !res.is_empty() { log!("Shell read: {:?}", String::from_utf8_lossy(&res)); } - return Ok(res); + Ok(res) } fn read_line(&mut self) -> Result, Box> { let mut res = Vec::new(); @@ -167,20 +160,20 @@ impl Tty for Shell { break; } } - return Ok(res); + Ok(res) } fn write(&mut self, data: &[u8]) -> Result<(), Box> { let mut stream = self.inner.lock().unwrap(); info!("Shell locked..."); - match stream.write(data) { + match stream.write_all(data) { Ok(_) => { stream.flush().unwrap(); info!("Shell write: {:?}", String::from_utf8_lossy(data)); - return Ok(()); + Ok(()) } Err(e) => { err!("Write to shell process failed. Reason: {}", e); - return Err(Box::new(e)); + Err(Box::new(e)) } } } diff --git a/src/term/ssh.rs b/src/term/ssh.rs index 130fb8e..ebfedb1 100644 --- a/src/term/ssh.rs +++ b/src/term/ssh.rs @@ -190,6 +190,6 @@ impl Tty for Ssh { err!("Flush to shell process failed. Reason: {}", e); return Err(Box::::from(e)); } - return Ok(()); + Ok(()) } } diff --git a/src/util/logger.rs b/src/util/logger.rs index c219bf3..f8b94dd 100644 --- a/src/util/logger.rs +++ b/src/util/logger.rs @@ -12,47 +12,47 @@ pub enum LogLevel { singleton!(LogLevelConf, i32, LogLevel::Info as i32); pub fn __log(s: &str) { - if LogLevelConf::get().to_owned() <= LogLevel::Debug as i32 { + if *LogLevelConf::get() <= LogLevel::Debug as i32 { println!("{} {}", "[LOG]".blue(), s); } } #[macro_export] macro_rules! log { - ($($arg:tt)*) => (crate::util::logger::__log(&format!($($arg)*))) + ($($arg:tt)*) => ($crate::util::logger::__log(&format!($($arg)*))) } pub fn __info(s: &str) { - if LogLevelConf::get().to_owned() <= LogLevel::Info as i32 { + if *LogLevelConf::get() <= LogLevel::Info as i32 { println!("{} {}", "[INFO]".green(), s); } } #[macro_export] macro_rules! info { - ($($arg:tt)*) => (crate::util::logger::__info(&format!($($arg)*))) + ($($arg:tt)*) => ($crate::util::logger::__info(&format!($($arg)*))) } pub fn __warn(s: &str) { - if LogLevelConf::get().to_owned() <= LogLevel::Warn as i32 { + if *LogLevelConf::get() <= LogLevel::Warn as i32 { eprintln!("{} {}", "[WARN]".yellow(), s); } } #[macro_export] macro_rules! warn { - ($($arg:tt)*) => (crate::util::logger::__warn(&format!($($arg)*))) + ($($arg:tt)*) => ($crate::util::logger::__warn(&format!($($arg)*))) } pub fn __err(s: &str) { - if LogLevelConf::get().to_owned() <= LogLevel::Error as i32 { + if *LogLevelConf::get() <= LogLevel::Error as i32 { eprintln!("{} {}", "[ERROR]".red(), s); } } #[macro_export] macro_rules! err { - ($($arg:tt)*) => (crate::util::logger::__err(&format!($($arg)*))) + ($($arg:tt)*) => ($crate::util::logger::__err(&format!($($arg)*))) } pub fn set_log_level(level: LogLevel) { diff --git a/src/util/util.rs b/src/util/util.rs index da40eb1..8d79f74 100644 --- a/src/util/util.rs +++ b/src/util/util.rs @@ -20,12 +20,10 @@ macro_rules! unfinished { } pub fn rand_string(len: usize) -> Vec { - let rnd = thread_rng() + thread_rng() .sample_iter(&Alphanumeric) .take(len) - .collect::>(); - - rnd + .collect::>() } pub fn try_read(