summaryrefslogtreecommitdiff
path: root/i3status-rust/407.patch
blob: ac4ca6b7a1cc908ebc11bd14b8d3cc1244eb4c57 (plain)
    1 From 41fd7bf7a7a548aa3e1da05a3ac8ce2f873249c0 Mon Sep 17 00:00:00 2001
    2 From: Bram Vandenbogaerde <contact@bramvdb.com>
    3 Date: Sat, 20 Jul 2019 19:05:58 +0200
    4 Subject: [PATCH] Made wind speed and degree optional by providing a default
    5  value + cleaned up the JSON AST traversal code
    6 
    7 ---
    8  src/blocks/weather.rs | 97 +++++++++++++++++--------------------------
    9  1 file changed, 39 insertions(+), 58 deletions(-)
   10 
   11 diff --git a/src/blocks/weather.rs b/src/blocks/weather.rs
   12 index 2c75067..8236b17 100644
   13 --- a/src/blocks/weather.rs
   14 +++ b/src/blocks/weather.rs
   15 @@ -48,6 +48,13 @@ pub struct Weather {
   16      update_interval: Duration,
   17  }
   18  
   19 +fn malformed_json_error() -> Error {
   20 +    BlockError(
   21 +            "weather".to_string(),
   22 +            "Malformed JSON.".to_string(),
   23 +    )
   24 +}
   25 +
   26  impl Weather {
   27      fn update_weather(&mut self) -> Result<()> {
   28          match self.service {
   29 @@ -97,67 +104,41 @@ impl Weather {
   30                          format!("API Error: {}", val.as_str().unwrap()),
   31                      ));
   32                  };
   33 -                let raw_weather = match json.pointer("/weather/0/main")
   34 +                let raw_weather = json.pointer("/weather/0/main")
   35                      .and_then(|v| v.as_str())
   36 -                    .map(|s| s.to_string()) {
   37 -                    Some(v) => v,
   38 -                    None => {
   39 -                        return Err(BlockError(
   40 -                            "weather".to_string(),
   41 -                            "Malformed JSON.".to_string(),
   42 -                        ));
   43 -                    }
   44 -                };
   45 -                let raw_temp = match json.pointer("/main/temp").and_then(|v| v.as_f64()) {
   46 -                    Some(v) => v,
   47 -                    None => {
   48 -                        return Err(BlockError(
   49 -                            "weather".to_string(),
   50 -                            "Malformed JSON.".to_string(),
   51 -                        ));
   52 -                    }
   53 -                };
   54 -                let raw_wind_speed = match json.pointer("/wind/speed").and_then(|v| v.as_f64()) {
   55 -                    Some(v) => v,
   56 -                    None => {
   57 -                        return Err(BlockError(
   58 -                            "weather".to_string(),
   59 -                            "Malformed JSON.".to_string(),
   60 -                        ));
   61 -                    }
   62 -                };
   63 -                let raw_wind_direction = match json.pointer("/wind/deg").and_then(|v| v.as_f64()) {
   64 -                    Some(v) => v,
   65 -                    None => {
   66 -                        return Err(BlockError(
   67 -                            "weather".to_string(),
   68 -                            "Malformed JSON.".to_string(),
   69 -                        ));
   70 -                    }
   71 -                };
   72 -                let raw_location = match json.pointer("/name").and_then(|v| v.as_str()).map(|s| {
   73 -                    s.to_string()
   74 -                }) {
   75 -                    Some(v) => v,
   76 -                    None => {
   77 -                        return Err(BlockError(
   78 -                            "weather".to_string(),
   79 -                            "Malformed JSON.".to_string(),
   80 -                        ));
   81 -                    }
   82 -                };
   83 +                    .map(|s| s.to_string())
   84 +                    .ok_or_else(malformed_json_error)?;
   85 +
   86 +                let raw_temp = json.pointer("/main/temp").and_then(|v| v.as_f64()).ok_or_else(malformed_json_error)?;
   87 +
   88 +                let raw_wind_speed: f64= json.pointer("/wind/speed")
   89 +                    .map_or(Some(0.0), |v| v.as_f64()) // provide default value 0.0
   90 +                    .ok_or_else(malformed_json_error)?; // error when conversion to f64 fails
   91 +
   92 +                let raw_wind_direction: Option<f64>= json.pointer("/wind/deg")
   93 +                    .map_or(Some(None), |v| v.as_f64().and_then(|v| Some(Some(v)))) // provide default value None
   94 +                    .ok_or_else(malformed_json_error)?; // error when conversion to f64 fails
   95 +
   96 +
   97 +                let raw_location = json.pointer("/name")
   98 +                    .and_then(|v| v.as_str())
   99 +                    .map(|s| s.to_string())
  100 +                    .ok_or_else(malformed_json_error)?;
  101  
  102                  // Convert wind direction in azimuth degrees to abbreviation names
  103 -                fn convert_wind_direction(direction: f64) -> String {
  104 -                    match direction.round() as i64 {
  105 -                        24 ... 68 => "NE".to_string(),
  106 -                        69 ... 113 => "E".to_string(),
  107 -                        114 ... 158 => "SE".to_string(),
  108 -                        159 ... 203 => "S".to_string(),
  109 -                        204 ... 248 => "SW".to_string(),
  110 -                        249 ... 293 => "W".to_string(),
  111 -                        294 ... 338 => "NW".to_string(),
  112 -                        _ => "N".to_string()
  113 +                fn convert_wind_direction(direction_opt: Option<f64>) -> String {
  114 +                    match direction_opt {
  115 +                        Some(direction) => match direction.round() as i64 {
  116 +                            24 ... 68 => "NE".to_string(),
  117 +                            69 ... 113 => "E".to_string(),
  118 +                            114 ... 158 => "SE".to_string(),
  119 +                            159 ... 203 => "S".to_string(),
  120 +                            204 ... 248 => "SW".to_string(),
  121 +                            249 ... 293 => "W".to_string(),
  122 +                            294 ... 338 => "NW".to_string(),
  123 +                            _ => "N".to_string()
  124 +                        },
  125 +                        None => "-".to_string()
  126                      }
  127                  }
  128  

Generated by cgit