From 6e05947498a2928d20ee08004d6469b7d4e97380 Mon Sep 17 00:00:00 2001 From: Dennis Date: Fri, 27 Jul 2018 23:43:24 +0200 Subject: [PATCH] Removed all remaining unwrap() and expect() functions from main.rs They now make use of the messages.rs error messages. --- src/main.rs | 26 +++++++++++++++++++++----- src/messages.rs | 13 ++++++++----- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7713e0f..a902f60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,9 +85,18 @@ fn main() { (0x413d , 0x2107) ]; - info!("Initializing HidApi. This can take a moment."); + info!("Initializing HID object. This can take a moment."); + + let api = match hidapi::HidApi::new() { + Ok(res) => { + info!("Succesfully initialized HID object."); + res + }, + Err(_) => { + error!("Could not initialize HID object.") + }, + }; - let api = hidapi::HidApi::new().expect("Hidapi init failed!"); let mut dev_path = String::new(); for device in &api.devices() { @@ -98,9 +107,16 @@ fn main() { } } } - - let dev = api.open_path(dev_path.as_str()).unwrap(); - info!("Succesfully opened device."); + + let dev = match api.open_path(dev_path.as_str()) { + Ok(res) => { + info!("Succesfully opened device."); + res + }, + Err(_) => { + error!("Could not open device. Make sure your device is connected. Maybe try to reconnect it.") + }, + }; // All options that need the device to be open diff --git a/src/messages.rs b/src/messages.rs index c049787..f0bd4d9 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -18,15 +18,18 @@ macro_rules! warning { #[macro_export] macro_rules! error { ($($arg:tt)*) => { - eprintln!("└ {:7} — {}", "Error".on_red().white(), format_args!($($arg)*)); - process::exit(0); + { + eprintln!("└ {:7} — {}", "Error".on_red().white(), format_args!($($arg)*)); + println!(""); + process::exit(0); + } }; } pub fn welcome(string: &str) { - println!("┌{}┐", "─".repeat(string.len() + 2)); - println!("│{text:^-width$}│", text = string, width = string.len() + 2); - println!("├{}┘", "─".repeat(string.len() + 2)); + println!("┌{}┐", "─".repeat(string.len() + 20)); + println!("│{text:^-width$}│", text = string, width = string.len() + 20); + println!("├{}┘", "─".repeat(string.len() + 20)); } pub fn goodbye() {