From 1c0b1e7d8bec5dfc19cc49252aac7a949cba4aad Mon Sep 17 00:00:00 2001 From: Mahesh Asolkar Date: Tue, 17 Sep 2024 22:01:47 -0700 Subject: [PATCH] Added --debug-json option --- src/main.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 947c634..d6b2378 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,12 @@ impl fmt::Display for TemperatureUnits { // Commandline parsing #[derive(Parser, Debug)] -#[command(version, about, long_about = None)] +#[command(name = "bar_weather")] +#[command(version = "1.0")] +#[command(about = "Gets weather info for the bar")] +#[command(about, long_about = "Get weather information for the station indicated +by the --station argument. Imperial units by default. +Use --metirc to use metric units")] struct CommandlineArgs { /// Name of the weather station #[arg(short, long, default_value = "khio")] @@ -33,8 +38,11 @@ struct CommandlineArgs { /// Use Metric units. Imperial units otherwise #[arg(short, long)] metric: bool, -} + /// Show JSON data returned by query + #[arg(short, long)] + debug_json: bool, +} // // Application options @@ -43,6 +51,7 @@ struct CommandlineArgs { struct AppOptions { units: TemperatureUnits, station: String, + debug_json: bool, } // @@ -76,8 +85,12 @@ impl BarWeather { curl.write_function(move |data| { // To debug returned JSON - // use std::io::{stdout, Write}; - // stdout().write_all(data).unwrap(); + if self.opts.debug_json { + use std::io::{stdout, Write}; + println!("-----> Start of debug"); + stdout().write_all(data).unwrap(); + println!("-----> End of debug"); + } let v: Value = serde_json::from_str(str::from_utf8(data).unwrap()).unwrap(); let temperature_value :f32 = self.get_current_temperature(v); @@ -86,7 +99,6 @@ impl BarWeather { println!("{:.2}{}", temperature_value, temperature_unit); Ok(data.len()) - } ).unwrap(); curl.perform().unwrap(); @@ -127,6 +139,7 @@ fn main() { units: if cmd_args.metric { TemperatureUnits::Metric } else { TemperatureUnits::Imperial }, station: cmd_args.station, + debug_json: cmd_args.debug_json }, };