From e1c6aca799f0914ef2d52a04cba7c60ebe236474 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Leonard=20K=C3=B6nig?= <leonard.r.koenig@googlemail.com>
Date: Tue, 7 Jul 2020 13:48:20 +0200
Subject: [PATCH] button subsystem: use manual panic! instead of unwrap()

unwrap only works if the error value implements Debug, however since the
buttons subsystem doesn't return TockValue<E> but TockError this isn't the case
(anymore).  In theory one should think about refactoring check_user_presence()
to return TockResult<()> and possibly implementing some way for Ctap2StatusCode
to be converted to TockError enums or similar.  Then we can just pass errors
up the stack instead of panicking early.
---
 src/main.rs | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 0767a47..18b4646 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -333,17 +333,26 @@ fn check_user_presence(cid: ChannelID) -> Result<(), Ctap2StatusCode> {
         Ok(x) => x,
         Err(err) => panic!("Drivers were already taken when attempting to retrieve them"),
     };
-    let buttons_driver = all_drivers.buttons.init_driver().unwrap();
-    let _buttons_callback = buttons_driver.subscribe(&mut |_button_num, state| {
+    let buttons_driver = match all_drivers.buttons.init_driver() {
+        Ok(x) => x,
+        Err(err) => panic!("Couldn't init buttons subsystem"),
+    };
+    let _buttons_callback = match buttons_driver.subscribe(&mut |_button_num, state| {
         match state {
             ButtonState::Pressed => button_touched.set(true),
             ButtonState::Released => (),
         };
-    }).unwrap();
+    }) {
+        Ok(x) => x,
+        Err(err) => panic!("Couldn't subscribe to button"),
+    };
     let mut buttons = buttons_driver.buttons();
     // At the moment, all buttons are accepted. You can customize your setup here.
     for mut button in &mut buttons {
-        button.enable_interrupt().unwrap();
+        match button.enable_interrupt() {
+            Ok(_) => {},
+            Err(_) => panic!("Couldn't enable interrupts"),
+        }
     }
 
     let mut keepalive_response = Ok(());
@@ -362,8 +371,14 @@ fn check_user_presence(cid: ChannelID) -> Result<(), Ctap2StatusCode> {
         let mut keepalive_callback = timer_context.with_callback(|_, _| {
             keepalive_expired.set(true);
         });
-        let mut keepalive = keepalive_callback.init().unwrap();
-        let keepalive_alarm = keepalive.set_alarm(KEEPALIVE_DELAY).unwrap();
+        let mut keepalive = match keepalive_callback.init() {
+            Ok(x) => x,
+            Err(_) => panic!("Couldn't init callback!"),
+        };
+        let keepalive_alarm = match keepalive.set_alarm(KEEPALIVE_DELAY) {
+            Ok(x) => x,
+            Err(_) => panic!("Couldn't set alarm"),
+        };
 
         // Wait for a button touch or an alarm.
         usb_ctap_hid::yieldk_for(|| button_touched.get() || keepalive_expired.get());
@@ -398,7 +413,10 @@ fn check_user_presence(cid: ChannelID) -> Result<(), Ctap2StatusCode> {
 
     // Cleanup button callbacks.
     for mut button in &mut buttons {
-        button.disable_interrupt().unwrap();
+        match button.disable_interrupt() {
+            Ok(_) => {},
+            Err(_) => panic!("Couldn't disable interrupts"),
+        };
     }
 
     // Returns whether the user was present.
-- 
GitLab