From c23956bcbdd79881cda526986f8109b130df1667 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Sun, 3 Mar 2019 13:14:15 -0500 Subject: [PATCH] don't panic on missing/invalid config file, try to be helpful instead --- Cargo.toml | 2 +- src/config.rs | 14 +------------- src/main.rs | 38 +++++++++++++++++++++++++++----------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 55d3ab8..1a04597 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,4 +24,4 @@ default-features = false [features] pulseaudio_backend = ["librespot/pulseaudio-backend"] portaudio_backend = ["librespot/portaudio-backend"] -default = ["pulseaudio_backend"] \ No newline at end of file +default = ["pulseaudio_backend"] diff --git a/src/config.rs b/src/config.rs index 62883ea..2224eb6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,19 +1,7 @@ -use std::fs::File; -use std::io::prelude::*; - pub const CLIENT_ID: &str = "d420a117a32841c2b3474932e49fb54b"; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Default)] pub struct Config { pub username: String, pub password: String, } - -pub fn load(filename: &str) -> Result { - let mut f = File::open(filename).expect("ncspot configuration file not found"); - let mut contents = String::new(); - f.read_to_string(&mut contents) - .expect("something went wrong reading the file"); - - toml::from_str(&contents) -} diff --git a/src/main.rs b/src/main.rs index 575f207..561c6a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,11 +63,36 @@ fn init_logger(content: TextContent) { } fn main() { - let logbuf = TextContent::new("Welcome to ncspot\n"); - let logview = TextView::new_with_content(logbuf.clone()); std::env::set_var("RUST_LOG", "ncspot=trace"); std::env::set_var("RUST_BACKTRACE", "full"); + // Things here may cause the process to abort; we must do them before creating curses windows + // otherwise the error message will not be seen by a user + let path = match env::var_os("HOME") { + None => { + eprintln!("$HOME not set"); + process::exit(1); + } + Some(path) => PathBuf::from(format!("{0}/.config/ncspot", path.into_string().unwrap())), + }; + + let cfg: config::Config = { + let contents = std::fs::read_to_string(&path).unwrap_or_else(|_| { + eprintln!("Cannot read config file from {}", path.to_str().unwrap()); + eprintln!( + "Expected a config file with this format:\n{}", + toml::to_string_pretty(&config::Config::default()).unwrap() + ); + process::exit(1) + }); + toml::from_str(&contents).unwrap_or_else(|e| { + eprintln!("{}", e); + process::exit(1) + }) + }; + + let logbuf = TextContent::new("Welcome to ncspot\n"); + let logview = TextView::new_with_content(logbuf.clone()); init_logger(logbuf); let mut cursive = Cursive::default(); @@ -76,15 +101,6 @@ fn main() { cursive.add_global_callback('q', |s| s.quit()); cursive.set_theme(theme::default()); - let path = match env::var_os("HOME") { - None => { - println!("$HOME not set."); - process::exit(1) - } - Some(path) => PathBuf::from(format!("{0}/.config/ncspot", path.into_string().unwrap())), - }; - - let cfg = config::load(path.to_str().unwrap()).expect("could not load configuration file"); let queue = Arc::new(Mutex::new(queue::Queue::new(event_manager.clone()))); let spotify = Arc::new(spotify::Spotify::new(