From 5f875d9920112c90bd9a79f129e0496b609912bd Mon Sep 17 00:00:00 2001 From: dvkt Date: Mon, 23 Dec 2019 11:06:52 -0800 Subject: [PATCH] err when trying to save bookmark --- src/bookmarks.rs | 6 +++--- src/config.rs | 27 ++++++++++++++------------- src/history.rs | 6 +++--- src/menu.rs | 15 ++++++++++----- src/ui.rs | 6 ++++-- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/bookmarks.rs b/src/bookmarks.rs index f305f5f..79222a4 100644 --- a/src/bookmarks.rs +++ b/src/bookmarks.rs @@ -1,6 +1,6 @@ use config; -use std::io::Read; +use std::io::{Read, Result}; const BOOKMARKS_FILE: &str = "bookmarks.gph"; @@ -17,6 +17,6 @@ pub fn as_raw_menu() -> String { } // save a single history entry -pub fn save(label: &str, url: &str) { - config::append(BOOKMARKS_FILE, label, url); +pub fn save(label: &str, url: &str) -> Result<()> { + config::append(BOOKMARKS_FILE, label, url) } diff --git a/src/config.rs b/src/config.rs index 1bc2f28..813ec38 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,26 +6,24 @@ pub const DIR: &str = "~/.config/phetch/"; // Loads a file in the config directory for reading. pub fn load(filename: &str) -> Result> { - if let Some(dotdir) = path() { + path().and_then(|dotdir| { let path = dotdir.join(filename); if let Ok(file) = OpenOptions::new().read(true).open(&path) { Ok(BufReader::new(file)) } else { Err(error!("Couldn't open {:?}", path)) } - } else { - Err(error!("{} directory doesn't exist", DIR)) - } + }) } // Append a menu item as a line to a file in the config dir. -pub fn append(filename: &str, label: &str, url: &str) { - if let Some(dotdir) = path() { - let filename = dotdir.join(filename); +pub fn append(filename: &str, label: &str, url: &str) -> Result<()> { + path().and_then(|dotdir| { + let path = dotdir.join(filename); if let Ok(mut file) = std::fs::OpenOptions::new() .append(true) .create(true) - .open(filename) + .open(path) { let (t, host, port, sel) = gopher::parse_url(&url); file.write_all( @@ -39,23 +37,26 @@ pub fn append(filename: &str, label: &str, url: &str) { ) .as_ref(), ); + Ok(()) + } else { + Err(error!("Can't open file for writing: {:?}", filename)) } - } + }) } // PathBuf to config dir if it exists. // None if the config dir doesn't exist. -pub fn path() -> Option { +pub fn path() -> Result { let homevar = std::env::var("HOME"); if homevar.is_err() { - return None; + return Err(error!("$HOME not set, can't decode `~`")); } let dotdir = DIR.replace('~', &homevar.unwrap()); let dotdir = std::path::Path::new(&dotdir); if dotdir.exists() { - Some(std::path::PathBuf::from(dotdir)) + Ok(std::path::PathBuf::from(dotdir)) } else { - None + Err(error!("Config dir not found: {}", DIR)) } } diff --git a/src/history.rs b/src/history.rs index 384d2fc..8f9f6aa 100644 --- a/src/history.rs +++ b/src/history.rs @@ -1,5 +1,5 @@ use config; -use std::io::BufRead; +use std::io::{BufRead, Result}; const HISTORY_FILE: &str = "history.gph"; @@ -23,6 +23,6 @@ pub fn as_raw_menu() -> String { } // save a single history entry -pub fn save(label: &str, url: &str) { - config::append(HISTORY_FILE, label, url); +pub fn save(label: &str, url: &str) -> Result<()> { + config::append(HISTORY_FILE, label, url) } diff --git a/src/menu.rs b/src/menu.rs index 8d9cc8d..4f8a24b 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -470,7 +470,7 @@ impl Menu { self.input.clear(); if let Some(line) = self.link(self.link) { let url = line.url.to_string(); - let (typ, _, _, _) = gopher::parse_url(&url); + let (typ, host, _, _) = gopher::parse_url(&url); match typ { Type::Search => { if let Some(query) = ui::prompt(&format!("{}> ", line.name)) { @@ -482,10 +482,15 @@ impl Menu { Type::Error => Action::Error(line.name.to_string()), Type::Telnet => Action::Error("Telnet support coming soon".into()), _ => { - let hurl = url.to_string(); - let hname = line.name.clone(); - thread::spawn(move || history::save(&hurl, &hname)); - Action::Open(url) + // don't record internal urls + if host != "help" { + let hurl = url.to_string(); + let hname = line.name.clone(); + thread::spawn(move || history::save(&hurl, &hname)); + Action::Open(url) + } else { + Action::None + } } } } else { diff --git a/src/ui.rs b/src/ui.rs index d4cbbbf..1c24756 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -328,8 +328,10 @@ impl UI { Action::Keypress(Key::Ctrl('s')) => { if let Some(page) = self.views.get(self.focused) { let url = page.url(); - self.set_status(format!("Saved bookmark: {}", url)); - bookmarks::save(&url, &url); + match bookmarks::save(&url, &url) { + Ok(()) => self.set_status(format!("Saved bookmark: {}", url)), + Err(e) => return Err(error!("Save failed: {}", e)), + } } } Action::Keypress(Key::Ctrl('u')) => {