Browse Source

Get rid of last remaining uses of unwrap

Getty Ritter 5 years ago
parent
commit
5a51fadd8c
2 changed files with 18 additions and 9 deletions
  1. 1 1
      src/main.rs
  2. 17 8
      src/widgets.rs

+ 1 - 1
src/main.rs

@@ -150,7 +150,7 @@ fn draw(
     // set up our widgets
     let text = widgets::Text::new(left);
     let time = widgets::Time::new();
-    let bat = widgets::Battery::new().unwrap();
+    let bat = widgets::Battery::new()?;
 
     // and create a 'config' which tells us which widgets to draw from
     // the left, and which from the right

+ 17 - 8
src/widgets.rs

@@ -143,17 +143,26 @@ impl Battery {
 
 impl Widget for Battery {
     fn draw(&self, d: &Drawing, loc: Located) -> i32 {
-        let amt = self.read_status().unwrap();
+        let amt = self.read_status();
         let sz = d.size.ht - 8;
         let x = loc.target_x(d, sz);
-        if amt < 0.1 {
-            d.ctx.set_source_rgb(1.0, 0.0, 0.0);
-        } else if amt < 0.5 {
-            d.ctx.set_source_rgb(1.0, 1.0, 0.0);
-        } else {
-            d.ctx.set_source_rgb(0.0, 1.0, 0.5);
+        match amt {
+            Ok(x) if x < 0.1 =>
+                d.ctx.set_source_rgb(1.0, 0.0, 0.0),
+            Ok(x) if x < 0.5 =>
+                d.ctx.set_source_rgb(1.0, 1.0, 0.0),
+            Ok(_) =>
+                d.ctx.set_source_rgb(0.0, 1.0, 0.5),
+            Err(_) =>
+                d.ctx.set_source_rgb(0.0, 0.0, 0.0),
         }
-        d.ctx.rectangle(x, 8.0, sz as f64 * amt, sz as f64 - 8.0);
+
+        d.ctx.rectangle(
+            x,
+            8.0,
+            sz as f64 * amt.unwrap_or(1.0),
+            sz as f64 - 8.0,
+        );
         d.ctx.fill();
 
         d.ctx.set_source_rgb(1.0, 1.0, 1.0);