Added --debug-json option

This commit is contained in:
Mahesh Asolkar 2024-09-17 22:01:47 -07:00
parent 914e3f2a0c
commit 1c0b1e7d8b

View File

@ -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
},
};