From dce0729e7aba869bd61dc56bda686fac450439a9 Mon Sep 17 00:00:00 2001 From: chris west Date: Wed, 15 Jan 2020 19:51:28 -0800 Subject: [PATCH] deal with unused return values --- src/bookmarks.rs | 15 +++++---- src/gopher.rs | 12 +++---- src/history.rs | 13 +++----- src/lib.rs | 1 - src/phetchdir.rs | 8 ++--- src/ui.rs | 86 +++++++++++++++++++++++++++--------------------- src/ui/action.rs | 4 +-- 7 files changed, 75 insertions(+), 64 deletions(-) diff --git a/src/bookmarks.rs b/src/bookmarks.rs index 90db679..8baaf48 100644 --- a/src/bookmarks.rs +++ b/src/bookmarks.rs @@ -33,12 +33,15 @@ pub fn as_raw_menu() -> String { return out; } - phetchdir::load(BOOKMARKS_FILE) - .and_then(|mut reader| reader.read_to_string(&mut out)) - .map_err(|e| { - out = format!("3{}", e); - e - }); + match phetchdir::load(BOOKMARKS_FILE) { + Ok(mut reader) => { + if let Err(e) = reader.read_to_string(&mut out) { + out = format!("3{}", e); + } + } + Err(e) => out = format!("3{}", e), + } + out } diff --git a/src/gopher.rs b/src/gopher.rs index 9ff9810..68e8c58 100644 --- a/src/gopher.rs +++ b/src/gopher.rs @@ -163,8 +163,8 @@ pub fn request(host: &str, port: &str, selector: &str, tls: bool, tor: bool) -> let stream = TcpStream::connect_timeout(&sock, TCP_TIMEOUT_DURATION)?; stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION))?; if let Ok(mut stream) = connector.connect(host, stream) { - stream.write_all(selector.as_ref()); - stream.write_all("\r\n".as_ref()); + stream.write_all(selector.as_ref())?; + stream.write_all("\r\n".as_ref())?; return Ok(Stream { io: Box::new(stream), tls: true, @@ -188,8 +188,8 @@ pub fn request(host: &str, port: &str, selector: &str, tls: bool, tor: bool) -> Ok(s) => s, Err(e) => return Err(error!("Tor error: {}", e)), }; - stream.write_all(selector.as_ref()); - stream.write_all("\r\n".as_ref()); + stream.write_all(selector.as_ref())?; + stream.write_all("\r\n".as_ref())?; return Ok(Stream { io: Box::new(stream), tls: false, @@ -200,8 +200,8 @@ pub fn request(host: &str, port: &str, selector: &str, tls: bool, tor: bool) -> // no tls or tor, try regular connection let mut stream = TcpStream::connect_timeout(&sock, TCP_TIMEOUT_DURATION)?; stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION))?; - stream.write_all(selector.as_ref()); - stream.write_all("\r\n".as_ref()); + stream.write_all(selector.as_ref())?; + stream.write_all("\r\n".as_ref())?; Ok(Stream { io: Box::new(stream), tls: false, diff --git a/src/history.rs b/src/history.rs index 09ce33f..ddebb66 100644 --- a/src/history.rs +++ b/src/history.rs @@ -42,18 +42,15 @@ pub fn as_raw_menu() -> String { } let mut out = vec![format!("i{}:\r\ni", homepath)]; - phetchdir::load(HISTORY_FILE) - .and_then(|reader| { + match phetchdir::load(HISTORY_FILE) { + Ok(reader) => { let mut lines = reader.lines(); while let Some(Ok(line)) = lines.next() { out.insert(1, line); } - Ok(()) - }) - .map_err(|e| { - out.push(format!("3{}", e)); - e - }); + } + Err(e) => out.push(format!("3{}", e)), + } if out.len() == 1 { out.insert(1, "iNo history entries yet.".to_string()); diff --git a/src/lib.rs b/src/lib.rs index 86dd02c..f1aacb6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,6 @@ //! Suggestions and improvements are more than welcome. //! -#![allow(unused_must_use)] #![warn(absolute_paths_not_starting_with_crate)] #![warn(explicit_outlives_requirements)] #![warn(unreachable_pub)] diff --git a/src/phetchdir.rs b/src/phetchdir.rs index a6ccf2d..b18a598 100644 --- a/src/phetchdir.rs +++ b/src/phetchdir.rs @@ -71,8 +71,8 @@ pub fn prepend(filename: &str, label: &str, url: &str) -> Result<()> { { let url = gopher::parse_url(&url); let mut buf = vec![]; - file.read_to_end(&mut buf); - file.seek(std::io::SeekFrom::Start(0)); + file.read_to_end(&mut buf)?; + file.seek(std::io::SeekFrom::Start(0))?; write!( file, "{}{}\t{}\t{}\t{}\r\n", @@ -81,8 +81,8 @@ pub fn prepend(filename: &str, label: &str, url: &str) -> Result<()> { url.sel, url.host, url.port - ); - file.write_all(&buf); + )?; + file.write_all(&buf)?; Ok(()) } else { Err(error!("Can't open file for writing: {:?}", filename)) diff --git a/src/ui.rs b/src/ui.rs index 633b5d0..a881674 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -49,11 +49,20 @@ pub type Page = Box; /// How many lines to jump by when using page up/down. pub const SCROLL_LINES: usize = 15; + /// How big the longest line can be, for the purposes of calculating /// margin sizes. We often draw longer lines than this and allow /// wrapping in text views. pub const MAX_COLS: usize = 77; +/// Fatal errors. In general we want to try and catch any errors +/// (network, parsing gopher response, etc) and just show an error +/// message in the status bar, but if we can't write to STDOUT or +/// control the screen, we need to just crash. +const ERR_RAW_MODE: &str = "Fatal Error using Raw Mode."; +const ERR_SCREEN: &str = "Fatal Error using Alternate Screen."; +const ERR_STDOUT: &str = "Fatal Error writing to STDOUT."; + /// UI is mainly concerned with drawing to the screen, managing the /// active Views/pages, and responding to user input. pub struct UI { @@ -85,10 +94,8 @@ impl UI { // Store raw terminal but don't enable it yet or switch the // screen. We don't want to stare at a fully blank screen // while waiting for a slow page to load. - let out = stdout() - .into_raw_mode() - .expect("Failed to initialize raw mode."); - out.suspend_raw_mode(); + let out = stdout().into_raw_mode().expect(ERR_RAW_MODE); + out.suspend_raw_mode().expect(ERR_RAW_MODE); UI { views: vec![], @@ -106,15 +113,15 @@ impl UI { /// mode, eg inside run() pub fn startup(&mut self) { let mut out = self.out.borrow_mut(); - out.activate_raw_mode(); - write!(out, "{}", termion::screen::ToAlternateScreen); + out.activate_raw_mode().expect(ERR_RAW_MODE); + write!(out, "{}", termion::screen::ToAlternateScreen).expect(ERR_SCREEN); } /// Clean up after ourselves. Should only be used after running in /// interactive mode. pub fn shutdown(&mut self) { let mut out = self.out.borrow_mut(); - write!(out, "{}", termion::screen::ToMainScreen); + write!(out, "{}", termion::screen::ToMainScreen).expect(ERR_SCREEN); } /// Main loop. @@ -141,13 +148,13 @@ impl UI { termion::cursor::Hide, screen, status, - ); - out.flush(); + )?; + out.flush()?; self.dirty = false; } else { let mut out = self.out.borrow_mut(); - out.write_all(status.as_ref()); - out.flush(); + out.write_all(status.as_ref())?; + out.flush()?; } Ok(()) } @@ -306,13 +313,13 @@ impl UI { color::Reset, termion::cursor::Show, ); - stdout().flush(); + stdout().flush().expect(ERR_STDOUT); thread::sleep(Duration::from_millis(500)); } }); let result = req.join(); - tx.send(true); // stop spinner + tx.send(true).expect("Fatal Error in Spinner channel."); // stop spinner self.dirty = true; result.map_err(|e| error!("Spinner error: {:?}", e)) } @@ -404,8 +411,9 @@ impl UI { termion::clear::CurrentLine, question, termion::cursor::Show, - ); - out.flush(); + ) + .expect(ERR_STDOUT); + out.flush().expect(ERR_STDOUT); if let Some(Ok(key)) = stdin().keys().next() { match key { @@ -433,8 +441,9 @@ impl UI { prompt, input, termion::cursor::Show, - ); - out.flush(); + ) + .expect(ERR_STDOUT); + out.flush().expect(ERR_STDOUT); for k in stdin().keys() { if let Ok(key) = k { @@ -445,8 +454,9 @@ impl UI { "{}{}", termion::clear::CurrentLine, termion::cursor::Hide - ); - out.flush(); + ) + .expect(ERR_STDOUT); + out.flush().expect(ERR_STDOUT); return Some(input); } Key::Char(c) => input.push(c), @@ -456,8 +466,9 @@ impl UI { "{}{}", termion::clear::CurrentLine, termion::cursor::Hide - ); - out.flush(); + ) + .expect(ERR_STDOUT); + out.flush().expect(ERR_STDOUT); return None; } Key::Backspace | Key::Delete => { @@ -476,8 +487,9 @@ impl UI { termion::clear::CurrentLine, prompt, input, - ); - out.flush(); + ) + .expect(ERR_STDOUT); + out.flush().expect(ERR_STDOUT); } if !input.is_empty() { @@ -491,15 +503,15 @@ impl UI { fn telnet(&mut self, url: &str) -> Result<()> { let gopher::Url { host, port, .. } = gopher::parse_url(url); let out = self.out.borrow_mut(); - out.suspend_raw_mode(); + out.suspend_raw_mode().expect(ERR_RAW_MODE); let mut cmd = process::Command::new("telnet") .arg(host) .arg(port) .stdin(Stdio::inherit()) .stdout(Stdio::inherit()) .spawn()?; - cmd.wait(); - out.activate_raw_mode(); + cmd.wait()?; + out.activate_raw_mode().expect(ERR_RAW_MODE); self.dirty = true; // redraw when finished with session Ok(()) } @@ -524,11 +536,11 @@ impl UI { /// Ctrl-Z: Suspend Unix process w/ SIGTSTP. fn suspend(&mut self) { let mut out = self.out.borrow_mut(); - write!(out, "{}", termion::screen::ToMainScreen); - out.flush(); + write!(out, "{}", termion::screen::ToMainScreen).expect(ERR_SCREEN); + out.flush().expect(ERR_STDOUT); unsafe { libc::raise(libc::SIGTSTP) }; - write!(out, "{}", termion::screen::ToAlternateScreen); - out.flush(); + write!(out, "{}", termion::screen::ToAlternateScreen).expect(ERR_SCREEN); + out.flush().expect(ERR_STDOUT); self.dirty = true; } @@ -538,7 +550,7 @@ impl UI { match action { Action::List(actions) => { for action in actions { - self.process_action(action); + self.process_action(action)?; } } Action::Keypress(Key::Ctrl('c')) => { @@ -550,14 +562,14 @@ impl UI { Action::Redraw => self.dirty = true, Action::Draw(s) => { let mut out = self.out.borrow_mut(); - out.write_all(s.as_ref()); - out.flush(); + out.write_all(s.as_ref())?; + out.flush()?; } Action::Status(s) => self.set_status(&s), Action::Open(title, url) => self.open(&title, &url)?, Action::Prompt(query, fun) => { if let Some(response) = self.prompt(&query, "") { - self.process_action(fun(response)); + self.process_action(fun(response))?; } } Action::Keypress(Key::Left) | Action::Keypress(Key::Backspace) => { @@ -607,7 +619,7 @@ impl UI { let current_url = page.url(); if let Some(url) = self.prompt("Current URL: ", ¤t_url) { if url != current_url { - self.open(&url, &url); + self.open(&url, &url)?; } } } @@ -636,8 +648,8 @@ impl UI { impl Drop for UI { fn drop(&mut self) { let mut out = self.out.borrow_mut(); - write!(out, "{}{}", color::Reset, termion::cursor::Show,); - out.flush(); + write!(out, "{}{}", color::Reset, termion::cursor::Show).expect(ERR_STDOUT); + out.flush().expect(ERR_STDOUT); } } diff --git a/src/ui/action.rs b/src/ui/action.rs index 84f784b..59c0299 100644 --- a/src/ui/action.rs +++ b/src/ui/action.rs @@ -52,9 +52,9 @@ impl fmt::Debug for Action { Action::Status(s) => write!(f, "Status: {}", s), Action::Prompt(s, _) => write!(f, "Prompt: {}", s), Action::List(li) => { - writeln!(f, "List: "); + writeln!(f, "List: ")?; for a in li { - write!(f, "{:?}", a); + write!(f, "{:?}", a)?; } Ok(()) }