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 // Commandline parsing
#[derive(Parser, Debug)] #[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 { struct CommandlineArgs {
/// Name of the weather station /// Name of the weather station
#[arg(short, long, default_value = "khio")] #[arg(short, long, default_value = "khio")]
@ -33,8 +38,11 @@ struct CommandlineArgs {
/// Use Metric units. Imperial units otherwise /// Use Metric units. Imperial units otherwise
#[arg(short, long)] #[arg(short, long)]
metric: bool, metric: bool,
}
/// Show JSON data returned by query
#[arg(short, long)]
debug_json: bool,
}
// //
// Application options // Application options
@ -43,6 +51,7 @@ struct CommandlineArgs {
struct AppOptions { struct AppOptions {
units: TemperatureUnits, units: TemperatureUnits,
station: String, station: String,
debug_json: bool,
} }
// //
@ -76,8 +85,12 @@ impl BarWeather {
curl.write_function(move |data| { curl.write_function(move |data| {
// To debug returned JSON // To debug returned JSON
// use std::io::{stdout, Write}; if self.opts.debug_json {
// stdout().write_all(data).unwrap(); 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 v: Value = serde_json::from_str(str::from_utf8(data).unwrap()).unwrap();
let temperature_value :f32 = self.get_current_temperature(v); let temperature_value :f32 = self.get_current_temperature(v);
@ -86,7 +99,6 @@ impl BarWeather {
println!("{:.2}{}", temperature_value, temperature_unit); println!("{:.2}{}", temperature_value, temperature_unit);
Ok(data.len()) Ok(data.len())
} }
).unwrap(); ).unwrap();
curl.perform().unwrap(); curl.perform().unwrap();
@ -127,6 +139,7 @@ fn main() {
units: if cmd_args.metric { TemperatureUnits::Metric } units: if cmd_args.metric { TemperatureUnits::Metric }
else { TemperatureUnits::Imperial }, else { TemperatureUnits::Imperial },
station: cmd_args.station, station: cmd_args.station,
debug_json: cmd_args.debug_json
}, },
}; };